[New] ExecutionPolicy now controls delayed task life cycle
This commit is contained in:
parent
e80d0a7944
commit
857affbdea
|
@ -23,6 +23,9 @@ import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
|
||||||
import li.strolch.utils.time.PeriodDuration;
|
import li.strolch.utils.time.PeriodDuration;
|
||||||
|
|
||||||
import java.time.Duration;
|
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.ThreadLocalRandom;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -54,6 +57,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
|
||||||
protected Locator resourceLoc;
|
protected Locator resourceLoc;
|
||||||
protected Locator actionLoc;
|
protected Locator actionLoc;
|
||||||
|
|
||||||
|
protected List<ScheduledFuture<?>> futures;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The TX for this execution policy. The TX needs to be updated when this execution policy has a longer life time
|
* 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
|
* than the actual TX
|
||||||
|
@ -64,6 +69,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
|
||||||
super(tx);
|
super(tx);
|
||||||
this.tx = tx;
|
this.tx = tx;
|
||||||
this.realm = tx.getRealmName();
|
this.realm = tx.getRealmName();
|
||||||
|
this.futures = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -226,6 +232,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
|
||||||
public void stop() {
|
public void stop() {
|
||||||
this.stopped = true;
|
this.stopped = true;
|
||||||
try {
|
try {
|
||||||
|
this.futures.forEach(future -> future.cancel(false));
|
||||||
handleStopped();
|
handleStopped();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Stopping failed for " + this.actionLoc, 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) {
|
public void delay(PeriodDuration duration, Runnable runnable) {
|
||||||
long delayMs = duration.toMillis();
|
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) {
|
public void delay(Duration duration, Runnable runnable) {
|
||||||
long delayMs = duration.toMillis();
|
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;
|
delayMs = 20;
|
||||||
}
|
}
|
||||||
logger.info("Delaying runnable " + runnable + " by " + formatMillisecondsDuration(delayMs));
|
logger.info("Delaying runnable " + runnable + " by " + formatMillisecondsDuration(delayMs));
|
||||||
getDelayedExecutionTimer().delay(delayMs, runnable);
|
this.futures.add(getDelayedExecutionTimer().delay(delayMs, runnable));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue