Class AbstractLifecycleRunnable
- java.lang.Object
-
- org.elasticsearch.common.util.concurrent.AbstractRunnable
-
- org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable
-
- All Implemented Interfaces:
java.lang.Runnable
public abstract class AbstractLifecycleRunnable extends AbstractRunnable
AbstractLifecycleRunnableis a service-lifecycle awareAbstractRunnable.This simplifies the running and rescheduling of
Lifecycle-basedRunnables.
-
-
Constructor Summary
Constructors Constructor Description AbstractLifecycleRunnable(Lifecycle lifecycle, org.apache.logging.log4j.Logger logger)AbstractLifecycleRunnablemust be aware of the actuallifecycleto react properly.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voiddoRun()This method has the same semantics asRunnable.run()protected abstract voiddoRunInLifecycle()Perform runnable logic, but only if thelifecycleis not stopped or closed.voidonAfter()This method is called in a finally block after successful execution or on a rejection.protected voidonAfterInLifecycle()This method is invoked in the finally block of the run method, but it is only executed if thelifecycleis not stopped or closed.-
Methods inherited from class org.elasticsearch.common.util.concurrent.AbstractRunnable
isForceExecution, onFailure, onRejection, run
-
-
-
-
Constructor Detail
-
AbstractLifecycleRunnable
public AbstractLifecycleRunnable(Lifecycle lifecycle, org.apache.logging.log4j.Logger logger)
AbstractLifecycleRunnablemust be aware of the actuallifecycleto react properly.- Parameters:
lifecycle- The lifecycle to react toologger- The logger to use when logging- Throws:
java.lang.NullPointerException- if any parameter isnull
-
-
Method Detail
-
doRun
protected final void doRun() throws java.lang.ExceptionThis method has the same semantics asRunnable.run()This invokes
doRunInLifecycle()only if thelifecycleis not stopped or closed. Otherwise it exits immediately.- Specified by:
doRunin classAbstractRunnable- Throws:
java.lang.InterruptedException- if the run method throws an InterruptedExceptionjava.lang.Exception
-
doRunInLifecycle
protected abstract void doRunInLifecycle() throws java.lang.ExceptionPerform runnable logic, but only if thelifecycleis not stopped or closed.- Throws:
java.lang.InterruptedException- if the run method throws anInterruptedExceptionjava.lang.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
onAfterto add the caveat that it only runs if thelifecycleis 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:
onAfterin 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 thelifecycleis not stopped or closed.This method is most useful for rescheduling the next iteration of the current runnable.
-
-