posts - 4,  comments - 1,  trackbacks - 0
Java默认定时器Timer一般也能满足定时的需求, 但是使用Spring+Quartz来配置定时任务, 更加灵活强大.

例子如下:

一, Maven依赖包
 1        <dependency>
 2            <groupId>org.quartz-scheduler</groupId>
 3            <artifactId>quartz</artifactId>
 4            <version>1.8.0</version>
 5            <type>jar</type>
 6        </dependency>
 7        <dependency>
 8            <groupId>org.springframework</groupId>
 9            <artifactId>spring-aop</artifactId>
10            <version>${spring.version}</version>
11            <type>jar</type>
12            <scope>compile</scope>
13        </dependency>
14        <dependency>
15            <groupId>org.springframework</groupId>
16            <artifactId>spring-beans</artifactId>
17            <version>${spring.version}</version>
18            <type>jar</type>
19            <scope>compile</scope>
20        </dependency>
21        <dependency>
22            <groupId>org.springframework</groupId>
23            <artifactId>spring-context</artifactId>
24            <version>${spring.version}</version>
25            <type>jar</type>
26        </dependency>
27        <dependency>
28            <groupId>org.springframework</groupId>
29            <artifactId>spring-context-support</artifactId>
30            <version>${spring.version}</version>
31            <type>jar</type>
32        </dependency>
33        <dependency>
34            <groupId>org.springframework</groupId>
35            <artifactId>spring-core</artifactId>
36            <version>${spring.version}</version>
37            <type>jar</type>
38        </dependency>
39        <dependency>
40            <groupId>org.springframework</groupId>
41            <artifactId>spring-jdbc</artifactId>
42            <version>${spring.version}</version>
43            <type>jar</type>
44        </dependency>
45        <dependency>
46            <groupId>org.springframework</groupId>
47            <artifactId>spring-tx</artifactId>
48            <version>${spring.version}</version>
49            <type>jar</type>
50        </dependency>
51        <dependency>
52            <groupId>org.springframework</groupId>
53            <artifactId>spring-tx</artifactId>
54            <version>${spring.version}</version>
55            <type>jar</type>
56      </dependency>

二, 配置文件 SpringTest.xml
1<beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://www.springframework.org/schema/beans
4 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5
6 <bean name="ftpManager" class="org.springframework.scheduling.quartz.JobDetailBean">
7 <property name="jobClass" value="com.test.FTPManager" />
8 </bean>
9
10 <!-- Cron Trigger, run every 1 minute -->
11 <bean id="cronTriggerFtpManager" class="org.springframework.scheduling.quartz.CronTriggerBean">
12 <property name="jobDetail" ref="ftpManager" />
13 <property name="cronExpression" value="0 0/1 * * * ?" />
14 </bean>
15
16 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
17 <property name="jobDetails">
18 <list>
19 <ref bean="ftpManager" />
20 </list>
21 </property>
22
23 <property name="triggers">
24 <list>
25 <ref bean="cronTriggerFtpManager" />
26 </list>
27 </property>
28 </bean>
29
30</beans>

三, Java类

1public class FTPManager extends QuartzJobBean
2{
3}

4















posted on 2013-05-23 16:32 大雨大雨 阅读(915) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: