Package org.axonframework.update.common
Class DelayedTask
java.lang.Object
org.axonframework.update.common.DelayedTask
A utility class to run a task with a delay in a virtual thread. The task can be checked for its status (started,
finished, failed), and the failure cause can be retrieved if it failed.
This has severely lower overhead than using a ScheduledExecutorService or similar
constructs, as it does not require a thread pool or scheduling mechanism. It simply runs the task in a virtual thread
after the specified delay. The virtual thread is parked for the duration of the delay, and is executed on a carrier
thread when ready, which allows it to be lightweight and efficient.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels the task if it is still running.Gets the cause of the failure if the task has failed.booleanisFailed()Whether the task has failed.booleanWhether the task has finished running.booleanWhether the task has started running.static DelayedTaskCreates a newDelayedTaskthat runs the givenrunnableafter the specifieddelay.
-
Method Details
-
of
Creates a newDelayedTaskthat runs the givenrunnableafter the specifieddelay.- Parameters:
runnable- The task to run after the delay.delay- The delay in milliseconds before the task is executed. Must be non-negative.- Returns:
- A new instance of
DelayedTaskthat will run the givenrunnableafter the specifieddelay.
-
isStarted
public boolean isStarted()Whether the task has started running.- Returns:
trueif the task has started,falseotherwise.
-
isFinished
public boolean isFinished()Whether the task has finished running.- Returns:
trueif the task has finished,falseotherwise.
-
isFailed
public boolean isFailed()Whether the task has failed.- Returns:
trueif the task has failed,falseotherwise.
-
getFailureCause
Gets the cause of the failure if the task has failed.- Returns:
- The exception that caused the failure, or
nullif the task has not failed.
-
cancel
public void cancel()Cancels the task if it is still running. If the task has already started, it will not be interrupted. If the task is currently sleeping, it will be interrupted and will not run.
-