Spring--quartzJob配置

maven依赖:

    <!-- quartz job start-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!-- quartz job end-->

使用时要注意spring版本,其中3.2.x之前只支持1.8.x之前的版本,对应spring配置也会有不同。

自定义job:

public class MyQuartzJobPojo {

    private static Logger logger = Logger.getLogger(MyQuartzJobPojo.class);
    public void doMyQuartzJobPojo(){
        
        
        System.out.println("run in my quartz job pojo ..."+(new Date()).getTime());
    }
    
}

spring-quartz.xml配置:

<!-- quartz job pojo start -->
    <bean id="myJob" class="com.copsec.railway.im.quartzJob.MyQuartzJobPojo"/>
    <!-- job detail start -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJob"/>
        <property name="targetMethod" value="doMyQuartzJobPojo" />
    </bean>
    <!-- job detail stop -->
    <!-- job trigger start -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        <property name="startDelay" value="0" /> <!-- 调度工厂实例化后,经过0秒开始执行调度 -->
        <property name="cronExpression"><!-- 分时日月周  0 0 1 * * ? -->  
            <value>0/10 * * * * ?</value>  
        </property>  
    </bean>
    <!-- job trigger end -->
    <!-- schedule start -->
    <bean id="myScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>
    <!-- schedule stop -->
    <!-- quartz job pojo end -->

web.xml当中配置,不要把这个配置信息写入applicationContext.xml,会出现重复执行的情况。

<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml,classpath:spring-quartzJob.xml</param-value>  
    </context-param>
<!-- 防止spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>DispatchServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatchServlet</servlet-name>
        <!-- 默认匹配所有请求 -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

 

posted @ 2018-01-19 23:49  向东1991  阅读(248)  评论(0编辑  收藏  举报