一文搞懂如何實(shí)現(xiàn)Java,Spring動(dòng)態(tài)啟停定時(shí)任務(wù)
為什么需要定時(shí)任務(wù)
定時(shí)任務(wù)的應(yīng)用場(chǎng)景十分廣泛,如定時(shí)清理文件、定時(shí)生成報(bào)表、定時(shí)數(shù)據(jù)同步備份等。
Java定時(shí)任務(wù)的原理
jdk自帶的庫(kù)中,有兩種技術(shù)可以實(shí)現(xiàn)定時(shí)任務(wù),一種是Timer
,另一種是ScheduledThreadPoolExecutor
Timer+TimerTask
Timer是一個(gè)線程,控制執(zhí)行TimerTask所需要執(zhí)行的內(nèi)容
public class Timer { /** * The timer task queue. This data structure is shared with the timer * thread. The timer produces tasks, via its various schedule calls, * and the timer thread consumes, executing timer tasks as appropriate, * and removing them from the queue when they're obsolete. */ private final TaskQueue queue = new TaskQueue(); /** * The timer thread. */ private final TimerThread thread = new TimerThread(queue); 。。。。。。 }
其中,需要注意,Timer類有幾個(gè)方法創(chuàng)建不同的線程執(zhí)行:
延時(shí)執(zhí)行
//其中的delay是延時(shí)時(shí)間,表示多少毫秒后執(zhí)行一次task public void schedule(TimerTask task, long delay) { if (delay < 0) throw new IllegalArgumentException("Negative delay."); sched(task, System.currentTimeMillis()+delay, 0); }
指定時(shí)間點(diǎn)執(zhí)行
//到達(dá)指定時(shí)間time的時(shí)候執(zhí)行一次task public void schedule(TimerTask task, Date time) { sched(task, time.getTime(), 0); }
延時(shí)周期執(zhí)行
//經(jīng)過delay毫秒后按每period毫秒執(zhí)行一次的周期執(zhí)行task public void schedule(TimerTask task, long delay, long period) { if (delay < 0) throw new IllegalArgumentException("Negative delay."); if (period <= 0) throw new IllegalArgumentException("Non-positive period."); sched(task, System.currentTimeMillis()+delay, -period); }
指定時(shí)間點(diǎn)后周期執(zhí)行
//到達(dá)指定時(shí)間firstTime之后按照每period毫秒執(zhí)行一次的周期執(zhí)行task public void schedule(TimerTask task, Date firstTime, long period) { if (period <= 0) throw new IllegalArgumentException("Non-positive period."); sched(task, firstTime.getTime(), -period); }
TimerTask是一個(gè)實(shí)現(xiàn)了Runable接口的類,所以能夠放到線程去執(zhí)行:
public abstract class TimerTask implements Runnable { /** * This object is used to control access to the TimerTask internals. */ final Object lock = new Object(); 。。。。。。 }
示例:
public class JavaTimerJob { public static void main(String[] args) { Timer timer = new Timer(); Task task = new Task(); //當(dāng)前時(shí)間開始,每1秒執(zhí)行一次 timer.schedule(task, new Date(),1000); } } class Task extends TimerTask { @Override public void run() { System.out.println(new Date()+": This is my job..."); } }
執(zhí)行結(jié)果:
Tue May 30 13:45:47 CST 2022: This is my job...
Tue May 30 13:45:48 CST 2022: This is my job...
Tue May 30 13:45:49 CST 2022: This is my job...
Tue May 30 13:45:50 CST 2022: This is my job...
。。。。
弊端:Timer是單線程的,一旦定時(shí)任務(wù)中某一過程時(shí)刻拋出異常,將會(huì)導(dǎo)致整體線程停止,定時(shí)任務(wù)停止。
ScheduledThreadPoolExecutor
繼承了ThreadPoolExecutor
,,是一個(gè)基于線程池的調(diào)度器 通過實(shí)現(xiàn)ScheduledExecutorService
接口方法去實(shí)現(xiàn)任務(wù)調(diào)度,主要方法如下:
延時(shí)執(zhí)行
//command是待執(zhí)行的線程,delay表示延時(shí)時(shí)長(zhǎng),unit代表時(shí)間單位 public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); RunnableScheduledFuture<?> t = decorateTask(command, new ScheduledFutureTask<Void>(command, null, triggerTime(delay, unit))); delayedExecute(t); return t; }
延時(shí)周期執(zhí)行
//command是待執(zhí)行的線程,initialDelay表示延時(shí)時(shí)長(zhǎng),period代表執(zhí)行間隔時(shí)長(zhǎng),unit代表時(shí)間單位 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (period <= 0) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), unit.toNanos(period)); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; }
每段延時(shí)間隔執(zhí)行
//command是待執(zhí)行的線程,initialDelay表示延時(shí)時(shí)長(zhǎng),delay代表每次執(zhí)行線程前的延時(shí)時(shí)長(zhǎng),unit代表時(shí)間單位 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (delay <= 0) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), unit.toNanos(-delay)); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; }
示例:
public class JavaScheduledThreadPoolExecutor { public static void main(String[] args) { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8); //延時(shí)1秒后開始執(zhí)行,每3秒執(zhí)行一次 scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println(new Date()+": This is my job..."); } }, 1, 3, TimeUnit.SECONDS); } }
執(zhí)行結(jié)果:
Tue May 30 15:05:16 CST 2022: This is my job...
Tue May 30 15:05:19 CST 2022: This is my job...
Tue May 30 15:05:22 CST 2022: This is my job...
Tue May 30 15:05:25 CST 2022: This is my job...
。。。。。
Timer VS ScheduledThreadPoolExecutor
Timer
- 是單線程,如果開啟多個(gè)線程服務(wù),將會(huì)出現(xiàn)競(jìng)爭(zhēng),一旦出現(xiàn)異常,線程停止,定時(shí)任務(wù)停止;
- 兼容性更高,jdk1.3后使用
ScheduledThreadPoolExecutor
- 基于線程池實(shí)現(xiàn)多線程,且自動(dòng)調(diào)整線程數(shù),線程出錯(cuò)并不會(huì)影響整體定時(shí)任務(wù)執(zhí)行。
- 在jdk1.5后可使用
Spring定時(shí)任務(wù)
Spring原生定時(shí)任務(wù)主要依靠@Scheduled
注解實(shí)現(xiàn):
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled { String CRON_DISABLED = "-"; String cron() default ""; //類似于corn表達(dá)式,可以指定定時(shí)任務(wù)執(zhí)行的延遲及周期規(guī)則 String zone() default ""; //指明解析cron表達(dá)式的時(shí)區(qū)。 long fixedDelay() default -1; //在最后一次調(diào)用結(jié)束和下一次調(diào)用開始之間以固定周期(以毫秒為單位)執(zhí)行帶注解的方法。(要等待上次任務(wù)完成后) String fixedDelayString() default ""; //同上面作用一樣,只是String類型 long fixedRate() default -1; //在調(diào)用之間以固定的周期(以毫秒為單位)執(zhí)行帶注解的方法。(不需要等待上次任務(wù)完成) String fixedRateString() default ""; //同上面作用一樣,只是String類型 long initialDelay() default -1; //第一次執(zhí)行fixedRate()或fixedDelay()任務(wù)之前延遲的毫秒數(shù) 。 String initialDelayString() default ""; //同上面作用一樣,只是String類型 }
Spring靜態(tài)定時(shí)任務(wù)示例:
@Slf4j @Component public class TestJob { //每40秒執(zhí)行一次 @Scheduled(cron = "0/40 * * * * ?") public void logJob(){ if(log.isDebugEnabled()){ log.debug("現(xiàn)在是:{}",LocalDateTime.now()); } } }
執(zhí)行結(jié)果:
現(xiàn)在是:2022-05-30T16:03:40.006
現(xiàn)在是:2022-05-30T16:04
現(xiàn)在是:2022-05-30T16:04:40.003
@Scheduled定時(shí)任務(wù)原理(源碼)
①項(xiàng)目啟動(dòng)掃描帶有注解@Scheduled
的所有方法信息由ScheduledAnnotationBeanPostProcessor
的postProcessAfterInitialization
方法實(shí)現(xiàn)功能:
public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof AopInfrastructureBean || bean instanceof TaskScheduler || bean instanceof ScheduledExecutorService) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean); if (!this.nonAnnotatedClasses.contains(targetClass)) { //獲取定時(shí)任務(wù)的方法 Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass, (MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> { Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations( method, Scheduled.class, Schedules.class); return (!scheduledMethods.isEmpty() ? scheduledMethods : null); }); if (annotatedMethods.isEmpty()) { this.nonAnnotatedClasses.add(targetClass); if (logger.isTraceEnabled()) { logger.trace("No @Scheduled annotations found on bean class: " + targetClass); } } else { // Non-empty set of methods annotatedMethods.forEach((method, scheduledMethods) -> //調(diào)用processScheduled方法將定時(shí)任務(wù)方法存放到任務(wù)隊(duì)列中 scheduledMethods.forEach(scheduled -> processScheduled(scheduled, method, bean))); if (logger.isTraceEnabled()) { logger.trace(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName + "': " + annotatedMethods); } } } return bean; }
②調(diào)用processScheduled
方法將定時(shí)任務(wù)方法存放到任務(wù)隊(duì)列中
protected void processScheduled(Scheduled scheduled, Method method, Object bean) { try { //創(chuàng)建任務(wù)線程 Runnable runnable = createRunnable(bean, method); boolean processedSchedule = false; String errorMessage = "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required"; Set<ScheduledTask> tasks = new LinkedHashSet<>(4); //解析任務(wù)執(zhí)行初始延遲 long initialDelay = scheduled.initialDelay(); String initialDelayString = scheduled.initialDelayString(); if (StringUtils.hasText(initialDelayString)) { Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both"); if (this.embeddedValueResolver != null) { initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString); } if (StringUtils.hasLength(initialDelayString)) { try { initialDelay = parseDelayAsLong(initialDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException( "Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into long"); } } } //解析cron表達(dá)式 String cron = scheduled.cron(); if (StringUtils.hasText(cron)) { String zone = scheduled.zone(); if (this.embeddedValueResolver != null) { cron = this.embeddedValueResolver.resolveStringValue(cron); zone = this.embeddedValueResolver.resolveStringValue(zone); } if (StringUtils.hasLength(cron)) { Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers"); processedSchedule = true; if (!Scheduled.CRON_DISABLED.equals(cron)) { TimeZone timeZone; if (StringUtils.hasText(zone)) { timeZone = StringUtils.parseTimeZoneString(zone); } else { timeZone = TimeZone.getDefault(); } tasks.add(this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone)))); } } } // At this point we don't need to differentiate between initial delay set or not anymore if (initialDelay < 0) { initialDelay = 0; } //解析fixedDelay參數(shù) long fixedDelay = scheduled.fixedDelay(); if (fixedDelay >= 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; //存放任務(wù)到任務(wù)隊(duì)列中 tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } String fixedDelayString = scheduled.fixedDelayString(); if (StringUtils.hasText(fixedDelayString)) { if (this.embeddedValueResolver != null) { fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString); } if (StringUtils.hasLength(fixedDelayString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; try { fixedDelay = parseDelayAsLong(fixedDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException( "Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long"); } tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } } //解析fixedRate參數(shù) long fixedRate = scheduled.fixedRate(); if (fixedRate >= 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } String fixedRateString = scheduled.fixedRateString(); if (StringUtils.hasText(fixedRateString)) { if (this.embeddedValueResolver != null) { fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString); } if (StringUtils.hasLength(fixedRateString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; try { fixedRate = parseDelayAsLong(fixedRateString); } catch (RuntimeException ex) { throw new IllegalArgumentException( "Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long"); } tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } } // 斷言檢查 Assert.isTrue(processedSchedule, errorMessage); //并發(fā)控制將任務(wù)隊(duì)列存入注冊(cè)任務(wù)列表 synchronized (this.scheduledTasks) { Set<ScheduledTask> regTasks = this.scheduledTasks.computeIfAbsent(bean, key -> new LinkedHashSet<>(4)); regTasks.addAll(tasks); } } catch (IllegalArgumentException ex) { throw new IllegalStateException( "Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage()); } }
③將任務(wù)解析并添加到任務(wù)隊(duì)列后,交由ScheduledTaskRegistrar
類的scheduleTasks
方法添加(注冊(cè))定時(shí)任務(wù)到環(huán)境中:
protected void scheduleTasks() { if (this.taskScheduler == null) { //獲取ScheduledExecutorService對(duì)象,實(shí)際上都是使用ScheduledThreadPoolExecutor執(zhí)行定時(shí)任務(wù)調(diào)度 this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor); } if (this.triggerTasks != null) { for (TriggerTask task : this.triggerTasks) { addScheduledTask(scheduleTriggerTask(task)); } } if (this.cronTasks != null) { for (CronTask task : this.cronTasks) { addScheduledTask(scheduleCronTask(task)); } } if (this.fixedRateTasks != null) { for (IntervalTask task : this.fixedRateTasks) { addScheduledTask(scheduleFixedRateTask(task)); } } if (this.fixedDelayTasks != null) { for (IntervalTask task : this.fixedDelayTasks) { addScheduledTask(scheduleFixedDelayTask(task)); } } } private void addScheduledTask(@Nullable ScheduledTask task) { if (task != null) { this.scheduledTasks.add(task); } }
由上述源碼可以看出,Spring原生定時(shí)任務(wù)的大概步驟如下:
1.掃描帶@Scheduled注解的類和方法(ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(........))
2.將定時(shí)任務(wù)解析完成后加入任務(wù)隊(duì)列(ScheduledAnnotationBeanPostProcessor.processScheduled(........))
3.將定時(shí)任務(wù)注冊(cè)到當(dāng)前運(yùn)行環(huán)境,等待執(zhí)行(ScheduledTaskRegistrar.scheduleTasks(.......)) 且@Scheduled的底層調(diào)度實(shí)現(xiàn)是ScheduledThreadPoolExecutor
以上就是一文搞懂如何實(shí)現(xiàn)Java,Spring動(dòng)態(tài)啟停定時(shí)任務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Java Spring啟停定時(shí)任務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jdk17+springboot使用webservice的踩坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于jdk17+springboot使用webservice踩坑的相關(guān)資料,網(wǎng)上很多教程是基于jdk8的,所以很多在17上面跑不起來,折騰兩天,直接給答案,需要的朋友可以參考下2024-01-01三行Java代碼實(shí)現(xiàn)計(jì)算多邊形的幾何中心點(diǎn)
因?yàn)楣ぷ餍枰?jì)算采煤機(jī)工作面的中心點(diǎn),如果套用數(shù)學(xué)的計(jì)算公式,用java去實(shí)現(xiàn),太麻煩了。本文將利用java幾何計(jì)算的工具包,幾行代碼就能求出多變形的中心,簡(jiǎn)直yyds!還不快跟隨小編一起學(xué)起來2022-10-10關(guān)于spring?boot使用?jdbc+mysql?連接的問題
這篇文章主要介紹了spring?boot使用?jdbc+mysql?連接,在這里mysql?8.x版本驅(qū)動(dòng)包,要使用?com.mysql.cj.jdbc.Driver作為驅(qū)動(dòng)類,文中給大家詳細(xì)介紹,需要的朋友可以參考下2022-03-03Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法
這篇文章主要介紹了Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法,需要的朋友可以參考下2015-11-11Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
這篇文章主要介紹了Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析,Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null,int和integer(無論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比,需要的朋友可以參考下2023-12-12