Quartz写个简单的定时任务

spring+maven+Quartz定时任务
1.首先我们在项目中引进Quartz,pom.xml中添加如下包

1 <dependency>
2     <groupId>org.quartz-scheduler</groupId>
3     <artifactId>quartz</artifactId>
4     <version>2.2.1</version>
5 </dependency>
View Code

2.在web.xml中有如下配置

1     <!-- Context ConfigLocation -->
2     <context-param>
3         <param-name>contextConfigLocation</param-name>
4         <param-value>classpath*:/spring-context*.xml</param-value>
5     </context-param>
View Code

3.添加spring-context-quartz.xml配置文件,专门配置定时任务,1.startQuartz总管理类,启动触发器的配置,2.配置触发器及触发器关联的详细任务。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
 5     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
 9         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
10         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
14         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
15         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
16     default-lazy-init="true">
17     
18     <!-- Quartz common config-->
19     
20     <!-- 总管理类,启动触发器的配置, 如果将lazy-init='false'那么容器启动就会执行调度程序 -->  
21     <bean id="startQuartz" lazy-init='false' autowire="no"
22             class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
23         <property name="triggers">  
24             <list>  
25                 <ref bean="runSynchrodataHalfhour" /> 
26             </list>  
27         </property>  
28         <property name="autoStartup" value="true" />
29         <property name="startupDelay" value="30"/>
30     </bean>  
31     <!-- 定时器1: 同步统一权限应用表数据  -->
32     <!-- 定义执行的对象及对象中的方法 -->
33     <bean id="synchrodataOnehour" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
34         <property name="targetObject" ref="Synchrodata" />
35         <property name="targetMethod" value="importData" />
36         <property name="concurrent" value="false" /> <!-- 指是否并行执行 -->
37     </bean>  
38     <!-- 定义触发器的时间 -->
39     <bean id="runSynchrodataHalfhour" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
40         <property name="jobDetail" ref="synchrodataOnehour" />    
41         <property name="cronExpression" value="0 0/30 * * * ?">  </property>
42     </bean> 
43 </beans>
View Code

4.创建任务对象,此处代码逻辑省略,可以写定时从别的数据库同步数据到本地数据库。

 1 import org.springframework.stereotype.Component;
 2 
 3 @Component("Synchrodata")
 4 public class Synchrodata {
 5     
 6     
 7      public void importData() {
 8          //写自己要同步的数据逻辑
 9          System.out.println("导入成功");
10      }
11 }
View Code

5.以上已配置好定时任务,可以把时间调小点,打个断点到任务方法中,debugger启动项目看是否定时任务已配置成功。

6.可通过在线生成Cron表达式的工具:http://cron.qqe2.com/ 来生成自己想要的表达式。下面列举一下常用的时间表达式

 0 * * * * ? 每1分钟触发一次
0 0 * * * ? 每天每1小时触发一次
0 0 10 * * ? 每天10点触发一次
0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发 
0 30 9 1 * ? 每月1号上午9点半
0 15 10 15 * ? 每月15日上午10:15触发

*/5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 5-15 * * ? 每天5-15点整点触发
0 0/3 * * * ? 每三分钟触发一次
0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 
0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 

0 0 12 ? * WED 表示每个星期三中午12点
0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点
0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 
0 15 10 ? * MON-FRI 周一至周五的上午10:15触发

0 0 23 L * ? 每月最后一天23点执行一次
0 15 10 L * ? 每月最后一日的上午10:15触发 
0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 

0 15 10 * * ? 2005 2005年的每天上午10:15触发 
0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 
0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

猜你喜欢

转载自www.cnblogs.com/fzdsy/p/11791125.html