Spring2.5与Quartz的整合

applicationContext_quartz.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="false">

    <!-- task -->
    <!-- 定义了一个任务 -->
    <bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
       <property name="jobClass">
        <value>com.sgfm.datacenter.sys.DataTimingTask</value><!-- 类的名字 有这个类完成具体的 任务 -->
       </property>
    </bean>

    <!-- 第二步 调度定时任务 -->
    <!--这种配置与第一种方法效果一样 -->
    <bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail">
         <ref bean="quartzClock"/>
        </property>

        <property name="startDelay"><!-- 这里是服务启动后延时多少时间,开始计时任务,单位ms -->
         <value>6000</value>  
        </property>

        <property name="repeatInterval"><!-- 这里是每隔多长时间就进行一次计时任务,单位ms -->
         <value>2000</value>  
        </property>
    </bean>

    <!-- 这种配置可以精确几点执行定时任务 -->
    <!-- 定义了任务的执行方式 -->
    <bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean" >
       <property name="jobDetail">
        <ref bean="quartzClock"></ref>
       </property>

       <property name="cronExpression">
        <value>0 0 3 * * ? </value> <!-- 每天凌晨3点的定时-->
       </property>
    </bean>

    <!--第三步 启动定时任务,注意这里的ref bean -->
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       <property name="triggers">
        <list>
         <ref bean="cronQuartzClock"></ref>
        </list>
       </property>
    </bean>

</beans>

实现任务模块

package com.sgfm.datacenter.sys;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class DataTimingTask extends QuartzJobBean{
    private static Logger logger = Logger.getLogger(SystemMgr.class.getName());
    public DataTimingTask() {
        super();
    }
    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        // TODO Auto-generated method stub
        System.out.println("草根,你好!");
    }
}

你可能感兴趣的:(spring,quartz)