Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automation] Expose TriggerHandlerCallback scheduler #2388

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,11 @@ public void triggered(Map<String, ?> context) {
callback.triggered(this.trigger, context);
}

@Override
public ScheduledExecutorService getScheduler() {
return callback.getScheduler();
}

@Override
public Boolean isEnabled(String ruleUID) {
return callback.isEnabled(ruleUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@
*
* @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 {

Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
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;
Expand All @@ -32,12 +32,13 @@
*
* @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;

Expand All @@ -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
Expand Down Expand Up @@ -129,4 +130,9 @@ public void runNow(String uid) {
public void runNow(String uid, boolean considerConditions, Map<String, Object> context) {
re.runNow(uid, considerConditions, context);
}

@Override
public ScheduledExecutorService getScheduler() {
return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -104,6 +105,11 @@ public void triggered(Trigger trigger, Map<String, ?> context) {
}
}

@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
Expand Down