quartz

spring3.X以上才适配quartz 2.X版本
pom
        <!-- quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>


spring-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz" lazy-init="true" autowire="no"
          class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="payNotifyJobTrigger" />
            </list>
        </property>
    </bean>
    <!-- 启动触发器的配置结束 -->

    <!-- quartz-2.x的配置 -->
    <bean id="payNotifyJobTrigger"
          class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="payNotifyJobDetail" />
        </property>
        <property name="cronExpression">
            <value>1,3,5,8 * * * * ?</value>
        </property>
    </bean>
    <!-- 调度的配置结束 -->

    <!-- job的配置开始 -->
    <bean id="payNotifyJobDetail"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="payNotifyJob" />
        </property>
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    <!-- job的配置结束 -->

    <!-- 工作的bean -->
    <bean id="payNotifyJob" class="com.sogou.pay.notify.timer.PayNotifyJob" />
</beans>

job
package com.sogou.pay.notify.timer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

/**
* User: Liwei
* Date: 2014/12/31
* Time: 15:07
*/
public class PayNotifyJob {
    public void work(){
        System.out.println("Quartz的任务调度! " + new Date());
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml");
        context.getBean("startQuertz");
        System.out.println("work start");
    }
}


还可参考链接:
http://www.ibm.com/developerworks/cn/opensource/os-cn-quartz/
http://blog.csdn.net/lk_blog/article/details/11744621
http://www.blogjava.net/baoyaer/articles/155645.html
http://blog.csdn.net/tender001/article/details/7723901
https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

你可能感兴趣的:(quartz)