Spring结合Quartz实现多任务定时调用

简介:

      Quartz框架提供了丰富的任务调度支持,比如,在何时执行何种任务,它是一个开源的由OpenSymphony维护的项目,开发者能够在Java EE,或单独的Java SE应用中使用它。无论是简单的任务调度,还是复杂的企业级应用,Quartz都能够很好地胜任。

      

如果开发者需要开发如下方面的应用,则Quartz是理想的选择。

◆驱动工作流:比如,如果新创建的流程任务需要在2小时内处理完,则在2小时后Quartz会检查订单是否成功处理。如果没有处理,则Quartz会依据工作流定义的规则来对订单进行处理,销毁它,或者进行其他处理。

◆系统维护工作:比如,在每个工作日的固定时间将RDBMS中的内容导出为XML文件。

配置Spring的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<? 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-3.0.xsd
">
 
  
 
  < bean  class = "org.springframework.scheduling.quartz.SchedulerFactoryBean"  lazy-init = "false"  autowire = "no"  >
 
   < property  name = "triggers" >
 
    < list >
 
     < ref  bean = "testTrigger"  />
 
     < ref  bean = "testTriggerTwo"  />
 
    </ list >
 
   </ property >
 
   < property  name = "autoStartup"  value = "true"  />
 
  </ bean >
 
  
 
  < bean  id = "testTrigger"  class = "org.springframework.scheduling.quartz.CronTriggerBean" >
 
   < property  name = "jobDetail"  ref = "testJobDetail"  />
 
   < property  name = "cronExpression"  value = "*/2 * * * * ?"  />
 
   <!--
 
    每隔2秒钟触发一次
 
   -->
 
  </ bean >
 
  < bean  id = "testTriggerTwo"  class = "org.springframework.scheduling.quartz.CronTriggerBean" >
 
   < property  name = "jobDetail"  ref = "testJobDetailTwo"  />
 
   < property  name = "cronExpression"  value = "*/5 * * * * ?"  />
 
   <!--
 
    每隔5秒钟触发一次
 
   -->
 
  </ bean >
 
  < bean  id = "testJobDetail"
 
   class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
 
   < property  name = "targetObject"  ref = "testJob"  />
 
   < property  name = "targetMethod"  value = "execute"  />
 
   < property  name = "concurrent"  value = "false"  />
 
   <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
 
  </ bean >
 
  < bean  id = "testJobDetailTwo"
 
   class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
 
   < property  name = "targetObject"  ref = "testJobTwo"  />
 
   < property  name = "targetMethod"  value = "execute"  />
 
   < property  name = "concurrent"  value = "false"  />
 
   <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
 
  </ bean >
 
  < bean  id = "testJob"  class = "com.mzsx.spring.quartz.TestJob" ></ bean >
 
  < bean  id = "testJobTwo"  class = "com.mzsx.spring.quartz.TestJobTwo" ></ bean >
 
</ beans >

 

 

任务一:TestJob

1
2
3
4
5
6
7
8
9
10
11
package  com.mzsx.spring.quartz;
import  java.util.Date;
public  class  TestJob {  
     public  void  execute(){  
         try {  
          System.out.println( "TestJob:------------:" + new  Date());
          } catch (Exception ex){  
              ex.printStackTrace();  
          }  
      }  
}

 

 

任务二:TestJobTwo

1
2
3
4
5
6
7
8
9
10
11
package  com.mzsx.spring.quartz;
import  java.util.Date;
public  class  TestJobTwo {  
     public  void  execute(){  
         try {  
          System.out.println( "TestJobTwo:------------:" + new  Date());
          } catch (Exception ex){  
              ex.printStackTrace();  
          }  
      }  
}

 

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package  com.mzsx.spring.test;
import  java.util.Date;
import  javax.security.auth.Destroyable;
import  org.junit.Test;
import  org.springframework.beans.factory.BeanFactory;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.AbstractApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  com.mzsx.spring.ClasspathDemo;
import  com.mzsx.spring.aop.StaticLog;
import  com.mzsx.spring.contorller.UserContorller;
import  com.mzsx.spring.el.Cat;
import  com.mzsx.spring.el.Dog;
import  com.mzsx.spring.initafter.InitAfter;
import  com.mzsx.spring.initafter.InitMethodAfter;
import  com.mzsx.spring.model.Person;
import  com.mzsx.spring.service.IUserService;
public  class  TestQuartz {
  @Test
  public  void  testQuartz(){
   System.out.println( "Test start....." + new  Date());
   ApplicationContext context =  new  ClassPathXmlApplicationContext( "quartz.xml" );
   try  {
    Thread.sleep( 30000 );
   catch  (InterruptedException e) {
    e.printStackTrace();
   }
   System.out.println( "Test end." );
  }
  
  
}

 

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Test start.....Tue Jul  08  14 : 02 : 06  CST  2014
 
2014 - 7 - 8  14 : 02 : 06  org.springframework.context.support.AbstractApplicationContext prepareRefresh
 
信息: Refreshing 
org.springframework.context.support.ClassPathXmlApplicationContext @1ec8909
: startup date [Tue Jul  08  14 : 02 : 06  CST  2014 ]; root of context hierarchy
 
2014 - 7 - 8  14 : 02 : 07  org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
 
信息: Loading XML bean definitions from  class  path resource [quartz.xml]
 
2014 - 7 - 8  14 : 02 : 07  org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
 
信息: Pre-instantiating singletons in 
org.springframework.beans.factory.support.DefaultListableBeanFactory @186f247
: defining beans [org.springframework.scheduling.quartz.SchedulerFactoryBean# 0 ,testTrigger,testTriggerTwo,testJobDetail,testJobDetailTwo,testJob,testJobTwo]; root of factory hierarchy
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.core.QuartzScheduler <init>
 
信息: Quartz Scheduler v. 1.6 . 0  created.
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.simpl.RAMJobStore initialize
 
信息: RAMJobStore initialized.
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.impl.StdSchedulerFactory instantiate
 
信息: Quartz scheduler  'org.springframework.scheduling.quartz.SchedulerFactoryBean#0'  initialized from an externally provided properties instance.
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.impl.StdSchedulerFactory instantiate
 
信息: Quartz scheduler version:  1.6 . 0
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.core.QuartzScheduler setJobFactory
 
信息: JobFactory set to: 
org.springframework.scheduling.quartz.AdaptableJobFactory @16ea269
 
2014 - 7 - 8  14 : 02 : 07  org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup start
 
信息: Starting beans in phase  2147483647
 
2014 - 7 - 8  14 : 02 : 07  org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
 
信息: Starting Quartz Scheduler now
 
2014 - 7 - 8  14 : 02 : 07  org.quartz.core.QuartzScheduler start
 
信息: Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
 
TestJob:------------:Tue Jul  08  14 : 02 : 08  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 10  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 10  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 12  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 14  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 15  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 16  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 18  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 20  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 20  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 22  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 24  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 25  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 26  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 28  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 30  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 30  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 32  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 34  CST  2014
 
TestJobTwo:------------:Tue Jul  08  14 : 02 : 35  CST  2014
 
TestJob:------------:Tue Jul  08  14 : 02 : 36  CST  2014
 
Test end.

本文转自 梦朝思夕 51CTO博客,原文链接:
http://blog.51cto.com/qiangmzsx/1435837

 

相关文章
|
1月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
1月前
|
监控 Java 数据处理
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
|
3月前
|
Java 调度 流计算
在使用Spring Boot启动Flink处理任务时
在使用Spring Boot启动Flink处理任务时【1月更文挑战第22天】【1月更文挑战第108篇】
46 1
|
5月前
|
Java Spring
spring集成Quartz时区问题造成任务晚执行八小时
spring集成Quartz时区问题造成任务晚执行八小时
|
6月前
|
XML Java 大数据
Spring Batch:处理大数据和批量任务的解决方案
Spring Batch:处理大数据和批量任务的解决方案
184 0
|
7月前
|
XML Java 调度
Spring定时任务-任务执行和调度—官方原版
Spring定时任务-任务执行和调度—官方原版
67 0
|
8月前
|
Java Spring 容器
Spring Boot 定义系统启动任务,你会几种方式?
Spring Boot 定义系统启动任务,你会几种方式?
|
8月前
|
Java 数据库连接 数据库
quartz(四)如何在job中使用spring自动注入
quartz(四)如何在job中使用spring自动注入
96 0
|
8月前
|
Java Spring
Spring或SpringBoot项目随项目启动而启动线程执行特定任务的方法
项目启动,你是否有需求需要某个任务能随着项目启动就去执行某个任务呢?
360 0
|
10月前
|
消息中间件 Java Spring
Spring Boot异步任务、异步消息
1.异步任务 1.1.概述 举一个例子,我现在有一个网上商城,客户在界面点击下单后,后台需要完成两步: 1.创建客户订单 2.发短信通知客户订单号 这里面第2步是个高耗时的操作,如果全部挤在一条主线程里做,效果就会是客户点了一下界面的下单按钮,然后转半天圈,直到客户收到短信后,界面才停止转圈。 这里面我们发现其实我们并不需要等第2步完全跑完再返回,只要调用触发低2步就可以返回了。 第2步很适合做成异步任务,只要触发了就行,快速给出响应,不让请求卡在接口上,不必等它完整做完。
116 0