Package com.linecorp.armeria.common.util
Class StartStopSupport<T,U,V,L>
java.lang.Object
com.linecorp.armeria.common.util.StartStopSupport<T,U,V,L>
- Type Parameters:
T
- the type of the startup argument. UseVoid
if unused.U
- the type of the shutdown argument. UseVoid
if unused.V
- the type of the startup result. UseVoid
if unused.L
- the type of the life cycle event listener. UseVoid
if unused.
- All Implemented Interfaces:
AsyncCloseable
,ListenableAsyncCloseable
,AutoCloseable
Provides asynchronous start-stop life cycle support.
-
Constructor Summary
ModifierConstructorDescriptionprotected
StartStopSupport
(Executor executor) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addListener
(L listener) Adds the specifiedlistener
, so that it is notified when the state of thisStartStopSupport
changes.final void
close()
A synchronous version ofstop(Object)
.final CompletableFuture<?>
Releases any underlying resources held by this object asynchronously.protected void
closeFailed
(Throwable cause) Invoked when failed to stop inclose()
.protected abstract CompletionStage<V>
Invoked bystart(Object, boolean)
to perform the actual startup.protected abstract CompletionStage<Void>
Invoked bystop(Object)
to perform the actual startup, or indirectly bystart(Object, boolean)
when startup failed.final boolean
isClosed()
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation has been completed.final boolean
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
has been called.protected void
notificationFailed
(L listener, Throwable cause) Invoked when an event listener raises an exception.protected void
notifyStarted
(L listener, T arg, V result) Invoked when the startup procedure is finished.protected void
notifyStarting
(L listener, T arg) Invoked when the startup procedure begins.protected void
notifyStopped
(L listener, U arg) Invoked when the shutdown procedure is finished.protected void
notifyStopping
(L listener, U arg) Invoked when the shutdown procedure begins.final boolean
removeListener
(L listener) Removes the specifiedlistener
, so that it is not notified anymore.protected void
rollbackFailed
(Throwable cause) Invoked when failed to stop during the rollback after startup failure.final CompletableFuture<V>
start
(boolean failIfStarted) Begins the startup procedure without an argument by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.final CompletableFuture<V>
Begins the startup procedure without an argument by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.final CompletableFuture<V>
Begins the startup procedure by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.final CompletableFuture<Void>
stop()
Begins the shutdown procedure without an argument by callingdoStop(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.final CompletableFuture<Void>
Begins the shutdown procedure by callingdoStop(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.toString()
final CompletableFuture<?>
Returns theCompletableFuture
which is completed after theAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation is completed.
-
Constructor Details
-
StartStopSupport
Creates a new instance.- Parameters:
executor
- theExecutor
which will be used for invoking the extension points of this class:doStart(Object)
doStop(Object)
rollbackFailed(Throwable)
notificationFailed(Object, Throwable)
- All listener notifications
closeFailed(Throwable)
which is invoked at the caller thread.
-
-
Method Details
-
addListener
Adds the specifiedlistener
, so that it is notified when the state of thisStartStopSupport
changes. -
removeListener
Removes the specifiedlistener
, so that it is not notified anymore. -
start
Begins the startup procedure without an argument by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently. When the startup fails,stop()
will be invoked automatically to roll back the side effect caused by this method and any exceptions that occurred during the rollback will be reported torollbackFailed(Throwable)
. This method is a shortcut forstart(null, null, failIfStarted)
.- Parameters:
failIfStarted
- whether to fail the returnedCompletableFuture
with anIllegalStateException
when the startup procedure is already in progress or done
-
start
Begins the startup procedure without an argument by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently. When the startup fails,stop()
will be invoked automatically to roll back the side effect caused by this method and any exceptions that occurred during the rollback will be reported torollbackFailed(Throwable)
. This method is a shortcut forstart(arg, null, failIfStarted)
.- Parameters:
arg
- the argument to pass todoStart(Object)
, ornull
to pass no argument.failIfStarted
- whether to fail the returnedCompletableFuture
with anIllegalStateException
when the startup procedure is already in progress or done
-
start
public final CompletableFuture<V> start(@Nullable T arg, @Nullable U rollbackArg, boolean failIfStarted) Begins the startup procedure by callingdoStart(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently. When the startup fails,stop(Object)
will be invoked with the specifiedrollbackArg
automatically to roll back the side effect caused by this method and any exceptions that occurred during the rollback will be reported torollbackFailed(Throwable)
.- Parameters:
arg
- the argument to pass todoStart(Object)
, ornull
to pass no argument.rollbackArg
- the argument to pass todoStop(Object)
when rolling back.failIfStarted
- whether to fail the returnedCompletableFuture
with anIllegalStateException
when the startup procedure is already in progress or done
-
stop
Begins the shutdown procedure without an argument by callingdoStop(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently. This method is a shortcut ofstop(null)
. -
stop
Begins the shutdown procedure by callingdoStop(Object)
, ensuring that neitherdoStart(Object)
nordoStop(Object)
is invoked concurrently.- Parameters:
arg
- the argument to pass todoStop(Object)
, ornull
to pass no argument.
-
isClosing
public final boolean isClosing()Description copied from interface:ListenableAsyncCloseable
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
has been called.- Specified by:
isClosing
in interfaceListenableAsyncCloseable
- See Also:
-
isClosed
public final boolean isClosed()Description copied from interface:ListenableAsyncCloseable
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation has been completed.- Specified by:
isClosed
in interfaceListenableAsyncCloseable
- See Also:
-
whenClosed
Description copied from interface:ListenableAsyncCloseable
Returns theCompletableFuture
which is completed after theAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation is completed.- Specified by:
whenClosed
in interfaceListenableAsyncCloseable
-
closeAsync
Description copied from interface:AsyncCloseable
Releases any underlying resources held by this object asynchronously.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Returns:
- the
CompletableFuture
which is completed after the resources are released
-
close
public final void close()A synchronous version ofstop(Object)
. Exceptions occurred during shutdown are reported tocloseFailed(Throwable)
. No argument (i.e.null
) is passed.- Specified by:
close
in interfaceAsyncCloseable
- Specified by:
close
in interfaceAutoCloseable
-
doStart
Invoked bystart(Object, boolean)
to perform the actual startup.- Parameters:
arg
- the argument passed fromstart(Object, boolean)
, ornull
if no argument was specified.- Throws:
Exception
-
doStop
Invoked bystop(Object)
to perform the actual startup, or indirectly bystart(Object, boolean)
when startup failed.- Parameters:
arg
- the argument passed fromstop(Object)
, ornull
if no argument was specified.- Throws:
Exception
-
notifyStarting
Invoked when the startup procedure begins. Note that the startup procedure will be aborted if an exception is thrown.- Parameters:
listener
- the listenerarg
- the argument passed fromstart(Object, boolean)
, ornull
if no argument was specified.- Throws:
Exception
-
notifyStarted
Invoked when the startup procedure is finished.- Parameters:
listener
- the listenerarg
- the argument passed fromstart(Object, boolean)
, ornull
if no argument was specified.result
- the value of theCompletionStage
returned bydoStart(Object)
.- Throws:
Exception
-
notifyStopping
Invoked when the shutdown procedure begins.- Parameters:
listener
- the listenerarg
- the argument passed fromstop(Object)
, ornull
if no argument was specified.- Throws:
Exception
-
notifyStopped
Invoked when the shutdown procedure is finished.- Parameters:
listener
- the listenerarg
- the argument passed fromstop(Object)
, ornull
if no argument was specified.- Throws:
Exception
-
rollbackFailed
Invoked when failed to stop during the rollback after startup failure. -
notificationFailed
Invoked when an event listener raises an exception. -
closeFailed
Invoked when failed to stop inclose()
. -
toString
-