@Scheduled执⾏定时任务与cron表达式1 配置⽂件形式执⾏定时任务
1 1.X 版本与spring结合使⽤实例
1.1 常⽤maven管理 l⽂件
1<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
2    xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd">
3<modelVersion>4.0.0</modelVersion>
4<groupId>com.kevin.quartz</groupId>
5<artifactId>quartz</artifactId>
6<packaging>war</packaging>
7<version>1.0</version>
8<name>quartz1.x</name>
9<url></url>
10
11<properties>
12<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13<spring.version>3.2.14.RELEASE</spring.version>
14</properties>
15
16<build>
17<plugins>
18<plugin>
19<groupId>org.apache.maven.plugins</groupId>
20<artifactId>maven-war-plugin</artifactId>
21<configuration>
22<attachClasses>false</attachClasses>
23</configuration>
24</plugin>
25
26<!-- ⽣成源代码的jar包 -->
27<plugin>
28<groupId>org.apache.maven.plugins</groupId>
29<artifactId>maven-source-plugin</artifactId>
30<executions>
31<execution>
32<id>attach-sources</id>
33<goals>
34<goal>jar-no-fork</goal>
35</goals>
36</execution>
37</executions>
38</plugin>
39
40<plugin>
41<groupId>org.apache.maven.plugins</groupId>
42<artifactId>maven-compiler-plugin</artifactId>
43<configuration>
44<source>1.6</source>
45<target>1.6</target>
46</configuration>
47</plugin>
48</plugins>
49<!-- ⽣成jar或war包的名字 -->
50<finalName>${artifactId}</finalName>
51</build>
52
53<dependencies>
54
55<dependency>
56<groupId>javax.servlet</groupId>
57<artifactId>servlet-api</artifactId>
58<version>2.5</version>
59<scope>provided</scope>
60</dependency>
61<dependency>
62<groupId>junit</groupId>
63<artifactId>junit</artifactId>
64<version>4.10</version>
65<scope>test</scope>
66</dependency>
67
68<dependency>
69<groupId>log4j</groupId>
70<artifactId>log4j</artifactId>
73
74<!-- spring framework start -->
75<dependency>
76<groupId>org.springframework</groupId>
77<artifactId>spring-core</artifactId>
78<version>${spring.version}</version>
79</dependency>
80<dependency>
81<groupId>org.springframework</groupId>
82<artifactId>spring-context</artifactId>
83<version>${spring.version}</version>
84</dependency>
85<dependency>
86<groupId>org.springframework</groupId>
87<artifactId>spring-beans</artifactId>
88<version>${spring.version}</version>
89</dependency>
90<dependency>
91<groupId>org.springframework</groupId>
92<artifactId>spring-web</artifactId>
93<version>${spring.version}</version>
94</dependency>
95<dependency>
96<groupId>org.springframework</groupId>
97<artifactId>spring-webmvc</artifactId>
98<version>${spring.version}</version>
99</dependency>
100<dependency>
101<groupId>org.springframework</groupId> 102<artifactId>spring-aop</artifactId>
103<version>${spring.version}</version>
104</dependency>
105<dependency>
106<groupId>org.springframework</groupId> 107<artifactId>spring-jdbc</artifactId>
108<version>${spring.version}</version>
109</dependency>
110<dependency>
111<groupId>org.springframework</groupId> 112<artifactId>spring-tx</artifactId>
113<version>${spring.version}</version>
114</dependency>
115<dependency>
116<groupId>org.springframework</groupId> 117<artifactId>spring-orm</artifactId>
118<version>${spring.version}</version>
119</dependency>
120<dependency>
121<groupId>org.springframework</groupId> 122<artifactId>spring-context-support</artifactId> 123<version>${spring.version}</version>
124</dependency>
125<dependency>
126<groupId>org.springframework</groupId> 127<artifactId>spring-expression</artifactId> 128<version>${spring.version}</version>
129</dependency>
130<dependency>
131<groupId>org.springframework</groupId> 132<artifactId>spring-test</artifactId>
133<version>${spring.version}</version>
134</dependency>
135<dependency>
136<groupId>org.springframework</groupId> 137<artifactId>spring-aspects</artifactId>
138<version>${spring.version}</version>
139</dependency>
140<dependency>
141<groupId>aopalliance</groupId>
142<artifactId>aopalliance</artifactId>
143<version>1.0</version>
144</dependency>
145<dependency>
146<groupId>org.aspectj</groupId>
147<artifactId>aspectjweaver</artifactId>
148<version>1.8.6</version>
149</dependency>
150<!-- spring framework end -->
151
152<dependency>
153<groupId>org.quartz-scheduler</groupId> 154<artifactId>quartz</artifactId>
157</dependencies>
158
159</project>
View Code
2 定义我的⼯作类
package com.kevin.quartz.job;
import java.util.Date;
public class MyJob {
public void work() {
System.out.println("current datetime: " + new Date().toString());
}
}
3 Spring配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi=/2001/XMLSchema-instance xmlns:p="/schema/p"    xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd">
<!-- ⼯作的bean -->
<bean id="myJob" class="com.kevin.quartz.job.MyJob" />
<!-- job的配置开始 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>work</value>
</property>
<!--将并发设置为false-->
<property name="concurrent" value="false" />
</bean>
<!-- job的配置结束 -->
<!-- 调度的配置开始 -->
<bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value> <!— 每1秒执⾏⼀次 -->
</property>
</bean>
<!-- 调度的配置结束 -->
<!-- 启动触发器的配置开始 -->
<!--  总管理类如果将lazy-init='false'那么容器启动就会执⾏调度程序  -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--作业调度器,list下可加⼊其他的调度器-->
<ref bean="crontestJobTrigger" />
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->
</beans>
4 Web.Xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/xml/ns/javaee/web-app_3_0.xsd">
<!-- 說明:Spring的配置⽂件設置必須在啟動Spring Bean⼯廠監聽之前,否則會報錯-->
<!-- Spring配置⽂件開始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/l</param-value>
</context-param>
<!-- Spring配置⽂件結束 -->
<!-- 启动 Spring Bean ⼯⼚监听開始 -->
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<!-- 启动 Spring Bean ⼯⼚监听結束 -->
</web-app>
5 运⾏及效果
2 2.X与spring结合使⽤案例
1 maven管理项⽬ l
1<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
2    xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd">
3<modelVersion>4.0.0</modelVersion>
4<groupId>com.kevin.quartz</groupId>
5<artifactId>example1</artifactId>
6<packaging>jar</packaging>
7<version>1.0</version>
8
9<name>example1</name>
10<url></url>
11
12<properties>
13<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14<spring.version>4.0.0.RELEASE</spring.version>
15</properties>
16
17<build>
18<plugins>
19<plugin>
20<groupId>org.apache.maven.plugins</groupId>
21<artifactId>maven-compiler-plugin</artifactId>
22<configuration>
23<source>1.6</source>
24<target>1.6</target>
25</configuration>
26</plugin>
27<!-- ⽣成源代码的jar包 -->
28<plugin>
29<groupId>org.apache.maven.plugins</groupId>
30<artifactId>maven-source-plugin</artifactId>
31<executions>
32<execution>
33<id>attach-sources</id>
34<goals>
35<goal>jar-no-fork</goal>
36</goals>
37</execution>
38</executions>
39</plugin>
40</plugins>
41<finalName>${artifactId}</finalName>
42</build>
43
44<dependencies>
45<dependency>
46<groupId>junit</groupId>
47<artifactId>junit</artifactId>
48<version>4.10</version>
49<scope>test</scope>
50</dependency>
51
52<dependency>
53<groupId>log4j</groupId>
54<artifactId>log4j</artifactId>
55<version>1.2.16</version>
56</dependency>
57
58<!-- spring framework start -->
59<dependency>
60<groupId>org.springframework</groupId>
61<artifactId>spring-core</artifactId>
62<version>${spring.version}</version>
63</dependency>
64<dependency>
65<groupId>org.springframework</groupId>
66<artifactId>spring-context</artifactId>
67<version>${spring.version}</version>
68</dependency>
69<dependency>
70<groupId>org.springframework</groupId>
71<artifactId>spring-beans</artifactId>
72<version>${spring.version}</version>
73</dependency>
74<dependency>
75<groupId>org.springframework</groupId>
76<artifactId>spring-web</artifactId>
77<version>${spring.version}</version>
78</dependency>
79<dependency>
80<groupId>org.springframework</groupId>
81<artifactId>spring-webmvc</artifactId>
82<version>${spring.version}</version>
83</dependency>
84<dependency>
85<groupId>org.springframework</groupId>
86<artifactId>spring-aop</artifactId>
87<version>${spring.version}</version>
88</dependency>
89<dependency>
90<groupId>org.springframework</groupId>
91<artifactId>spring-jdbc</artifactId>
92<version>${spring.version}</version>
93</dependency>
94<dependency>
95<groupId>org.springframework</groupId>
96<artifactId>spring-tx</artifactId>
97<version>${spring.version}</version>
98</dependency>
99<dependency>
100<groupId>org.springframework</groupId> 101<artifactId>spring-orm</artifactId>
102<version>${spring.version}</version>
103</dependency>
104<dependency>
105<groupId>org.springframework</groupId> 106<artifactId>spring-context-support</artifactId> 107<version>${spring.version}</version>
spring framework jar包108</dependency>
109<dependency>
110<groupId>org.springframework</groupId> 111<artifactId>spring-expression</artifactId> 112<version>${spring.version}</version>
113</dependency>
114<dependency>
115<groupId>org.springframework</groupId> 116<artifactId>spring-test</artifactId>
117<version>${spring.version}</version>
118</dependency>
119<dependency>
120<groupId>org.springframework</groupId> 121<artifactId>spring-aspects</artifactId>
122<version>${spring.version}</version>
123</dependency>
124<dependency>
125<groupId>aopalliance</groupId>
126<artifactId>aopalliance</artifactId>
127<version>1.0</version>
128</dependency>
129<dependency>
130<groupId>org.aspectj</groupId>
131<artifactId>aspectjweaver</artifactId>
132<version>1.8.6</version>
133</dependency>
134<!-- spring framework end -->

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。