通过这个例子来展示Quartz在Spring中的使用,使用的技术如下:
1.Quartz 2.2.1
2.Spring 4.3.5.RELEASE
3.Maven 3.1

一,实例代码

pom.xml

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.devnp</groupId>
<artifactId>QuartzDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>QuartzDemo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sl4j.version>1.6.6</sl4j.version>
</properties>

<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>

<!-- Quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>

<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${sl4j.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

HelloJob.java

1
2
3
4
5
6
7
8
9
package com.devnp.job;

public class HelloJob{

public void sayHello(){
System.out.println("Hello World.");
}

}

配置:Spring-Quartz.xml

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
<?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-4.3.xsd">

<bean id="helloJobBean" class="com.devnp.job.HelloJob" />

<bean id="helloJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- Task Class -->
<property name="targetObject" ref="helloJobBean" />
<!-- Test Function -->
<property name="targetMethod" value="sayHello" />
</bean>

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

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

</beans>

测试: SpringQuartzDemo.java

1
2
3
4
5
6
7
8
9
10
11
package com.devnp;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzDemo {

@SuppressWarnings("resource")
public static void main(String[] args) {
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}

二,代码下载

SpringQuartzDemo.zip