Package io.servicetalk.concurrent.api
Class AsyncCloseables
java.lang.Object
io.servicetalk.concurrent.api.AsyncCloseables
A utility class to create
AsyncCloseable
s.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A resource that can be converted to anAsyncCloseable
. -
Method Summary
Modifier and TypeMethodDescriptionstatic Completable
closeAsyncGracefully
(AsyncCloseable closable, long timeout, TimeUnit timeoutUnit) InvokesAsyncCloseable.closeAsyncGracefully()
on theclosable
, applies a timeout, and if the timeout fires forces a call toAsyncCloseable.closeAsync()
.static ListenableAsyncCloseable
Creates an emptyListenableAsyncCloseable
that does nothing whenAsyncCloseable.closeAsync()
apart from completing theListenableAsyncCloseable.onClose()
.static CompositeCloseable
Creates a newCompositeCloseable
.static ListenableAsyncCloseable
toAsyncCloseable
(AsyncCloseables.CloseableResource closeableResource) Creates a newListenableAsyncCloseable
which uses the passedSupplier
to get the implementation of close.static ListenableAsyncCloseable
toListenableAsyncCloseable
(AsyncCloseable asyncCloseable) Wraps the passedAsyncCloseable
and creates a newListenableAsyncCloseable
.static ListenableAsyncCloseable
toListenableAsyncCloseable
(AsyncCloseable asyncCloseable, Function<Completable, Completable> onCloseDecorator) Wraps the passedAsyncCloseable
and creates a newListenableAsyncCloseable
.
-
Method Details
-
closeAsyncGracefully
public static Completable closeAsyncGracefully(AsyncCloseable closable, long timeout, TimeUnit timeoutUnit) InvokesAsyncCloseable.closeAsyncGracefully()
on theclosable
, applies a timeout, and if the timeout fires forces a call toAsyncCloseable.closeAsync()
.- Parameters:
closable
- TheAsyncCloseable
to initiateAsyncCloseable.closeAsyncGracefully()
on.timeout
- The timeout duration to wait forAsyncCloseable.closeAsyncGracefully()
to complete.timeoutUnit
- The time unit applied totimeout
.- Returns:
- A
Completable
that is notified once the close is complete.
-
emptyAsyncCloseable
Creates an emptyListenableAsyncCloseable
that does nothing whenAsyncCloseable.closeAsync()
apart from completing theListenableAsyncCloseable.onClose()
.- Returns:
- A new
ListenableAsyncCloseable
.
-
toListenableAsyncCloseable
Wraps the passedAsyncCloseable
and creates a newListenableAsyncCloseable
. This method owns the passedAsyncCloseable
after this method returns and hence it should not be used by the caller after this method returns.- Parameters:
asyncCloseable
-AsyncCloseable
to convert toListenableAsyncCloseable
.- Returns:
- A new
ListenableAsyncCloseable
.
-
toListenableAsyncCloseable
public static ListenableAsyncCloseable toListenableAsyncCloseable(AsyncCloseable asyncCloseable, Function<Completable, Completable> onCloseDecorator) Wraps the passedAsyncCloseable
and creates a newListenableAsyncCloseable
. This method owns the passedAsyncCloseable
after this method returns and hence it should not be used by the caller after this method returns.- Parameters:
asyncCloseable
-AsyncCloseable
to convert toListenableAsyncCloseable
.onCloseDecorator
-Function
that can decorate theCompletable
returned from the returnedListenableAsyncCloseable.onClose()
.- Returns:
- A new
ListenableAsyncCloseable
.
-
toAsyncCloseable
public static ListenableAsyncCloseable toAsyncCloseable(AsyncCloseables.CloseableResource closeableResource) Creates a newListenableAsyncCloseable
which uses the passedSupplier
to get the implementation of close.- Parameters:
closeableResource
-AsyncCloseables.CloseableResource
that is to be wrapped into aListenableAsyncCloseable
.AsyncCloseables.CloseableResource.doClose(boolean)
will be called when the returnedListenableAsyncCloseable
isclosed
orgracefully closed
for the first time.- Returns:
- A new
ListenableAsyncCloseable
.
-
newCompositeCloseable
Creates a newCompositeCloseable
.- Returns:
- A new
CompositeCloseable
.
-