-
java 定时任务之一 @Scheduled注解(第一种方法)
2017-12-12 22:09:22使用spring @Scheduled注解执行定时任务: 步骤: 1.xmlns 添加: http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd xmlns:task=...(本文仅供参考)
使用spring @Scheduled注解执行定时任务:
步骤:
1.xmlns 添加:
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd xmlns:task="http://www.springframework.org/schema/task"
2.注入:
<task:annotation-driven/>
3.注解写在实现类的方法上,实现类上要有组件的注解@Component
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
运行即可!!!
关于Cron表达式介绍cronExpression定义时间规则,Cron表达式由6或7个空格分隔的时间字段组成:秒 分钟 小时 日期 月份 星期 年(可选)
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 , - * / 星期 1-7 , - * ? / L C # 年 1970-2099 , - * /
“*”字符被用来指定所有的值。
如:
"*":字符在分钟的字段域里表示“每分钟”。
“?”:字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号来表明不想设置那个字段。“-”:字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
“,”:字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”。
“/”:字符用于指定增量。
如:“0/15”在秒域意思是每分钟的0,15,30和45秒。
“5/15”在分钟域表示每小时的5,20,35和50。
符号“*”在“/”前面(如:*/10)等价于0在“/”前面(如:0/10)。
记住一条本质:表达式的每个数值域都是一个有最大值和最小值的集合,如:
秒域和分钟域的集合是0-59,日期域是1-31,月份域是1-12。字符“/”可以帮助你在每个字符域中取相应的数值。如:“7/6”在月份域的时候只 有当7月的时候才会触发,并不是表示每个6月。
L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of- month域中表示一个月的最后一天。如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示 一个月的最后几天,例如‘6L’就表示一个月的最后一个星期五。
字符“W”只允许日期域出现。这个字符用于指定日期的最近工作日。例如:如果你在日期域中写 “15W”,表示:这个月15号最近的工作日。所以,如果15号是周六,则任务会在14号触发。如果15好是周日,则任务会在周一也就是16号触发。如果 是在日期域填写“1W”即使1号是周六,那么任务也只会在下周一,也就是3号触发,“W”字符指定的最近工作日是不能够跨月份的。字符“W”只能配合一个 单独的数值使用,不能够是一个数字段,如:1-15W是错误的。
“L”和“W”可以在日期域中联合使用,LW表示这个月最后一周的工作日。
字符“#”只允许在星期域中出现。这个字符用于指定本月的某某天。例如:“6#3”表示本月第三周的星期五(6表示星期五,3表示第三周)。“2#1”表示本月第一周的星期一。“4#5”表示第五周的星期三。
字符“C”允许在日期域和星期域出现。这个字符依靠一个指定的“日历”。也就是说这个表达式的值依赖于相关的“日历”的计算结果,如果没有“日历” 关联,则等价于所有包含的“日历”。如:日期域是“5C”表示关联“日历”中第一天,或者这个月开始的第一天的后5天。星期域是“1C”表示关联“日历” 中第一天,或者星期的第一天的后1天,也就是周日的后一天(周一)。
例子如下:
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"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触发关于Cron表达式的介绍来源:
http://blog.csdn.net/supingemail/article/details/22274279
表达式网站生成:
-
scheduled
2020-08-05 21:22:33package com.qinjingcao.finance.common; import ... ... import org.slf4j.Logger; import org.slf4j.LoggerFactory;...import org.springframework.beans.factory.annotation.Autowired;...impackage com.qinjingcao.finance.common; import com.qinjingcao.finance.entity.Loan; import com.qinjingcao.finance.service.LoanService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; /** * 定时任务 */ @Configuration @EnableScheduling public class Scheduled { private Logger log = LoggerFactory.getLogger(Scheduled.class); @Autowired private LoanService loanService; @org.springframework.scheduling.annotation.Scheduled(cron = "0 0 23 * * ?") public void test() throws Exception{ List<Loan> list = loanService.selectAllExamedLoan(); for (Loan loan : list) { Date loantime = loan.getLoantime(); Integer term = loan.getTerm(); Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String loantimes = format.format(loantime); String currentDate = format.format(date); System.out.println((date.getTime() - loantime.getTime()) / (24 * 60 * 60 * 1000) + "天"); if((date.getTime() - loantime.getTime()) / (24 * 60 * 60 * 1000)>term){ loan.setLoanstatus(1); Integer result = loanService.updateLoan(loan); log.info(loan.getUser().getUsername()+"逾期了"); } } log.info("定时任务执行完成->->"); } }
-
spring定时任务详解(@Scheduled注解)
2016-07-07 17:09:36在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" ...在springMVC里使用spring的定时任务非常的简单,如下:
(一)在xml里加入task的命名空间
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
(二)启用注解驱动的定时任务
<task:annotation-driven scheduler="myScheduler"/>
(三)配置定时任务的线程池
推荐配置线程池,若不配置多任务下会有问题。后面会详细说明单线程的问题。
<task:scheduler id="myScheduler" pool-size="5"/>
(四)写我们的定时任务
@Scheduled注解为定时任务,cron表达式里写执行的时机
package com.mvc.task.impl; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.concurrent.TimeUnit; import org.joda.time.DateTime; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.mvc.task.IATask; @Component public class ATask implements IATask{ @Scheduled(cron="0/10 * * * * ? ") //每10秒执行一次 @Override public void aTask(){ try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(DateTime.now().toDate())+"*********A任务每10秒执行一次进入测试"); } }
package com.mvc.task.impl; import java.text.DateFormat; import java.text.SimpleDateFormat; import org.joda.time.DateTime; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.mvc.task.IBTask; @Component public class BTask implements IBTask{ @Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次 @Override public void bTask(){ DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(DateTime.now().toDate())+"*********B任务每5秒执行一次进入测试"); } }
spring的定时任务默认是单线程,多个任务执行起来时间会有问题(B任务会因为A任务执行起来需要20S而被延后20S执行),如下图所示:
当我们配置了线程池后再来看结果(多线程下,B任务再也不会因为A任务执行起来要20S而被延后了):cron表达式详解:
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
按顺序依次为
1 秒(0~59)
2 分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"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触发
有些子表达式能包含一些范围或列表
例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
“*”字符代表所有可能的值
“/”字符用来指定数值的增量
例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样
“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值
当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写
如果在“L”前有具体的内容,它就具有其他的含义了。例如:“6L”表示这个月的倒数第6天
注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题
W 字符代表着平日(Mon-Fri),并且仅能用于日域中。它用来指定离指定日的最近的一个平日。大部分的商业处理都是基于工作周的,所以 W 字符可能是非常重要的。
例如,日域中的 15W 意味着 "离该月15号的最近一个平日。" 假如15号是星期六,那么 trigger 会在14号(星期五)触发,因为星期四比星期一离15号更近。
C:代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * / -
使用spring @Scheduled注解执行定时任务、
2012-07-14 09:31:34以前框架使用quartz框架执行定时调度问题、老大说这配置太麻烦、每个调度都需要多加在spring的配置中、能不能减少配置的量从而提高开发效率、最近看了看spring的 scheduled的使用注解的方式进行调度、感觉很方便、...以前框架使用quartz框架执行定时调度问题、
老大说这配置太麻烦、每个调度都需要多加在spring的配置中、
能不能减少配置的量从而提高开发效率、
最近看了看spring的 scheduled的使用注解的方式进行调度、
感觉很方便、起码配置的东西少了很多、
所以留下来以备忘了、
首先要配置我们的spring.xml
xmlns 多加下面的内容、
xmlns:task="http://www.springframework.org/schema/task"
然后xsi:schemaLocation多加下面的内容、
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
最后是我们的task任务扫描注解<task:annotation-driven/>
我的配置扫描位置是:<context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:component-scan base-package="com.test"/>
扫描的是com.test这样的包下的内容、下面需要接口和实现(我的这几个java文件都是com.test的包下的、)
public interface IMyTestService { public void myTest(); }
@Component //import org.springframework.stereotype.Component; public class MyTestServiceImpl implements IMyTestService { @Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次 @Override public void myTest(){ System.out.println("进入测试"); } }
执行后控制台就会打印出 进入测试 了需要注意的几点:
1、spring的@Scheduled注解 需要写在实现上、
2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)
3、实现类上要有组件的注解@Component
剩下的就是corn表达式了、具体使用以及参数请百度google、
下面只例出几个式子
CRON表达式 含义
"0 0 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分每分钟一次触发
"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发 -
Scheduled gain
2020-12-02 00:35:08In the X15 aircraft XML file, there is a scheduled gain. Do you know how we can obtain a scheduled gain for an aircraft in JSBSim? I know it is possible to design and implement gain-scheduled ... -
Scheduled tasks
2020-12-09 00:27:50<p>There exists a piece of logic within <strong>DADI/publish</strong> which stores scheduled document changes (publish, unpublish etc) and makes requests to API at the appropriate time. <p>In order ... -
Scheduled events
2020-12-26 19:21:10Added missing class for Cloud Watch Events - Scheduled Events. https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-scheduled-event</p> <p>By submitting this pull request, I ... -
Scheduled Posts
2020-12-09 11:02:03<p>To avoid this, I think it would be a good idea to implement a function which gives every user the opportunity to create scheduled posts and publish them at a time determined by the user <p>The ... -
Scheduled downtime
2020-11-24 23:02:49<div><p>Add possibility to create stash for clients/checks in order to permit a scheduled downtime.</p><p>该提问来源于开源项目:sensu/uchiwa</p></div> -
Scheduled workers
2020-11-23 10:33:32<div><p>Was this intended to work with workers that need to be scheduled?</p><p>该提问来源于开源项目:mhenrixon/sidekiq-unique-jobs</p></div> -
Scheduled reboot
2020-12-06 13:06:06<div><p>Sometimes, ... Can you add settings for scheduled reboot? where we can choose time or rebooting daily, weekly or monthly.</p><p>该提问来源于开源项目:oznu/homebridge-config-ui-x</p></div> -
@Scheduled
2020-08-03 16:13:32每隔5秒执行一次: @Scheduled(cron = "*/5 * * * * ?") 每隔1分钟执行一次: @Scheduled(cron = "0 */1 * * * ?") 每天23点执行一次: @Scheduled(cron = "0 0 23 * * ?") 每天凌晨1点执行一次: @Scheduled(cron =... -
spring的定时任务@Scheduled简单实用
2019-05-05 23:05:41方式一:使用注解 @Component @EnableScheduling//可以在启动类上注解也可以在当前文件 ... @Scheduled(cron = "0/10 * * * * ?") public void runfirst(){ System.out.println("********first job is ... -
Support scheduled tasks
2020-11-29 13:48:57<div><p>I would like to manage the scheduled task with <code>ecs-cli</code>. For example: <ul><li><code>ecs-cli scheduled create "command"</code>: Override with the specified command and ... -
Disable existing Scheduled Task with win_scheduled_task
2020-11-23 10:10:26<p>trying to use win_scheduled_task to disable an existing scheduled task, using; <p>win_scheduled_task: name="ServerManager" enable=no state=present returns, <p>"Missing ... -
Show when scheduled jobs are scheduled to be run.
2020-12-02 10:15:50<div><p>It'd be super helpful and handy to show when a job is scheduled to be run whenever looking at a scheduled job.</p><p>该提问来源于开源项目:seomoz/qless</p></div> -
have_scheduled
2020-12-02 09:55:35<div><p>I cannot make the have_scheduled matchers work. Test case: <pre><code> reminder: cron: 0 23 * * 1-5 class: MyClass queue: my_quque description: "" </code></pre> <p>MyClass.perform... -
浅谈Scheduled
2020-05-09 15:21:16@Scheduled(cron = "0 0 0 * * ?") 依次为:秒、分、时、日、月、周、年(可选) 秒(0~59) 分钟(0~59) 小时(0~23) 日(0~31) 月(0~11) 星期(1~7 1为SUN-依次为SUN,MON,TUE,WED,THU,FRI,SAT) “*... -
@Scheduled不执行的原因
2016-10-18 16:17:041. 今天用@Schedule做了一个定时任务,希望凌晨1点执行,代码如下@... @Scheduled(cron = "0 0 1 * * ?") public void parseMongodbDataToJson() { } } 第二天来公司了,发现根本没有执行。然后开始查找问题 2. 首先 -
spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法
2020-08-28 19:57:37主要介绍了spring-boot通过@Scheduled配置定时任务,文中还给大家介绍了springboot 定时任务@Scheduled注解的方法,需要的朋友可以参考下 -
Remove scheduled downlink
2020-12-28 09:24:30<p>There is a way to remove a scheduled downlink from queue? Our devices use OTAA and after reset cnt goes to 0, is there a way to remove previous scheduled downlink? to avoid twice received message. ... -
implement scheduledtasks
2020-12-09 02:24:50<p>Implement Scheduled Tasks as a separate abstraction from TaskBase because of timezones, and time muck. Use a cron to back all Scheduled Tasks so we can work on queueing/managing one time format ... -
Delayed scheduled jobs
2020-11-29 04:16:21<div><p>Is there a method to start scheduled jobs after a delay ? If I'm starting a lot of similar jobs at the same time, I want to even out the load by staggering when they start. <p>One ... -
SpringBoot Scheduled
2018-01-24 15:07:08在Spring Boot中编写定时任务是非常简单的事,直接使用Scheduled注解就可以完成复杂的定时任务设置.下面是一个10分钟执行定时任务的示例: @Scheduled(cron = "0 0/10 * * * ?") private void schedulerEmptyMsg() ... -
Creating scheduled WebJobs
2021-01-07 05:22:50<p>However, using the Azure Portal GUI, scheduled jobs can also be added manually. However, they require a script/program to be manually uploaded (in .zip format), which is inconvenient given that you... -
Scheduled commands improvements
2020-12-09 10:05:17<ul><li>Removed unnecessary flag for fields</li><li>Now Scheduled Lamdba just gets created when there are scheduled commands defined.</li></ul> <h2>Checks <ul><li>[x] Project Builds</li><li>[x] ...
-
ProBuilder快速原型开发技术
-
阿里云云计算ACP考试必备教程
-
全连接层(线性层)-Linear
-
Samsung Presents Galaxy S21 5G, S21+ 5G and S21 Ultra 5G
-
算法导论(基础知识)——编程大牛的必经之路
-
电商设计专业思维
-
逻辑-Python核心编程.pdf
-
静态反调试技术(3)
-
【数据分析-随到随学】Spark理论及实战
-
万宝盛华大中华推出的天天U才好用吗?
-
ISO15118-5
-
Netron-Setup-4.7.3.exe
-
通过移动 联通API数据获取加盟意向客户
-
商业的本质——杰克·韦尔奇著
-
使用 Sealos + Longhorn 部署 KubeSphere v3.0.0
-
基于多源数据融合的电力故障事件识别及预控系统
-
第1章 Java入门基础及环境搭建【java编程进阶】
-
数据类型转换、运算符、方法入门
-
Java web项目创建笔记26 之《整合kettle》
-
微信支付2021系列之付款码支付一学就会java版