SpringBoot项目中的定时任务实现方法

摘要:
03091*在每天下午2:00至2:59之间每分钟触发一次?每月1日凌晨执行。有关详细信息,@Scheduled注释了每个参数的详细信息。默认情况下,计划任务的cron表达式的详细说明是单线程的。任务的执行时间受前一任务的执行速度的影响。多线程计时任务:向启动类添加@EnableAsync注释,并向相应的方法添加@Async注释@EnableAsync@EnableScheduling@SpringBootApplicationpublicclassApplication{publicstaticvoidmain{SpringApplication.run;}}@Async@Scheduled//公共void first()抛出InterruptedException{System.out.println;System.out.print ln();Thread.sleep;}每1秒@Async@Scheduled//PublicvoidSecond()throwsInterruptedException{System.out.println;System.out.print();Thread.sleep;}有关更多信息,请参阅:SpringBoot中计划任务的详细说明。如何使用ThreadPoolTaskScheduler类@AutowiredprivateThreadPoolTaskSchedulertradingScheduler//任务队列privateConcurrentHashMap<Runnable,ScheduledFuture<?˃˃scheduledFutureMap=newConcurrentHashMap();PublicvoidscheduleSyncTask(){//Stringcron=“00/5***?

@Scheduled注解

开启条件

在启动类上加上 @EnableScheduling 注解
示例:

@EnableScheduling
@SpringBootApplication
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}

使用方式

作用在方法
可选参数:(常用)cron,fixedDelay,示例:

@Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
@Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行

cron表达式

顺序
范围0-590-590-231-30(31)1-121-7
字符, - * /, - * /, - * /, - * / ? L W C, - * /, - * / L # C

字符含义

*:代表所有可能的值
-:指定范围
,:列出枚举  例如在分钟里,"5,15"表示5分钟和20分钟触发
/:指定增量  例如在分钟里,"3/15"表示从3分钟开始,没隔15分钟执行一次
?:表示没有具体的值,使用?要注意冲突
L:表示last,例如星期中表示7或SAT,月份中表示最后一天31或30,6L表示这个月倒数第6天,FRIL表示这个月的最后一个星期五
W:只能用在月份中,表示最接近指定天的工作日
#:只能用在星期中,表示这个月的第几个周几,例如6#3表示这个月的第3个周五

cron示例

0 * * * * ? 每1分钟触发一次
0 0 * * * ? 每天每1小时触发一次
0 0 10 * * ? 每天10点触发一次
0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
0 30 9 1 * ? 每月1号上午9点半
0 15 10 15 * ? 每月15日上午10:15触发
*/5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 5-15 * * ? 每天5-15点整点触发
0 0/3 * * * ? 每三分钟触发一次
0 0 0 1 * ?  每月1号凌晨执行一次

更多参考@Scheduled注解各参数详解定时任务cron表达式详解

注意事项

默认为单线程,,任务的执行时机会受上一个任务执行时间的影响。
多线程定时任务:

在启动类上加上 @EnableAsync 注解,在对应方法上加上 @Async注解

@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
 @Async
 @Scheduled(fixedDelay = 1000) //间隔1秒 
 public void first() throws InterruptedException { 
 System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
线程 : " + Thread.currentThread().getName()); System.out.println(); Thread.sleep(1000 * 10);
 }
 
 @Async
 @Scheduled(fixedDelay = 1000) //间隔1秒 
 public void Second() throws InterruptedException { 
 System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
线程 : " + Thread.currentThread().getName()); System.out.println(); Thread.sleep(1000 * 10);
 }

更多参考玩转SpringBoot之定时任务详解


ThreadPoolTaskScheduler类

使用方式

@Autowired
private ThreadPoolTaskScheduler tradingScheduler;

//任务队列
private ConcurrentHashMap<Runnable, ScheduledFuture<?>> scheduledFutureMap = new ConcurrentHashMap<>();
public void scheduleSyncTask() {
        //每5分钟一次
        String cron = "0 0/5 * * * ?";
        ScheduledFuture<?> future = tradingScheduler.schedule(() -> { /**Runnable线程run()方法代码**/}, new CronTrigger(cron));
        scheduledFutureMap.put(runner, future);
        log.info("{} 任务启动成功",LocalDateTime.now());
    }

RestFul接口来管理任务
参考 Spring动态管理定时任务——ThreadPoolTaskScheduler

注意事项

默认定时线程池线程为 1 ,即 private volatile int poolSize = 1; ,因此多个定时任务串行执行
修改poolSize

@Bean(name = "sync")
public ThreadPoolTaskScheduler threadPoolTaskScheduler4Sync() {
    ThreadPoolTaskScheduler syncScheduler = new ThreadPoolTaskScheduler();
    syncScheduler.setPoolSize(5);
    syncScheduler.setThreadGroupName("syncTg");
    syncScheduler.setThreadNamePrefix("syncThread-");
    return syncScheduler;
}
@Resource(name = "sync")
private ThreadPoolTaskScheduler threadPoolTaskScheduler;

更多参考
ThreadPoolTaskScheduler注意事项之线程池大小poolSize以及通过JMX修改


Quartz框架

简介

参考 Quartz框架介绍

SpringBoot整合Quartz

参考 SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本

免责声明:文章转载自《SpringBoot项目中的定时任务实现方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇NVM安装Node.js64位系统未注册"MSDAORA.1"提供程序下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

微信小程序 View:flex 布局

微信小程序View支持两种布局方式:Block和Flex所有View默认都是block要使用flex布局的话需要显式的声明:display:flex;下面就来介绍下微信小程序的Flex布局先做一个简单的demo123加上背景色能看的更清楚些.main{width:100%;background-color:antiquewhite;}.item{height...

Datax3.0使用说明

任务是DataX作业的最小单位。每个任务负责一些数据的同步。DataX的调度决策思想是:-DataXJob根据数据库和表划分为100个任务。...

将txt、csv等文本文件导入Hive

将txt、csv等文本文件导入Hive目录将txt、csv等文本文件导入Hive00.数据在虚拟机外01.启动hadoop、hdfs02.将文件放置在hdfs目录下03.登录hive并进入指定数据库04.根据文件创建表05.执行导入语句00.数据在虚拟机外如果数据在虚拟机内,请跳过此步,直接执行接下来的操作。...

MeteoInfo-Java解析与绘图教程(一)

MeteoInfo-Java解析与绘图教程(一)已经进入开发行业很多年了,这两年一直从事气象开发行业,为此对气象绘图有了新的见解像色斑图与卫星图一直都有python去绘制,在偶然的情况下,我接触到了meteoInfo,在对其使用过程中,也可以做到用java绘制格点散点图,色斑图,等值图,卫星图,风场图所以趁这个机会我开始记录自己的探索过程,方便你我他对于绘图...

GERBER文件

GERBER文件GERBER文件是一种国际标准的光绘格式文件,它包含RS-274-D和RS-274-X两种格式,其中RS-274-D称为基本GERBER格式,并要同时附带D码文件才能完整描述一张图形;RS-274-X称为扩展GERBER格式,它本身包含有D码信息。或GERBER描述是防焊层,并且描述之图形主要是防焊部分。若您自己将PCB文件转换成GERBER...

DB2锁表或超时解决方案

命令如下:db2"forceapplication"4、使用命令listapplication查看是否已经断开了哪些进行了死锁的进程。...