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