一、在java开发领域,目前可以通过以下几种方式进行定时任务
1、单机部署模式
timer:jdk中自带的一个定时调度类,可以简单的实现按某一频度进行任务执行。提供的功能比较单一,无法实现复杂的调度任务。
scheduledexecutorservice:也是jdk自带的一个基于线程池设计的定时任务类。其每个调度任务都会分配到线程池中的一个线程执行,所以其任务是并发执行的,互不影响。
spring task:spring提供的一个任务调度工具,支持注解和配置文件形式,支持cron表达式,使用简单但功能强大。
quartz:一款功能强大的任务调度器,可以实现较为复杂的调度功能,如每月一号执行、每天凌晨执行、每周五执行等等,还支持分布式调度,就是配置稍显复杂。
2、分布式集群模式(不多介绍,简单提一下)
问题:
问题i的简单思考:
成熟的解决方案:
1、quartz:可以去看看这篇文章[quartz分布式]( )。
2、elastic-job:()当当开发的弹性分布式任务调度系统,采用zookeeper实现分布式协调,实现任务高可用以及分片。
3、xxl-job:()是大众点评员发布的分布式任务调度平台,是一个轻量级分布式任务调度框架。
4、saturn:(https://github.com/vipshop/saturn) 是唯品会提供一个分布式、容错和高可用的作业调度服务框架。
二、springtask实现定时任务(这里是基于springboot)
1、简单的定时任务实现
使用方式:
使用缺点:
使用优点:
源码分析:
//默认使用的调度器 if(this.taskscheduler == null) { this.localexecutor = executors.newsinglethreadscheduledexecutor(); this.taskscheduler = new concurrenttaskscheduler(this.localexecutor); } //可以看到singlethreadscheduledexecutor指定的核心线程为1,说白了就是单线程执行 public static scheduledexecutorservice newsinglethreadscheduledexecutor() { return new delegatedscheduledexecutorservice (new scheduledthreadpoolexecutor(1)); } //利用了delayedworkqueue延时队列作为任务的存放队列,这样便可以实现任务延迟执行或者定时执行 public scheduledthreadpoolexecutor(int corepoolsize) { super(corepoolsize, integer.max_value, 0, nanoseconds, new delayedworkqueue()); }
2、实现并发的定时任务
使用方式:
方式一:由1中我们知道之所以定时任务是阻塞执行,是配置的线程池决定的,那就好办了,换一个不就行了!直接上代码:
@configuration public class scheduledconfig implements schedulingconfigurer { @autowired private taskscheduler mythreadpooltaskscheduler; @override public void configuretasks(scheduledtaskregistrar scheduledtaskregistrar) { //简单粗暴的方式直接指定 //scheduledtaskregistrar.setscheduler(executors.newscheduledthreadpool(5)); //也可以自定义的线程池,方便线程的使用与维护,这里不多说了 scheduledtaskregistrar.settaskscheduler(mythreadpooltaskscheduler); } } @bean(name = "mythreadpooltaskscheduler") public taskscheduler getmythreadpooltaskscheduler() { threadpooltaskscheduler taskscheduler = new threadpooltaskscheduler(); taskscheduler.setpoolsize(10); taskscheduler.setthreadnameprefix("haina-scheduled-"); taskscheduler.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy()); //调度器shutdown被调用时等待当前被调度的任务完成 taskscheduler.setwaitfortaskstocompleteonshutdown(true); //等待时长 taskscheduler.setawaitterminationseconds(60); return taskscheduler; }
方式二:方式一的本质改变了任务调度器默认使用的线程池,接下来这种是不改变调度器的默认线程池,而是把当前任务交给一个异步线程池去执行
首先使用@enableasync 启用异步任务
然后在定时任务的方法加上@async即可,默认使用的线程池为simpleasynctaskexecutor(该线程池默认来一个任务创建一个线程,就会不断创建大量线程,极有可能压爆服务器内存。当然它有自己的限流机制,这里就不多说了,有兴趣的自己翻翻源码~)
项目中为了更好的控制线程的使用,我们可以自定义我们自己的线程池,使用方式@async("mythreadpool")
废话太多,直接上代码:
@scheduled(fixedrate = 1000*10,initialdelay = 1000*20) @async("mythreadpooltaskexecutor") //@async public void scheduledtest02(){ system.out.println(thread.currentthread().getname()+"--->xxxxx--->"+thread.currentthread().getid()); } //自定义线程池 @bean(name = "mythreadpooltaskexecutor") public taskexecutor getmythreadpooltaskexecutor() { threadpooltaskexecutor taskexecutor = new threadpooltaskexecutor(); taskexecutor.setcorepoolsize(20); taskexecutor.setmaxpoolsize(200); taskexecutor.setqueuecapacity(25); taskexecutor.setkeepaliveseconds(200); taskexecutor.setthreadnameprefix("haina-threadpool-"); // 线程池对拒绝任务(无线程可用)的处理策略,目前只支持abortpolicy、callerrunspolicy;默认为后者 taskexecutor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy()); //调度器shutdown被调用时等待当前被调度的任务完成 taskexecutor.setwaitfortaskstocompleteonshutdown(true); //等待时长 taskexecutor.setawaitterminationseconds(60); taskexecutor.initialize(); return taskexecutor; }
线程池的使用心得(后续有专门文章来探讨)
java中提供了threadpoolexecutor和scheduledthreadpoolexecutor,对应与spring中的threadpooltaskexecutor和threadpooltaskscheduler,但是在原有的基础上增加了新的特性,在spring环境下更容易使用和控制。
使用自定义的线程池能够避免一些默认线程池造成的内存溢出、阻塞等等问题,更贴合自己的服务特性
使用自定义的线程池便于对项目中线程的管理、维护以及监控。
即便在非spring环境下也不要使用java默认提供的那几种线程池,坑很多,阿里代码规约不说了吗,得相信大厂!!!
三、动态定时任务的实现
问题:
使用@scheduled注解来完成设置定时任务,但是有时候我们往往需要对周期性的时间的设置会做一些改变,或者要动态的启停一个定时任务,那么这个时候使用此注解就不太方便了,原因在于这个注解中配置的cron表达式必须是常量,那么当我们修改定时参数的时候,就需要停止服务,重新部署。
解决办法:
方式一:实现schedulingconfigurer接口,重写configuretasks方法,重新制定trigger,核心方法就是addtriggertask(runnable task, trigger trigger) ,不过需要注意的是,此种方式修改了配置值后,需要在下一次调度结束后,才会更新调度器,并不会在修改配置值时实时更新,实时更新需要在修改配置值时额外增加相关逻辑处理。
@configuration public class scheduledconfig implements schedulingconfigurer { @autowired private taskscheduler mythreadpooltaskscheduler; @override public void configuretasks(scheduledtaskregistrar scheduledtaskregistrar) { //scheduledtaskregistrar.setscheduler(executors.newscheduledthreadpool(5)); scheduledtaskregistrar.settaskscheduler(mythreadpooltaskscheduler); //可以实现动态调整定时任务的执行频率 scheduledtaskregistrar.addtriggertask( //1.添加任务内容(runnable) () -> system.out.println("cccccccccccccccc--->" + thread.currentthread().getid()), //2.设置执行周期(trigger) triggercontext -> { //2.1 从数据库动态获取执行周期 string cron = "0/2 * * * * ? "; //2.2 合法性校验. // if (stringutils.isempty(cron)) { // // omitted code .. // } //2.3 返回执行周期(date) return new crontrigger(cron).nextexecutiontime(triggercontext); } ); } }
方式二:使用threadpooltaskscheduler类可实现动态添加删除功能,当然也可实现执行频率的调整
首先,我们要认识下这个调度类,它其实是对java中scheduledthreadpoolexecutor的一个封装改进后的产物,主要改进有以下几点:
顺便说下threadpooltaskexecutor相对于threadpoolexecutor的改进点:
扯了这么多,还是直接上代码:
@component public class dynamictimedtask { private static final logger logger = loggerfactory.getlogger(dynamictimedtask.class); //利用创建好的调度类统一管理 //@autowired //@qualifier("mythreadpooltaskscheduler") //private threadpooltaskscheduler mythreadpooltaskscheduler; //接受任务的返回结果 private scheduledfuture<?> future; @autowired private threadpooltaskscheduler threadpooltaskscheduler; //实例化一个线程池任务调度类,可以使用自定义的threadpooltaskscheduler @bean public threadpooltaskscheduler threadpooltaskscheduler() { threadpooltaskscheduler executor = new threadpooltaskscheduler(); return new threadpooltaskscheduler(); } /** * 启动定时任务 * @return */ public boolean startcron() { boolean flag = false; //从数据库动态获取执行周期 string cron = "0/2 * * * * ? "; future = threadpooltaskscheduler.schedule(new checkmodelfile(),cron); if (future!=null){ flag = true; logger.info("定时check训练模型文件,任务启动成功!!!"); }else { logger.info("定时check训练模型文件,任务启动失败!!!"); } return flag; } /** * 停止定时任务 * @return */ public boolean stopcron() { boolean flag = false; if (future != null) { boolean cancel = future.cancel(true); if (cancel){ flag = true; logger.info("定时check训练模型文件,任务停止成功!!!"); }else { logger.info("定时check训练模型文件,任务停止失败!!!"); } }else { flag = true; logger.info("定时check训练模型文件,任务已经停止!!!"); } return flag; } class checkmodelfile implements runnable{ @override public void run() { //编写你自己的业务逻辑 system.out.print("模型文件检查完毕!!!") } } }
四、总结
到此基于springtask下的定时任务的简单使用算是差不多了,其中不免有些错误的地方,或者理解有偏颇的地方欢迎大家提出来!
基于分布式集群下的定时任务使用,后续有时间再继续!!!
以上所述是小编给大家介绍的springboot并发定时任务动态定时任务实现详解整合,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
unity的错误解决办法:NullReferenceException: Object reference not set to an instance of an object;tiny proje
Hadoop 之 HDFS (HDFS 数据流的 读写 流程)
听说你一读Spring源码就懵逼?我帮你把架子搭好了,你填就行!
首席架构师推荐:金融保险领域数字化转型实践--如何优雅地修改业务中台中分层应用Maven多模块的版本号?(命令导入式)
[JVM学习之路]一、初识JVM,了解其结构、模型及生命周期
网友评论