T - the type of the startup argument. Use Void if unused.U - the type of the shutdown argument. Use Void if unused.V - the type of the startup result. Use Void if unused.L - the type of the life cycle event listener. Use Void if unused.public abstract class StartStopSupport<T,U,V,L> extends Object implements ListenableAsyncCloseable
| Modifier | Constructor and Description |
|---|---|
protected |
StartStopSupport(Executor executor)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(L listener)
Adds the specified
listener, so that it is notified when the state of this
StartStopSupport changes. |
void |
close()
A synchronous version of
stop(Object). |
CompletableFuture<?> |
closeAsync()
Releases any underlying resources held by this object asynchronously.
|
protected void |
closeFailed(Throwable cause)
Invoked when failed to stop in
close(). |
protected abstract CompletionStage<V> |
doStart(T arg)
Invoked by
start(Object, boolean) to perform the actual startup. |
protected abstract CompletionStage<Void> |
doStop(U arg)
Invoked by
stop(Object) to perform the actual startup, or indirectly by
start(Object, boolean) when startup failed. |
boolean |
isClosed()
Returns whether
AsyncCloseable.close() or AsyncCloseable.closeAsync() operation has been completed. |
boolean |
isClosing()
Returns whether
AsyncCloseable.close() or AsyncCloseable.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.
|
boolean |
removeListener(L listener)
Removes the specified
listener, so that it is not notified anymore. |
protected void |
rollbackFailed(Throwable cause)
Invoked when failed to stop during the rollback after startup failure.
|
CompletableFuture<V> |
start(boolean failIfStarted)
Begins the startup procedure without an argument by calling
doStart(Object), ensuring that
neither doStart(Object) nor doStop(Object) is invoked concurrently. |
CompletableFuture<V> |
start(T arg,
boolean failIfStarted)
Begins the startup procedure without an argument by calling
doStart(Object), ensuring that
neither doStart(Object) nor doStop(Object) is invoked concurrently. |
CompletableFuture<V> |
start(T arg,
U rollbackArg,
boolean failIfStarted)
Begins the startup procedure by calling
doStart(Object), ensuring that neither
doStart(Object) nor doStop(Object) is invoked concurrently. |
CompletableFuture<Void> |
stop()
Begins the shutdown procedure without an argument by calling
doStop(Object), ensuring that
neither doStart(Object) nor doStop(Object) is invoked concurrently. |
CompletableFuture<Void> |
stop(U arg)
Begins the shutdown procedure by calling
doStop(Object), ensuring that neither
doStart(Object) nor doStop(Object) is invoked concurrently. |
String |
toString() |
CompletableFuture<?> |
whenClosed()
Returns the
CompletableFuture which is completed after the AsyncCloseable.close() or
AsyncCloseable.closeAsync() operation is completed. |
protected StartStopSupport(Executor executor)
executor - the Executor which will be used for invoking the extension points of this class:
doStart(Object)doStop(Object)rollbackFailed(Throwable)notificationFailed(Object, Throwable)closeFailed(Throwable) which is invoked at the caller thread.public final void addListener(L listener)
listener, so that it is notified when the state of this
StartStopSupport changes.public final boolean removeListener(L listener)
listener, so that it is not notified anymore.public final CompletableFuture<V> start(boolean failIfStarted)
doStart(Object), ensuring that
neither doStart(Object) nor doStop(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 to
rollbackFailed(Throwable). This method is a shortcut for
start(null, null, failIfStarted).failIfStarted - whether to fail the returned CompletableFuture with
an IllegalStateException when the startup procedure is already
in progress or donepublic final CompletableFuture<V> start(T arg, boolean failIfStarted)
doStart(Object), ensuring that
neither doStart(Object) nor doStop(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 to
rollbackFailed(Throwable). This method is a shortcut for
start(arg, null, failIfStarted).arg - the argument to pass to doStart(Object),
or null to pass no argument.failIfStarted - whether to fail the returned CompletableFuture with
an IllegalStateException when the startup procedure is already
in progress or donepublic final CompletableFuture<V> start(T arg, U rollbackArg, boolean failIfStarted)
doStart(Object), ensuring that neither
doStart(Object) nor doStop(Object) is invoked concurrently. When the startup fails,
stop(Object) will be invoked with the specified rollbackArg automatically to roll back
the side effect caused by this method and any exceptions that occurred during the rollback will be
reported to rollbackFailed(Throwable).arg - the argument to pass to doStart(Object),
or null to pass no argument.rollbackArg - the argument to pass to doStop(Object) when rolling back.failIfStarted - whether to fail the returned CompletableFuture with
an IllegalStateException when the startup procedure is already
in progress or donepublic final CompletableFuture<Void> stop()
doStop(Object), ensuring that
neither doStart(Object) nor doStop(Object) is invoked concurrently. This method is
a shortcut of stop(null).public final CompletableFuture<Void> stop(U arg)
doStop(Object), ensuring that neither
doStart(Object) nor doStop(Object) is invoked concurrently.arg - the argument to pass to doStop(Object), or null to pass no argument.public boolean isClosing()
ListenableAsyncCloseableAsyncCloseable.close() or AsyncCloseable.closeAsync() has been called.isClosing in interface ListenableAsyncCloseableListenableAsyncCloseable.isClosed()public boolean isClosed()
ListenableAsyncCloseableAsyncCloseable.close() or AsyncCloseable.closeAsync() operation has been completed.isClosed in interface ListenableAsyncCloseableListenableAsyncCloseable.isClosing()public CompletableFuture<?> whenClosed()
ListenableAsyncCloseableCompletableFuture which is completed after the AsyncCloseable.close() or
AsyncCloseable.closeAsync() operation is completed.whenClosed in interface ListenableAsyncCloseablepublic CompletableFuture<?> closeAsync()
AsyncCloseablecloseAsync in interface AsyncCloseableCompletableFuture which is completed after the resources are releasedpublic final void close()
stop(Object). Exceptions occurred during shutdown are reported to
closeFailed(Throwable). No argument (i.e. null) is passed.close in interface AsyncCloseableclose in interface AutoCloseableprotected abstract CompletionStage<V> doStart(T arg) throws Exception
start(Object, boolean) to perform the actual startup.arg - the argument passed from start(Object, boolean),
or null if no argument was specified.Exceptionprotected abstract CompletionStage<Void> doStop(U arg) throws Exception
stop(Object) to perform the actual startup, or indirectly by
start(Object, boolean) when startup failed.arg - the argument passed from stop(Object),
or null if no argument was specified.Exceptionprotected void notifyStarting(L listener, T arg) throws Exception
listener - the listenerarg - the argument passed from start(Object, boolean),
or null if no argument was specified.Exceptionprotected void notifyStarted(L listener, T arg, V result) throws Exception
listener - the listenerarg - the argument passed from start(Object, boolean),
or null if no argument was specified.result - the value of the CompletionStage returned by doStart(Object).Exceptionprotected void notifyStopping(L listener, U arg) throws Exception
listener - the listenerarg - the argument passed from stop(Object),
or null if no argument was specified.Exceptionprotected void notifyStopped(L listener, U arg) throws Exception
listener - the listenerarg - the argument passed from stop(Object),
or null if no argument was specified.Exceptionprotected void rollbackFailed(Throwable cause)
protected void notificationFailed(L listener, Throwable cause)
Copyright © 2020 LeanCloud. All rights reserved.