博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3 + Quartz 1.8.6 Scheduler Example--reference
阅读量:7028 次
发布时间:2019-06-28

本文共 4423 字,大约阅读时间需要 14 分钟。

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 . 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

...
org.springframework
spring-core
3.1.2.RELEASE
org.springframework
spring-context-support
3.1.2.RELEASE
org.springframework
spring-tx
3.1.2.RELEASE
org.quartz-scheduler
quartz
1.8.6
...

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.

 
 

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.

 
 
 

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.

 
 

4.2 CronTrigger

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

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

 

5. Scheduler Factory

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

 

6. Spring Bean Configuration File

Complete Spring’s bean configuration file.

File : Spring-Quartz.xml

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 startSchedulerINFO: Starting Quartz Scheduler nowSpring 3 + Quartz 1.8.6 ~ //run every 5 secondsSpring 3 + Quartz 1.8.6 ~

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

转载地址:http://zdrxl.baihongyu.com/

你可能感兴趣的文章