任务框架quartz在项目中的的运用。。

首先下载quatz包,quatz2.0以上的包在spring中用的时候由于版本问题会报错:java.lang.IncompatibleClassChangeError

所以先用quartz-1.8.6.jar 下载地址:点击下载

写一个简单的任务类TestTask.java:

1 package cn.edu.qfnu.main.web.task;
2 
3 public class TestTask {
4     
5      public void test(){
6         System.out.println("测试方法运行成功!!!!!!!!");
7     }
8 }

 写配置文件task.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6        http://www.springframework.org/schema/beans 
 7        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8        http://www.springframework.org/schema/tx 
 9        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
10        http://www.springframework.org/schema/aop 
11        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12        http://www.springframework.org/schema/context 
13        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14 
15     <bean name="quartzScheduler"
16         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
17 
18         <!--必须,QuartzScheduler 延时启动,应用启动后 QuartzScheduler 再启动 -->
19         <property name="startupDelay" value="60" />
20         <!-- 普通触发器 :触发器列表 -->
21         <property name="triggers">
22             <list>
23                 <ref local="testTrigger" />
24             </list>
25         </property>
26     </bean>
27 
28     <!-- 配置执行定时任务的类和方法 -->
29     <bean id="testDetail"
30         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
31         <property name="targetObject">
32             <bean class="cn.edu.qfnu.main.web.task.TestTask"></bean>
33         </property>
34         <property name="targetMethod">
35             <value>test</value>
36         </property>
37     </bean>
38 
39     <!-- 配置触发器 -->
40     <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
41         <property name="jobDetail">
42             <ref bean="testDetail" />
43             <!-- 触发器触发的 执行定时任务的bean -->
44         </property>
45         <property name="cronExpression">
46             <!-- 每天23时 -->  <!-- 定时任务执行的间隔 -->
47             <value>0 20 15 * * ?</value>
48         </property>
49     </bean>
50 </beans>

结果到了15点20分的时候:

 
附上cronExpression的写法:

"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

原文地址:https://www.cnblogs.com/Akishimo/p/3098877.html