From 857affbdeac7cb83ab56d56fcdeaccee27eedb36 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 30 Oct 2023 12:04:26 +0100 Subject: [PATCH] [New] ExecutionPolicy now controls delayed task life cycle --- .../strolch/execution/policy/ExecutionPolicy.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/service/src/main/java/li/strolch/execution/policy/ExecutionPolicy.java b/service/src/main/java/li/strolch/execution/policy/ExecutionPolicy.java index 927f3abf2..277aab548 100644 --- a/service/src/main/java/li/strolch/execution/policy/ExecutionPolicy.java +++ b/service/src/main/java/li/strolch/execution/policy/ExecutionPolicy.java @@ -23,6 +23,9 @@ import li.strolch.runtime.privilege.PrivilegedRunnableWithResult; import li.strolch.utils.time.PeriodDuration; import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; @@ -54,6 +57,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy { protected Locator resourceLoc; protected Locator actionLoc; + protected List> futures; + /** * The TX for this execution policy. The TX needs to be updated when this execution policy has a longer life time * than the actual TX @@ -64,6 +69,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy { super(tx); this.tx = tx; this.realm = tx.getRealmName(); + this.futures = new ArrayList<>(); } /** @@ -226,6 +232,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy { public void stop() { this.stopped = true; try { + this.futures.forEach(future -> future.cancel(false)); handleStopped(); } catch (Exception e) { logger.error("Stopping failed for " + this.actionLoc, e); @@ -282,7 +289,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy { */ public void delay(PeriodDuration duration, Runnable runnable) { long delayMs = duration.toMillis(); - getDelayedExecutionTimer().delay(delayMs, runnable); + this.futures.add(getDelayedExecutionTimer().delay(delayMs, runnable)); } /** @@ -293,7 +300,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy { */ public void delay(Duration duration, Runnable runnable) { long delayMs = duration.toMillis(); - getDelayedExecutionTimer().delay(delayMs, runnable); + this.futures.add(getDelayedExecutionTimer().delay(delayMs, runnable)); } /** @@ -336,7 +343,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy { delayMs = 20; } logger.info("Delaying runnable " + runnable + " by " + formatMillisecondsDuration(delayMs)); - getDelayedExecutionTimer().delay(delayMs, runnable); + this.futures.add(getDelayedExecutionTimer().delay(delayMs, runnable)); } /**