Spring Quartz定时器源码 配置文件 多个触发器

1. [图片] 需要的jar包    

2. [代码]spring Quartz定时器源码 配置文件 时间配置     

?
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.job;
 
import java.util.Date;
 
public class TimingJob {
     
     public void executeTask() {
         System.out.println( "当前时间:" + new Date()+ "\n如果还想知道另一种方式,请联系我!" );
     }
}
 
 
 
<?xml version= "1.0" encoding= "UTF-8" ?>
 
<beans xmlns= "http://www.springframework.org/schema/beans"
     xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop"
     xmlns:tx= "http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http: //www.springframework.org/schema/beans
     http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http: //www.springframework.org/schema/tx
     http: //www.springframework.org/schema/tx/spring-tx-2.0.xsd
     http: //www.springframework.org/schema/aop
     http: //www.springframework.org/schema/aop/spring-aop-2.0.xsd">
 
     <bean id= "timingJob" class = "com.job.TimingJob" />
     <!-- 配置一个Job,方法 -->
      <bean id= "jobBean" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
          <property name= "targetObject" >
           <ref bean= "timingJob" />
          </property>
          <property name= "targetMethod" >
           <value>executeTask</value>
          </property>
     </bean>
 
     <!-- 简单的触发器 -->
     <bean id= "simpleTimingJobTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" >
         <property name= "jobDetail" >
             <ref bean= "jobBean" />
         </property>
         <!-- 以毫秒为单位,启动后一分钟触发 -->
         <property name= "startDelay" >
             <value> 60000 </value>
         </property>
         <!-- 每间隔一分钟触发一次 -->
         <property name= "repeatInterval" >
             <value> 60000 </value>
         </property>
     </bean>
 
     <!-- 复杂的触发器 -->
     <bean id= "complexTimingJobTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" >
         <property name= "jobDetail" >
             <ref bean= "jobBean" />
         </property>
         <property name= "cronExpression" >
             <!--每分钟触发一次 -->
             <value> 0 0 / 1 * * * ?</value>
         </property>
     </bean>
 
     <!-- Spring触发工厂 -->
     <bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >
         <property name= "triggers" >
             <list>
                 <ref bean= "simpleTimingJobTrigger" />
                 <!-- ....下面可以继续添加其他触发器 -->
             </list>
         </property>
     </bean>
</beans>
 
<?xml version= "1.0" encoding= "UTF-8" ?>
<web-app version= "2.5"
     xmlns= "http://java.sun.com/xml/ns/javaee"
     xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http: //java.sun.com/xml/ns/javaee
     http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
   <!-- spring加载 -->
   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/classes/applicationContext_*.xml</param-value>
   </context-param>
   
   <!--监听-->
   <listener>
     <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
   </listener>
   
</web-app>

3. [代码]复杂触发器时间配置     

?
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
0 - 59 , - * /
 
0 - 59 , - * /
 
小时 0 - 23 , - * /
 
1 - 31 , - * ? / L W C
 
1 - 12 or JAN-DEC , - * /
 
周几 1 - 7 or SUN-SAT , - * ? / L C #
 
年 (可选字段) empty, 1970 - 2099 , - * /
 
可用值详细分析如下:
 
“*”——字符可以用于所有字段,在“分”字段中设为 "*" 表示 "每一分钟" 的含义。
 
“?”——字符可以用在“日”和“周几”字段. 它用来指定 '不明确的值' . 这在你需要指定这两个字段中的某一个值而不是另外一个的时候会被用到。在后面的例子中可以看到其含义。
 
“-”——字符被用来指定一个值的范围,比如在“小时”字段中设为 "10-12" 表示 "10点到12点"
 
“,”——字符指定数个值。比如在“周几”字段中设为 "MON,WED,FRI" 表示 "the days Monday, Wednesday, and Friday"
 
“/”——字符用来指定一个值的的增加幅度. 比如在“秒”字段中设置为 "0/15" 表示 "第0, 15, 30, 和 45秒" 。而 "5/15" 则表示 "第5, 20, 35, 和 50" . 在 '/' 前加 "*" 字符相当于指定从 0 秒开始. 每个字段都有一系列可以开始或结束的数值。对于“秒”和“分”字段来说,其数值范围为 0 59 ,对于“小时”字段来说其为 0 23 , 对于“日”字段来说为 0 31 , 而对于“月”字段来说为 1 12 "/" 字段仅仅只是帮助你在允许的数值范围内从开始 "第n" 的值。
 
“L”——字符可用在“日”和“周几”这两个字段。它是 "last" 的缩写, 但是在这两个字段中有不同的含义。例如,“日”字段中的 "L" 表示 "一个月中的最后一天" —— 对于一月就是 31 号对于二月来说就是 28 号(非闰年)。而在“周几”字段中, 它简单的表示 "7" or "SAT" ,但是如果在“周几”字段中使用时跟在某个数字之后, 它表示 "该月最后一个星期×" —— 比如 "6L" 表示 "该月最后一个周五" 。当使用 'L' 选项时,指定确定的列表或者范围非常重要,否则你会被结果搞糊涂的。
 
“W”——可用于“日”字段。用来指定历给定日期最近的工作日(周一到周五) 。比如你将“日”字段设为 "15W" ,意为: "离该月15号最近的工作日" 。因此如果 15 号为周六,触发器会在 14 号即周五调用。如果 15 号为周日, 触发器会在 16 号也就是周一触发。如果 15 号为周二,那么当天就会触发。然而如果你将“日”字段设为 "1W" , 而一号又是周六, 触发器会于下周一也就是当月的 3 号触发,因为它不会越过当月的值的范围边界。 'W' 字符只能用于“日”字段的值为单独的一天而不是一系列值的时候。
 
“L”和“W”可以组合用于“日”字段表示为 'LW' ,意为 "该月最后一个工作日"
 
“#”—— 字符可用于“周几”字段。该字符表示“该月第几个周×”,比如 "6#3" 表示该月第三个周五( 6 表示周五而 "#3" 该月第三个)。再比如: "2#1" = 表示该月第一个周一而 "4#5" = 该月第五个周三。注意如果你指定 "#5" 该月没有第五个“周×”,该月是不会触发的。
 
“C”—— 字符可用于“日”和“周几”字段,它是 "calendar" 的缩写。 它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日历, 那它等同于包含全部日历。“日”字段值为 "5C" 表示 "日历中的第一天或者5号以后" ,“周几”字段值为 "1C" 则表示 "日历中的第一天或者周日以后"
 
对于“月份”字段和“周几”字段来说合法的字符都不是大小写敏感的。
 
附表:
"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 触发

4. [文件] 源码 ~ 4MB     下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Quartz框架来实现定时任务。下面是一个简单的配置示例: 1. 添加依赖 在pom.xml中添加Quartz依赖: ```xml <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency> ``` 2. 配置Job和Trigger 在程序中定义一个Job,示例代码如下: ```java @Component public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 定时任务执行的逻辑 } } ``` 然后定义一个Trigger,示例代码如下: ```java @Component public class MyTrigger { @Bean public JobDetail jobDetail() { return JobBuilder.newJob(MyJob.class) .withIdentity("myJob") .storeDurably() .build(); } @Bean public Trigger trigger() { return TriggerBuilder.newTrigger() .forJob(jobDetail()) .withIdentity("myTrigger") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // 每5秒执行一次 .build(); } } ``` 3. 配置Scheduler 创建一个SchedulerFactoryBean,配置其中的Scheduler实例,示例代码如下: ```java @Configuration public class QuartzConfig { @Autowired private MyTrigger myTrigger; @Bean public SchedulerFactoryBean schedulerFactoryBean() { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setTriggers(myTrigger.trigger()); return schedulerFactoryBean; } } ``` 4. 启动应用程序 在应用程序启动时,Scheduler会自动启动,定时任务也会随之开始执行。 注意事项: - 如果需要并发执行Job,可以在Job类上添加注解@DisallowConcurrentExecution。 - 如果需要持久化Job信息,可以配置JobStore,例如使用JDBCJobStore将Job信息存储到数据库中。 - 如果需要动态创建和管理Job和Trigger,可以使用JobDetailFactory和TriggerFactory来创建Job和Trigger,并使用Scheduler来管理它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值