首页 > 代码库 > spring整合quartz

spring整合quartz

 Quartz 介绍

    Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; 
    Quartz框架是一个全功能、开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统。Quartz可以执行上千上万的任务调度。
 
   核心概念
     Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节
     相关文档:http://www.quartz-scheduler.org/documentation/quartz-2.2.x/quick-start

实战 

  第一步 :spring、quartz 相应的jar包,添加到项目中(需要的call me)

    /WEB-INF/lib/quartz-2.2.1.jar

    以及spring的一些必要包

 

第二:在spring中配置quartz任务调度
                             1.在spring配置文件中加入以下代码,单独新建quartz配置文件,方便管理
<!-- 包含quartz定时任运行方式 -->
<import resource="classpath:applicationContext-quartz.xml"/>

  

    新建applicationContext-quartz.xml,内容如下。以下为两个不同时间的时间任务配置,(work1,work2),如果只需要配置一个任务,请去重复

<?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">

<!-- 6. 配置QUARTZ -->


<!-- 集成方式:JobDetailFactoryBean,并且任务类需要继承QuartzJobBean-->
<!-- 定义jobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="http://www.mamicode.com/true" />
<!-- 目标类 /wmuitp/src/test/SpringQuartz.java-->
<property name="jobClass" value="http://www.mamicode.com/blog.quartz.SpringQuartzTest"></property>

<!-- 在这个例子中,jobDataAsMap没有用,此目标类中接受的参数 ,若参数为service,则可以在此进行参数配置,类似struts2 -->
<!--
<property name="jobDataAsMap">
<map>
<entry key="service"><value>simple is the beat</value></entry>
</map>
</property>
-->
</bean>



<bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="http://www.mamicode.com/true" />
<!-- 目标类 /wmuitp/src/test/SpringQuartz.java-->
<property name="jobClass" value="http://www.mamicode.com/blog.quartz.SpringQuartzTest2"></property>
</bean>

<!-- 定义simpleTrigger触发器 -->
<!--
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="repeatCount">
<value>8</value>
</property>
<property name="repeatInterval">
<value>1000</value>
</property>
<property name="startDelay">
<value>4</value>
</property>
</bean>
-->

<!-- 另一种触发器是CornTrigger -->
<bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<!-- 每个4秒触发 -->
<property name="cronExpression" value="http://www.mamicode.com/0/4 * * * * ?"/>
</bean>
<!-- 另一种触发器是CornTrigger2 -->
<bean id="cornTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail2"/>
<!-- 每个10秒触发 -->
<property name="cronExpression" value="http://www.mamicode.com/0/10 * * * * ?"/>
</bean>

<!-- 定义核心调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger"/>
</property>
</bean>
<!-- 定义核心调度器2 -->
<bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger2"/>
</property>
</bean>

</beans>

  #目标类
    <property name="jobClass" value="http://www.mamicode.com/test.SpringQuartzTest"></property>

  下面第三步:编写目标类
    
package test;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class SpringQuartzTest extends QuartzJobBean
{

    /*业务实现*/
    public void work() {
        System.out.println("执行调度任务:"+new Date());
    }

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        this.work();
    }
}

spring整合quartz