公司网站域名费用怎么交,asp网站转html,网站代码隐蔽代码,网站建设公司需要什么Spring的Scheduled注解实现定时任务
【简介篇】
项目经常会用到定时任务#xff0c;实现定时任务的方式有很多种。在Spring框架中#xff0c;实现定时任务很简单#xff0c;常用的实现方式是使用注解Scheduled。
Scheduled 常用来实现简单的定时任务。例如凌晨1点跑批Scheduled注解实现定时任务
【简介篇】
项目经常会用到定时任务实现定时任务的方式有很多种。在Spring框架中实现定时任务很简单常用的实现方式是使用注解Scheduled。
Scheduled 常用来实现简单的定时任务。例如凌晨1点跑批每10秒查询支付状态等
【实战篇】
SpringBoot项目
1、配置
在spring boot的启动类上加EnableScheduling注解允许支持Scheduled
SpringBootApplication
EnableScheduling
public class Application {public static void main(String[] args) throws Exception {SpringApplication.run(Application.class);}
}2、任务类
Component
public class ScheduleTask {// 每隔5秒执行一次Scheduled(cron 0/5 * * * * ?)public void printSay() {System.out.println(每隔5秒执行一次 new Date());}
}3、结果
Spring项目
1、配置
在spring.xml或applicationContext.xml以自己的配置文件名为准中添加配置
!-- 1、在 xmlns中加入 --
xmlns:taskhttp://www.springframework.org/schema/task!-- 2、 在xsi:schemaLocation中加入 --http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd!— 3、 配置spring扫描注解的路径 --
context:component-scan base-packagecom.it.mytask /!-- 4、 定时任务扫描配置 --
!-- 4.1、 方式一 --
task:executor idexecutor pool-size5 /
task:scheduler idscheduler pool-size10 /
task:annotation-driven executorexecutor schedulerscheduler /!-- 4.2、 方式二--
task:annotation-driven schedulerscheduler modeproxy/
task:scheduler idscheduler pool-size10/例
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:taskhttp://www.springframework.org/schema/taskxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsddefault-lazy-initfalsecontext:annotation-config /!— 配置spring扫描注解的路径 --context:component-scan base-packagecom.it.mytask /!-- 定时任务扫描配置 --!— 开启这个配置spring才能识别Scheduled注解 --task:annotation-driven schedulerscheduler modeproxy/task:scheduler idscheduler pool-size10//beans2、任务类同上
3、结果同上
【原理篇】
Scheduled源码
Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
Retention(RetentionPolicy.RUNTIME)
Documented
Repeatable(Schedules.class)
public interface Scheduled {String cron() default ;String zone() default ;long fixedDelay() default -1L;String fixedDelayString() default ;long fixedRate() default -1L;String fixedRateString() default ;long initialDelay() default -1L;String initialDelayString() default ;
}
案例
package com.task;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;Component
public class ScheduledTask {private Logger logger LoggerFactory.getLogger(ScheduledTask.class);// fixedRate 5000表示每隔5秒Spring scheduling会调用一次该方法不论该方法的执行时间是多少Scheduled(fixedRate 5000)public void task() {logger.info(每隔5秒执行一次);}// fixedDelay 5000表示当方法执行完毕5秒后Spring scheduling会再次调用该方法Scheduled(fixedDelay 5000)public void taskAfter() {logger.info(当方法执行完毕5秒后执行);}// cron */5 * * * * * * 通用的定时任务表达式表示每隔5秒执行一次Scheduled(cron */5 * * * * *)public void taskCron() {logger.info(每隔5秒执行一次);}
}
cron 表达式
1、cron一共有7位但是最后一位是年19702099可以留空所以我们可以写6位,按顺序依次为
秒0~59分钟0~59小时0~23天月1~31但是你需要考虑你月的天数月1~12星期1~7 1SUNMONTUEWEDTHUFRISAT
2、cron的一些特殊符号
(*)星号可以理解为每的意思每秒每分每天每月每年…(?)问号问号只能出现在日期和星期这两个位置表示这个位置的值不确定每天3点执行所以第六位星期的位置我们是不需要关注的就是不确定的值。同时日期和星期是两个相互排斥的元素通过问号来表明不指定值。比如1月10日比如是星期1如果在星期的位置是另指定星期二就前后冲突矛盾了。(-)减号表达一个范围如在小时字段中使用“10-12”则表示从10到12点即10,11,12(,)逗号表达一个列表值如在星期字段中使用“1,2,4”则表示星期一星期二星期四(/)斜杠如x/yx是开始值y是步长比如在第一位秒 0/15就是从0秒开始每15秒最后就是015304560 另*/y等同于0/y
eg.下面列举几个例子供大家来验证
0 0 3 * * ? 每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行与上面作用相同
0 5/10 3 * * ? 每天3点的 5分15分25分35分45分55分这几个时间点执行
0 10 3 ? * 1 每周星期天3点10分 执行注1表示星期天
0 10 3 ? * 1#3 每个月的第三个星期星期天 执行#号只能出现在星期的位置常用示例: 格式: [秒] [分] [小时] [日] [月] [周] [年]
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分每分触发
0 0/5 14 * * ? 每天下午的 2点到2点59分(整点开始每隔5分触发)
0 0/5 14,18 * * ? 每天下午的 18点到18点59分(整点开始每隔5分触发)
0 0-5 14 * * ? 每天下午的 2点到2点05分每分触发
0 10,44 14 ? 3 WED 3月分每周三下午的 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 每月的第三周的星期五开始触发
0 0 12 1/5 * ? 每月的第一个中午开始每隔5天触发一次
0 11 11 11 11 ? 每年的11月11号 11点11分触发(光棍节)