Spring 3 + Quartz 1.8.6 Scheduler Example--reference

In this tutorial, we will show you how to integrate Spring with Quartz scheduler framework. Spring comes with many handy classes to support Quartz, and decouple your class to Quartz APIs.

Tools Used :

  1. Spring 3.1.2.RELEASE
  2. Quartz 1.8.6
  3. Eclipse 4.2
  4. Maven 3

Why NOT Quartz 2?
Currently, Spring 3 is still NOT support Quartz 2 APIs, see this SPR-8581 bug report. Will update this article again once bug fixed is released.

1. Project Dependency

You need following dependencies to integrate Spring 3 and Quartz 1.8.6

File : pom.xml

...
<dependencies>

    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <!-- QuartzJobBean in spring-context-support.jar -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <!-- Spring + Quartz need transaction -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <!-- Quartz framework -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>1.8.6</version>
    </dependency>

</dependencies>
...

2. Scheduler Task

Create a normal Java class, this is the class you want to schedule in Quartz.

File : RunMeTask.java

package com.mkyong.common;

public class RunMeTask {
    public void printMe() {
        System.out.println("Spring 3 + Quartz 1.8.6 ~");
    }
}

3. Declare Quartz Scheduler Job

With Spring, you can declare Quartz job in two ways :

3.1 MethodInvokingJobDetailFactoryBean
This is the simplest and straightforward method, suitable for simple scheduler.

<bean id="runMeJob"
 	class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
 
	<property name="targetObject" ref="runMeTask" />
	<property name="targetMethod" value="printMe" />
 
</bean>

3.2 JobDetailBean
The QuartzJobBean is more flexible and suitable for complex scheduler. You need to create a class extends the Spring’sQuartzJobBean, and define the method you want to schedule in executeInternal() method, and pass the scheduler task (RunMeTask) via setter method.

File : RunMeJob.java

package com.mkyong.common;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class RunMeJob extends QuartzJobBean {
	private RunMeTask runMeTask;
 
	public void setRunMeTask(RunMeTask runMeTask) {
		this.runMeTask = runMeTask;
	}
 
	protected void executeInternal(JobExecutionContext context)
		throws JobExecutionException {
 
		runMeTask.printMe();
 
	}
}

Configure the target class via jobClass and method to run via jobDataAsMap.

<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
 
	<property name="jobClass" value="com.mkyong.common.RunMeJob" />
 
	<property name="jobDataAsMap">
		<map>
			<entry key="runMeTask" value-ref="runMeTask" />
		</map>
	</property>
 
</bean>

4. Trigger

Configure Quartz trigger to define when will run your scheduler job. Two type of triggers are supported :

4.1 SimpleTrigger
It allows to set the start time, end time, repeat interval to run your job.

        <!-- Simple Trigger, run every 5 seconds -->
	<bean id="simpleTrigger"
                class="org.springframework.scheduling.quartz.SimpleTriggerBean">
 
		<property name="jobDetail" ref="runMeJob" />
		<property name="repeatInterval" value="5000" />
		<property name="startDelay" value="1000" />
 
	</bean>

4.2 CronTrigger
It allows Unix cron expression to specify the dates and times to run your job.

	<!-- Cron Trigger, run every 5 seconds -->
	<bean id="cronTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
 
		<property name="jobDetail" ref="runMeJob" />
		<property name="cronExpression" value="0/5 * * * * ?" />
 
	</bean>

Note
The Unix cron expression is highly flexible and powerful, read more in following websites :

  1. http://en.wikipedia.org/wiki/CRON_expression
  2. http://www.quartz-scheduler.org/docs/examples/Example3.html

5. Scheduler Factory

Create a Scheduler factory bean to integrate both job detail and trigger together.

   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	<property name="jobDetails">
	   <list>
	      <ref bean="runMeJob" />
	   </list>
	</property>
 
	<property name="triggers">
	    <list>
		<ref bean="simpleTrigger" />
	    </list>
	</property>
   </bean>

6. Spring Bean Configuration File

Complete Spring’s bean configuration file.

File : Spring-Quartz.xml

<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-3.0.xsd">
 
	<bean id="runMeTask" class="com.mkyong.common.RunMeTask" />
 
	<!-- Spring Quartz -->
	<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
 
		<property name="jobClass" value="com.mkyong.common.RunMeJob" />
 
		<property name="jobDataAsMap">
		  <map>
			<entry key="runMeTask" value-ref="runMeTask" />
		  </map>
		</property>
 
	</bean>
 
	<!--
	<bean id="runMeJob"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="runMeTask" />
		<property name="targetMethod" value="printMe" />
	</bean>
	-->
 
	<!-- Simple Trigger, run every 5 seconds -->
	<bean id="simpleTrigger"
                class="org.springframework.scheduling.quartz.SimpleTriggerBean">
 
		<property name="jobDetail" ref="runMeJob" />
		<property name="repeatInterval" value="5000" />
		<property name="startDelay" value="1000" />
 
	</bean>
 
	<!-- Cron Trigger, run every 5 seconds -->
	<bean id="cronTrigger"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
 
		<property name="jobDetail" ref="runMeJob" />
		<property name="cronExpression" value="0/5 * * * * ?" />
 
	</bean>
 
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="jobDetails">
			<list>
				<ref bean="runMeJob" />
			</list>
		</property>
 
		<property name="triggers">
			<list>
				<ref bean="simpleTrigger" />
			</list>
		</property>
	</bean>
 
</beans>

7. Demo

Run it ~

package com.mkyong.common;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App
{
    public static void main( String[] args ) throws Exception
    {
    	new ClassPathXmlApplicationContext("Spring-Quartz.xml");
    }
}

Output to console.

Jul 25, 2012 3:23:09 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Spring 3 + Quartz 1.8.6 ~ //run every 5 seconds
Spring 3 + Quartz 1.8.6 ~

原文地址:http://www.mkyong.com/spring/spring-quartz-scheduler-example/

Spring 3 + Quartz 1.8.6 Scheduler Example--reference

时间: 2024-10-10 16:39:32

Spring 3 + Quartz 1.8.6 Scheduler Example--reference的相关文章

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

Spring整合Quartz实现定时任务调度

一. 核心类 1. Job: 表示一个工作, 具体的业务处理都在这里. 2. JobDetail: 表示一个具体的可执行的调度程序. 3. Trigger: 用于调度参数的配置(什么时候去调用Job). 4. Scheduler: 表示一个调度容器, 容器中有一个线程池, 用来并行调度执行每个作业, 一个调度容器中可以注册多个JobDetail和Trigger. 二. 整合spring 1. 代码结构图: 2. applicationContext.xml <?xml version="1

Spring整合Quartz实现持久化、动态设定时间

一.spring整合 网上一搜有很多整合的方式,这里我采用了其中的一种(暂时还没有对其他的方法研究过). 对于spring的整合其中的任务,spring提供了几个类.接口(这些类都实现了Job接口): org.springframework.scheduling.quartz.QuartzJobBean org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.MethodInvokingJob org.sp

spring整合quartz并持久化

spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overdueRecall" class="com.sursen.souba.ddlibserve.quartz.OverdueRecallTimerTask" /> <!--定义定时执行overdueRecallTimerTask 这个bean中的overdueRecall(

spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)

背景:因为在项目中用到了定时任务,当时想到了spring的quartz,写完发现费了很大功夫,光是整合就花了一上午,其中最大的问题就是版本问题,项目中用的是spring3.2.8的版本,查阅发现,3.0以上的版本需要使用quartz2.X以上版本,我就去官网下载了2.1.7的quartz,结果发现jar包与spring冲突,最后使用了quartz1.6.0版本. spring与quartz整合第一步需要导jar包,这个在百度搜下quartz的jar,下载一个 第二步:分布式定时任务,是基于数据库

Quartz学习总结(1)——Spring集成Quartz框架

一.Quartz简介 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序.Jobs可以做成标准的Java组件或 EJBs.Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多对多关系,

Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ©Copyright 蕃薯耀 2017年9月6日 http://ww

Spring与Quartz使用说明 job不同步执行(第一个job完成后才开始第二个job)

转自:http://blog.sina.com.cn/s/blog_675e9aeb0100qghp.html 最 近因为项目后台需要使用一个线程去定时跑一些任务,所以看了一下Spring和Quartz的使用与集成.例如发短信.邮件.根据实际使用,此集成使用比 自己去写线程方便和灵活,可以不用代码实现复杂的计划任务,如“1月份的每周三.周五的早上9点至10点每5钟一次任务”.所以分享一下. Quartz官方网址:http://www.quartz-scheduler.org/ 集成方法 集成起来

spring 与 quartz 的整合

最近研究了一下定时任务,比较好的实现就是使用 quartz 来实现.在这里写个笔记记录一下 spring 整合 quartz 的步骤,仅供参考. 我是用的 Maven 来管理项目,先把需要的依赖粘一下 Maven 中的 pom 文件的配置: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns