[automation] Expose TriggerHandlerCallback scheduler (#2388)

* [automation] Expose TriggerHandlerCallback scheduler

Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
This commit is contained in:
Fabian Wolter 2021-05-31 10:04:21 +02:00 committed by GitHub
parent 17f95768a4
commit e007307796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 3 deletions

View File

@ -13,6 +13,7 @@
package org.openhab.core.automation.module.script.rulesupport.internal.delegates;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import org.openhab.core.automation.RuleStatus;
import org.openhab.core.automation.RuleStatusInfo;
@ -44,6 +45,11 @@ public class SimpleTriggerHandlerCallbackDelegate implements SimpleTriggerHandle
callback.triggered(this.trigger, context);
}
@Override
public ScheduledExecutorService getScheduler() {
return callback.getScheduler();
}
@Override
public Boolean isEnabled(String ruleUID) {
return callback.isEnabled(ruleUID);

View File

@ -13,6 +13,7 @@
package org.openhab.core.automation.handler;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import org.openhab.core.automation.ModuleHandlerCallback;
import org.openhab.core.automation.Rule;
@ -27,6 +28,7 @@ import org.openhab.core.automation.type.Output;
*
* @author Yordan Mihaylov - Initial contribution
* @author Kai Kreuzer - made it a sub-interface of ModuleHandlerCallback
* @author Fabian Wolter - Add method for retrieving the handler's scheduler
*/
public interface TriggerHandlerCallback extends ModuleHandlerCallback {
@ -45,4 +47,9 @@ public interface TriggerHandlerCallback extends ModuleHandlerCallback {
* </ul>
*/
public void triggered(Trigger trigger, Map<String, ?> context);
/**
* @return the scheduler of this rule
*/
public ScheduledExecutorService getScheduler();
}

View File

@ -15,9 +15,9 @@ package org.openhab.core.automation.internal;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import org.openhab.core.automation.RuleStatus;
import org.openhab.core.automation.RuleStatusInfo;
@ -32,12 +32,13 @@ import org.openhab.core.common.NamedThreadFactory;
*
* @author Yordan Mihaylov - Initial contribution
* @author Kai Kreuzer - improved stability
* @author Fabian Wolter - Change executor to ScheduledExecutorService and expose it
*/
public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
private final String ruleUID;
private ExecutorService executor;
private ScheduledExecutorService executor;
private Future<?> future;
@ -46,7 +47,7 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
protected TriggerHandlerCallbackImpl(RuleEngineImpl re, String ruleUID) {
this.re = re;
this.ruleUID = ruleUID;
executor = Executors.newSingleThreadExecutor(new NamedThreadFactory("rule-" + ruleUID));
executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("rule-" + ruleUID));
}
@Override
@ -129,4 +130,9 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
public void runNow(String uid, boolean considerConditions, Map<String, Object> context) {
re.runNow(uid, considerConditions, context);
}
@Override
public ScheduledExecutorService getScheduler() {
return executor;
}
}

View File

@ -17,6 +17,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.ModuleHandlerCallback;
@ -104,6 +105,11 @@ public class CompositeTriggerHandler
}
}
@Override
public ScheduledExecutorService getScheduler() {
return callback.getScheduler();
}
/**
* The {@link CompositeTriggerHandler} sets itself as callback to the child triggers and store the callback to the
* rule engine. In this way the trigger of composite type will be notified always when some of the child triggers