Class AbstractLifecycleRunnable
java.lang.Object
org.elasticsearch.common.util.concurrent.AbstractRunnable
org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable
- All Implemented Interfaces:
Runnable
AbstractLifecycleRunnable
is a service-lifecycle aware AbstractRunnable
.
This simplifies the running and rescheduling of Lifecycle
-based Runnable
s.
-
Constructor Summary
ConstructorDescriptionAbstractLifecycleRunnable(Lifecycle lifecycle, org.apache.logging.log4j.Logger logger)
AbstractLifecycleRunnable
must be aware of the actuallifecycle
to react properly. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doRun()
This method has the same semantics asRunnable.run()
protected abstract void
Perform runnable logic, but only if thelifecycle
is not stopped or closed.void
onAfter()
This method is called in a finally block after successful execution or on a rejection.protected void
This method is invoked in the finally block of the run method, but it is only executed if thelifecycle
is not stopped or closed.Methods inherited from class org.elasticsearch.common.util.concurrent.AbstractRunnable
isForceExecution, onFailure, onRejection, run
-
Constructor Details
-
AbstractLifecycleRunnable
AbstractLifecycleRunnable
must be aware of the actuallifecycle
to react properly.- Parameters:
lifecycle
- The lifecycle to react toologger
- The logger to use when logging- Throws:
NullPointerException
- if any parameter isnull
-
-
Method Details
-
doRun
This method has the same semantics asRunnable.run()
This invokes
doRunInLifecycle()
only if thelifecycle
is not stopped or closed. Otherwise it exits immediately.- Specified by:
doRun
in classAbstractRunnable
- Throws:
InterruptedException
- if the run method throws an InterruptedExceptionException
-
doRunInLifecycle
Perform runnable logic, but only if thelifecycle
is not stopped or closed.- Throws:
InterruptedException
- if the run method throws anInterruptedException
Exception
-
onAfter
public final void onAfter()This method is called in a finally block after successful execution or on a rejection.This overrides the default behavior of
onAfter
to add the caveat that it only runs if thelifecycle
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.- Overrides:
onAfter
in classAbstractRunnable
- See Also:
onAfterInLifecycle()
-
onAfterInLifecycle
protected void onAfterInLifecycle()This method is invoked in the finally block of the run method, but it is only executed if thelifecycle
is not stopped or closed.This method is most useful for rescheduling the next iteration of the current runnable.
-