From 0f3ad713107b14ca49a83c73039eb6afc4d57c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B0=E9=BE=99=E5=AE=A2=E6=A0=88?= <704831365@qq.com> Date: Thu, 8 Aug 2019 17:20:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20JbootScheduleManager.java?= =?UTF-8?q?=20=E4=BF=AE=E6=AD=A3FixedRate=E5=92=8CFixedDelay=20Job?= =?UTF-8?q?=E5=9C=A8=E6=8F=92=E4=BB=B6=E4=B8=AD=E6=97=A0=E6=B3=95=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=20JbootCron4jPlug?= =?UTF-8?q?in=E7=9A=84=E6=8F=92=E4=BB=B6=E4=B8=AD=E5=90=AF=E5=81=9C?= =?UTF-8?q?=E6=9A=82=E6=97=B6=E4=B8=8D=E6=94=AF=E6=8C=81=EF=BC=8C=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=B8=ADJob=E5=8A=A0=E8=BD=BD=E7=9B=AE?= =?UTF-8?q?=E5=89=8D=E9=9C=80=E5=9C=A8onStart=E5=92=8ConStop=E4=B8=AD?= =?UTF-8?q?=E6=89=8B=E5=8A=A8addSchedule=E5=92=8CremoveSchedule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By lixin <704831365@qq.com> --- .../components/schedule/JbootScheduleManager.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jboot/components/schedule/JbootScheduleManager.java b/src/main/java/io/jboot/components/schedule/JbootScheduleManager.java index 0ec47683..cfbc1cfd 100644 --- a/src/main/java/io/jboot/components/schedule/JbootScheduleManager.java +++ b/src/main/java/io/jboot/components/schedule/JbootScheduleManager.java @@ -31,6 +31,7 @@ import java.io.File; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -45,6 +46,9 @@ public class JbootScheduleManager { private JbooScheduleConfig config; private Map scheduleRunnableCache = new ConcurrentHashMap<>(); + + // add by lixin 08.08, 用于 remove fixedScheduler + private Map scheduleFuture = new ConcurrentHashMap<>(); public JbootScheduleManager() { config = Jboot.config(JbooScheduleConfig.class); @@ -54,6 +58,7 @@ public class JbootScheduleManager { cron4jPlugin = cron4jProperties.exists() ? new JbootCron4jPlugin(new Prop(config.getCron4jFile())) : new JbootCron4jPlugin(); + LOG.info("Init JbootScheduleManager"); } @@ -88,7 +93,9 @@ public class JbootScheduleManager { : new JbootDistributedRunnable(runnable, fixedDelayJob.period()); try { scheduleRunnableCache.put(runnableClass,executeRunnable); - fixedScheduler.scheduleWithFixedDelay(executeRunnable, fixedDelayJob.initialDelay(), fixedDelayJob.period(), TimeUnit.SECONDS); + // modified by lixin 08.08, 用于remove fixedScheduler + // fixedScheduler.scheduleWithFixedDelay(executeRunnable, fixedDelayJob.initialDelay(), fixedDelayJob.period(), TimeUnit.SECONDS); + scheduleFuture.put(runnableClass, fixedScheduler.scheduleWithFixedDelay(executeRunnable, fixedDelayJob.initialDelay(), fixedDelayJob.period(), TimeUnit.SECONDS)); } catch (Exception e) { LOG.error(e.toString(), e); } @@ -102,7 +109,9 @@ public class JbootScheduleManager { : new JbootDistributedRunnable(runnable, fixedRateJob.period()); try { scheduleRunnableCache.put(runnableClass,executeRunnable); - fixedScheduler.scheduleAtFixedRate(executeRunnable, fixedRateJob.initialDelay(), fixedRateJob.period(), TimeUnit.SECONDS); + // modified by lixin 08.08, 用于remove fixedScheduler + // fixedScheduler.scheduleAtFixedRate(executeRunnable, fixedRateJob.initialDelay(), fixedRateJob.period(), TimeUnit.SECONDS); + scheduleFuture.put(runnableClass, fixedScheduler.scheduleAtFixedRate(executeRunnable, fixedRateJob.initialDelay(), fixedRateJob.period(), TimeUnit.SECONDS)); } catch (Exception e) { LOG.error(e.toString(), e); } @@ -128,6 +137,8 @@ public class JbootScheduleManager { if (runnable != null){ fixedScheduler.remove(runnable); scheduleRunnableCache.remove(removeClass); + //add by lixin 08.08, 用于remove fixedScheduler + scheduleFuture.get(removeClass).cancel(true); } } -- Gitee