From 987cf221d35ca9c70de9cd5fcd2652663d5a40c4 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Thu, 8 Dec 2022 21:38:32 +0100 Subject: [PATCH] Fix timer cancellation in automation cache (#3204) Signed-off-by: Jan N. Klug --- .../internal/CacheScriptExtension.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java index 27a688f624..3aa6afd855 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java @@ -19,7 +19,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.Timer; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -131,16 +130,23 @@ public class CacheScriptExtension implements ScriptExtensionProvider { } /** - * Check if object is {@link ScheduledFuture} or {@link Timer} and schedule cancellation of those jobs + * Check if object is {@link ScheduledFuture}, {@link java.util.Timer} or + * {@link org.openhab.core.automation.module.script.action.Timer} and schedule cancellation of those jobs * * @param o the {@link Object} to check */ private void asyncCancelJob(@Nullable Object o) { + Runnable cancelJob = null; if (o instanceof ScheduledFuture) { - scheduler.execute(() -> ((ScheduledFuture) o).cancel(true)); - } else if (o instanceof Timer) { + cancelJob = () -> ((ScheduledFuture) o).cancel(true); + } else if (o instanceof java.util.Timer) { + cancelJob = () -> ((java.util.Timer) o).cancel(); + } else if (o instanceof org.openhab.core.automation.module.script.action.Timer) { + cancelJob = () -> ((org.openhab.core.automation.module.script.action.Timer) o).cancel(); + } + if (cancelJob != null) { // not using execute so ensure this operates in another thread and we don't block here - scheduler.schedule(() -> ((Timer) o).cancel(), 0, TimeUnit.SECONDS); + scheduler.schedule(cancelJob, 0, TimeUnit.SECONDS); } }