[Major] Refactored the DurationExecutionTimer to not be singleton
Once again it is clear how bad singletons are. One test killed the timer, thus all other tests failed. Now the DelayedExecutionTimer is retrieved from the ExecutionHandler and is called DelayedExecutionTimer with a default implementation of SimpleDurationExecutionTimer instantiated by the EventBasedExecutionHandler
This commit is contained in:
parent
992f3966dc
commit
bff0215510
|
@ -28,6 +28,7 @@ public class StrolchRestfulConstants {
|
||||||
public static final String LAST_OFFSET = "lastOffset";
|
public static final String LAST_OFFSET = "lastOffset";
|
||||||
public static final String NEXT_OFFSET = "nextOffset";
|
public static final String NEXT_OFFSET = "nextOffset";
|
||||||
public static final String PREVIOUS_OFFSET = "previousOffset";
|
public static final String PREVIOUS_OFFSET = "previousOffset";
|
||||||
|
public static final String DATA_SET_SIZE = "dataSetSize";
|
||||||
public static final String SIZE = "size";
|
public static final String SIZE = "size";
|
||||||
public static final String OFFSET = "offset";
|
public static final String OFFSET = "offset";
|
||||||
public static final String LIMIT = "limit";
|
public static final String LIMIT = "limit";
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package li.strolch.execution;
|
||||||
|
|
||||||
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
|
import li.strolch.model.Locator;
|
||||||
|
|
||||||
|
public interface DelayedExecutionTimer {
|
||||||
|
|
||||||
|
void cancel(Locator locator);
|
||||||
|
|
||||||
|
void execute(String realm, ComponentContainer container, Locator locator, long duration);
|
||||||
|
|
||||||
|
void destroy();
|
||||||
|
}
|
|
@ -16,10 +16,31 @@ import li.strolch.privilege.model.PrivilegeContext;
|
||||||
|
|
||||||
public class EventBasedExecutionHandler extends ExecutionHandler {
|
public class EventBasedExecutionHandler extends ExecutionHandler {
|
||||||
|
|
||||||
|
private DelayedExecutionTimer delayedExecutionTimer;
|
||||||
|
|
||||||
public EventBasedExecutionHandler(ComponentContainer container, String componentName) {
|
public EventBasedExecutionHandler(ComponentContainer container, String componentName) {
|
||||||
super(container, componentName);
|
super(container, componentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() throws Exception {
|
||||||
|
|
||||||
|
this.delayedExecutionTimer = new SimpleDurationExecutionTimer();
|
||||||
|
|
||||||
|
super.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() throws Exception {
|
||||||
|
|
||||||
|
if (this.delayedExecutionTimer != null) {
|
||||||
|
this.delayedExecutionTimer.destroy();
|
||||||
|
this.delayedExecutionTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
super.stop();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void toExecution(String realm, Locator locator) {
|
public void toExecution(String realm, Locator locator) {
|
||||||
runAsAgent(ctx -> {
|
runAsAgent(ctx -> {
|
||||||
|
@ -125,4 +146,9 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
|
||||||
protected StrolchTransaction openTx(String realm, Certificate cert, Class<?> clazz) {
|
protected StrolchTransaction openTx(String realm, Certificate cert, Class<?> clazz) {
|
||||||
return getContainer().getRealm(realm).openTx(cert, clazz);
|
return getContainer().getRealm(realm).openTx(cert, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DelayedExecutionTimer getDelayedExecutionTimer() {
|
||||||
|
return this.delayedExecutionTimer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ public abstract class ExecutionHandler extends StrolchComponent {
|
||||||
public ExecutionHandler(ComponentContainer container, String componentName) {
|
public ExecutionHandler(ComponentContainer container, String componentName) {
|
||||||
super(container, componentName);
|
super(container, componentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract DelayedExecutionTimer getDelayedExecutionTimer();
|
||||||
|
|
||||||
public abstract void toExecution(String realm, Locator locator);
|
public abstract void toExecution(String realm, Locator locator);
|
||||||
|
|
||||||
|
|
|
@ -11,28 +11,26 @@ import org.slf4j.LoggerFactory;
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.model.Locator;
|
import li.strolch.model.Locator;
|
||||||
|
|
||||||
public class DurationExecutionTimer {
|
public class SimpleDurationExecutionTimer implements DelayedExecutionTimer {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DurationExecutionTimer.class);
|
private static final Logger logger = LoggerFactory.getLogger(SimpleDurationExecutionTimer.class);
|
||||||
|
|
||||||
private static final DurationExecutionTimer instance;
|
|
||||||
|
|
||||||
static {
|
|
||||||
instance = new DurationExecutionTimer();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static DurationExecutionTimer getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
|
|
||||||
private Map<Locator, SimulationTask> simulationPolicies;
|
private Map<Locator, SimulationTask> simulationPolicies;
|
||||||
|
|
||||||
public DurationExecutionTimer() {
|
public SimpleDurationExecutionTimer() {
|
||||||
this.simulationPolicies = new HashMap<>();
|
this.simulationPolicies = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
if (this.timer != null) {
|
||||||
|
this.timer.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void cancel(Locator locator) {
|
public void cancel(Locator locator) {
|
||||||
SimulationTask task = this.simulationPolicies.remove(locator);
|
SimulationTask task = this.simulationPolicies.remove(locator);
|
||||||
if (task != null) {
|
if (task != null) {
|
||||||
|
@ -40,6 +38,7 @@ public class DurationExecutionTimer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void execute(String realm, ComponentContainer container, Locator locator, long duration) {
|
public void execute(String realm, ComponentContainer container, Locator locator, long duration) {
|
||||||
if (this.timer == null)
|
if (this.timer == null)
|
||||||
this.timer = new Timer("SimulationExecution", true);
|
this.timer = new Timer("SimulationExecution", true);
|
|
@ -2,7 +2,6 @@ package li.strolch.execution.policy;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.command.UpdateActivityCommand;
|
import li.strolch.command.UpdateActivityCommand;
|
||||||
import li.strolch.execution.DurationExecutionTimer;
|
|
||||||
import li.strolch.model.Locator;
|
import li.strolch.model.Locator;
|
||||||
import li.strolch.model.State;
|
import li.strolch.model.State;
|
||||||
import li.strolch.model.activity.Action;
|
import li.strolch.model.activity.Action;
|
||||||
|
@ -33,7 +32,7 @@ public class DurationExecution extends ExecutionPolicy {
|
||||||
String realmName = tx().getRealmName();
|
String realmName = tx().getRealmName();
|
||||||
Locator locator = action.getLocator();
|
Locator locator = action.getLocator();
|
||||||
logger.warn("Executing action " + action.getLocator() + " has a duration of " + durationP.getValueAsString());
|
logger.warn("Executing action " + action.getLocator() + " has a duration of " + durationP.getValueAsString());
|
||||||
DurationExecutionTimer.getInstance().execute(realmName, getContainer(), locator, durationP.getValue());
|
getDelayedExecutionTimer().execute(realmName, getContainer(), locator, durationP.getValue());
|
||||||
|
|
||||||
action.setState(State.EXECUTION);
|
action.setState(State.EXECUTION);
|
||||||
|
|
||||||
|
@ -59,7 +58,7 @@ public class DurationExecution extends ExecutionPolicy {
|
||||||
@Override
|
@Override
|
||||||
public void toStopped(Action action) {
|
public void toStopped(Action action) {
|
||||||
|
|
||||||
DurationExecutionTimer.getInstance().cancel(action.getLocator());
|
getDelayedExecutionTimer().cancel(action.getLocator());
|
||||||
|
|
||||||
action.setState(State.STOPPED);
|
action.setState(State.STOPPED);
|
||||||
|
|
||||||
|
@ -73,7 +72,7 @@ public class DurationExecution extends ExecutionPolicy {
|
||||||
@Override
|
@Override
|
||||||
public void toError(Action action) {
|
public void toError(Action action) {
|
||||||
|
|
||||||
DurationExecutionTimer.getInstance().cancel(action.getLocator());
|
getDelayedExecutionTimer().cancel(action.getLocator());
|
||||||
|
|
||||||
action.setState(State.ERROR);
|
action.setState(State.ERROR);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package li.strolch.execution.policy;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.command.UpdateActivityCommand;
|
import li.strolch.command.UpdateActivityCommand;
|
||||||
|
import li.strolch.execution.DelayedExecutionTimer;
|
||||||
|
import li.strolch.execution.ExecutionHandler;
|
||||||
import li.strolch.model.State;
|
import li.strolch.model.State;
|
||||||
import li.strolch.model.activity.Action;
|
import li.strolch.model.activity.Action;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
@ -31,4 +33,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
|
||||||
|
|
||||||
logger.warn("Action " + action.getLocator() + " is now in WARNING!");
|
logger.warn("Action " + action.getLocator() + " is now in WARNING!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected DelayedExecutionTimer getDelayedExecutionTimer() {
|
||||||
|
return getComponent(ExecutionHandler.class).getDelayedExecutionTimer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue