三生三世十里桃花45集,美姑县人民政府,aolikes
1. 定时任务实现方式
定时任务实现方式:
定时任务执行方式:
2. 创建定时任务
package com.autonavi.task.test; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; import com.autonavi.task.scheduledtasks; @component public class scheduledtest { private static final logger logger = loggerfactory.getlogger(scheduledtasks.class); @scheduled(cron="0 0/2 * * * ?") public void executefiledownloadtask() { // 间隔2分钟,执行任务 thread current = thread.currentthread(); system.out.println("定时任务1:"+current.getid()); logger.info("scheduledtest.executefiledownloadtask 定时任务1:"+current.getid()+ ",name:"+current.getname()); } }
@scheduled 注解用于标注这个方法是一个定时任务的方法,有多种配置可选。cron支持cron表达式,指定任务在特定时间执行;fixedrate以特定频率执行任务;fixedratestring以string的形式配置执行频率。
3. 启动定时任务
@springbootapplication @enablescheduling public class app { private static final logger logger = loggerfactory.getlogger(app.class); public static void main(string[] args) { springapplication.run(app.class, args); logger.info("start"); } }
其中 @enablescheduling 注解的作用是发现注解@scheduled的任务并后台执行。
springboot本身默认的执行方式是串行执行,也就是说无论有多少task,都是一个线程串行执行,并行需手动配置
4. 并行任务
继承schedulingconfigurer类并重写其方法即可,如下:
@configuration @enablescheduling public class scheduleconfig implements schedulingconfigurer { @override public void configuretasks(scheduledtaskregistrar taskregistrar) { taskregistrar.setscheduler(taskexecutor()); } @bean(destroymethod="shutdown") public executor taskexecutor() { return executors.newscheduledthreadpool(100); } }
再次执行之前的那个demo,会欣然发现已经是并行执行了!
5. 异步并行任务
import org.springframework.scheduling.taskscheduler; import org.springframework.scheduling.annotation.asyncconfigurer; import org.springframework.scheduling.annotation.enableasync; import org.springframework.scheduling.annotation.enablescheduling; import org.springframework.scheduling.annotation.schedulingconfigurer; import org.springframework.scheduling.concurrent.threadpooltaskscheduler; import org.springframework.scheduling.config.scheduledtaskregistrar; @configuration @enablescheduling @enableasync( mode = advicemode.proxy, proxytargetclass = false, order = ordered.highest_precedence ) @componentscan( basepackages = "hello" ) public class rootcontextconfiguration implements asyncconfigurer, schedulingconfigurer { @bean public threadpooltaskscheduler taskscheduler() { threadpooltaskscheduler scheduler = new threadpooltaskscheduler(); scheduler.setpoolsize(20); scheduler.setthreadnameprefix("task-"); scheduler.setawaitterminationseconds(60); scheduler.setwaitfortaskstocompleteonshutdown(true); return scheduler; } @override public executor getasyncexecutor() { executor executor = this.taskscheduler(); return executor; } @override public void configuretasks(scheduledtaskregistrar registrar) { taskscheduler scheduler = this.taskscheduler(); registrar.settaskscheduler(scheduler); } }
在启动的main方法加入额外配置:
@springbootapplication public class application { public static void main(string[] args) throws exception { annotationconfigapplicationcontext rootcontext = new annotationconfigapplicationcontext(); rootcontext.register(rootcontextconfiguration.class); rootcontext.refresh(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
apollo与springboot集成实现动态刷新配置的教程详解
网友评论