Spring定时器使用和配置

spring定时器

只需要在spring配置文件中加上三段配置,即可完成定时器功能,代码示例:

<bean id="doInsertAliAvReminds" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   

<property name="targetObject">   

     <ref bean="aliavRemindService" />  

    </property>   

    <property name="targetMethod">   

        <value>insertReminds</value>

    </property>   

</bean>

 

<bean id="doInsertAliAvRemindsTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail">

<ref bean="doInsertAliAvReminds" />

</property>

<property name="cronExpression">

<value>0 30 16 * * ?</value>

</property>

</bean>

 

<bean id="doInsertAliAvRemindsTaskTriggerTaskTriggerscheduler" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   

    <property name="triggers">   

        <list>   

         <ref local="doInsertAliAvRemindsTaskTrigger" />  

        </list>   

    </property>   

</bean>

 

 

另外,从网上找了一份比较全的spring定时器时间配置:

这是时间的设置规则 
org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpression属性。 
一个cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素。从左至右,这些元素的定义如下: 

1.秒(0–59) 
2.分钟(0–59) 
3.小时(0–23) 
4.月份中的日期(1–31) 
5.月份(1–12JAN–DEC) 
6.星期中的日期(1–7SUN–SAT) 
7.年份(1970–2099) 
0 0 10,14,16 * * ? 
每天上午10,下午2点和下午4点 
0 0,15,30,45 * 1-10 * ? 
每月前10天每隔15分钟 
30 0 0 1 1 ? 2012 
201211日午夜过30秒时 
0 0 8-5 ? * MON-FRI 
每个工作日的工作时间 
各个时间可用值如下: 

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秒开始.每个字段都有一系列可以开始或结束的数值。对于字段来说,其数值范围为059,对于小时字段来说其为023,对于字段来说为031,而对于字段来说为112"/"字段仅仅只是帮助你在允许的数值范围内从开始"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 * * ?"每天中午十二点触发 
"0 15 10 ? * *"每天早上1015触发 
"0 15 10 * * ?"每天早上1015触发 
"0 15 10 * * ? *"每天早上1015触发 
"0 15 10 * * ? 2005" 2005年的每天早上1015触发 
"0 * 14 * * ?"每天从下午2点开始到259分每分钟一次触发 
"0 0/5 14 * * ?"每天从下午2点开始到255分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"每天的下午2点至2556点至655分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"每天14:0014:05每分钟一次触发 
"0 10,44 14 ? 3 WED"三月的每周三的14101444触发 
"0 15 10 ? * MON-FRI"每个周一、周二、周三、周四、周五的1015触发 
"0 15 10 15 * ?"每月15号的1015触发 
"0 15 10 L * ?"每月的最后一天的1015触发 
"0 15 10 ? * 6L"每月最后一个周五的1015 

posted @   silentmuh  阅读(436)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
Live2D
欢迎阅读『Spring定时器使用和配置』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Welcome Home - Radical Face
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Ben P. Cooper

作曲 : Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

Was never much but we've made the most

Welcome home

Ships are launching from my chest

Some have names but most do not

If you find one,please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

All my nightmares escaped my head

Bar the door, please don't let them in

You were never supposed to leave

Now my head's splitting at the seams

And I don't know if I can

Here, beneath my lungs

I feel your thumbs press into my skin again

点击右上角即可分享
微信分享提示