Spring任务调度配置

最近项目使用任务调度的功能很多,将spring集成任务调度的配置记录一下,以备不时之需。

需要的jar包:quartz-1.5.2.jar(spring的jar包就不用说了)

配置如下:

<!--任务调度配置-->
    <!--定义jobDetail,定时执行createFileStatusService这个bean中的deleteAndDownloadProgram方法-->
    <bean id="defineJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!--调度的对象-->
        <property name="targetObject">  
            <ref bean="createFileStatusService"/>  
        </property>  
        <!--调度对象的方法-->
        <property name="targetMethod">  
            <value>deleteAndDownloadProgram</value>  
        </property>  
    </bean>  
  
    <!--触发器设置,设置触发的jobDetail是defineJobDetail,触发的时间为每天凌晨2:00-->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="defineJobDetail"/>
        </property>
        <property name="cronExpression">
            <!--触发时间表达式,从左到右,秒、分、时、日、月、星期,*号为通配符,?号为不设置该字段-->
            <value>0 0 2 * * ?</value>
        </property>
    </bean>
  
    <!--管理触发器列表,可以在bean的list中放置多个触发器-->
    <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref local="cronTrigger" />  
            </list>  
        </property>  
    </bean>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章