Spring--quartzJob配置

maven依賴:web

    <!-- 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配置也會有不一樣。spring

自定義job:app

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配置:maven

<!-- 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,會出現重複執行的狀況。url

<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>
相關文章
相關標籤/搜索