public abstract class AbstractLifecycleRunnable extends AbstractRunnable
AbstractLifecycleRunnable
is a service-lifecycle aware AbstractRunnable
.
This simplifies the running and rescheduling of Lifecycle
-based Runnable
s.
Constructor and Description |
---|
AbstractLifecycleRunnable(Lifecycle lifecycle,
org.apache.logging.log4j.Logger logger)
AbstractLifecycleRunnable must be aware of the actual lifecycle to react properly. |
Modifier and Type | Method and Description |
---|---|
protected void |
doRun()
This method has the same semantics as
Runnable.run() |
protected abstract void |
doRunInLifecycle()
Perform runnable logic, but only if the
lifecycle is not stopped or closed. |
void |
onAfter()
This method is called in a finally block after successful execution
or on a rejection.
|
protected void |
onAfterInLifecycle()
This method is invoked in the finally block of the run method, but it is only executed if the
lifecycle is not
stopped or closed. |
isForceExecution, onFailure, onRejection, run
public AbstractLifecycleRunnable(Lifecycle lifecycle, org.apache.logging.log4j.Logger logger)
AbstractLifecycleRunnable
must be aware of the actual lifecycle
to react properly.lifecycle
- The lifecycle to react toologger
- The logger to use when loggingjava.lang.NullPointerException
- if any parameter is null
protected final void doRun() throws java.lang.Exception
Runnable.run()
This invokes doRunInLifecycle()
only if the lifecycle
is not stopped or closed. Otherwise it exits
immediately.
doRun
in class AbstractRunnable
java.lang.InterruptedException
- if the run method throws an InterruptedExceptionjava.lang.Exception
protected abstract void doRunInLifecycle() throws java.lang.Exception
lifecycle
is not stopped or closed.java.lang.InterruptedException
- if the run method throws an InterruptedException
java.lang.Exception
public final void onAfter()
This overrides the default behavior of onAfter
to add the caveat that it only runs if the lifecycle
is not
stopped or closed.
Note: this does not guarantee that it won't be stopped concurrently as it invokes onAfterInLifecycle()
,
but it's a solid attempt at preventing it. For those that use this for rescheduling purposes, the next invocation would be
effectively cancelled immediately if that's the case.
onAfter
in class AbstractRunnable
onAfterInLifecycle()
protected void onAfterInLifecycle()
lifecycle
is not
stopped or closed.
This method is most useful for rescheduling the next iteration of the current runnable.