-
public interface ChannelGroupFuture extends Future<Void>, Iterable<Future<Void>>
The result of an asynchronousChannelGroup
operation.ChannelGroupFuture
is composed ofFuture
s which represent the outcome of the individual I/O operations that affect theChannel
s in theChannelGroup
.All I/O operations in
ChannelGroup
are asynchronous. It means any I/O calls will return immediately with no guarantee that the requested I/O operations have been completed at the end of the call. Instead, you will be returned with aChannelGroupFuture
instance which tells you when the requested I/O operations have succeeded, failed, or cancelled.Various methods are provided to let you check if the I/O operations has been completed, wait for the completion, and retrieve the result of the I/O operation. It also allows you to add more than one
ChannelGroupFutureListener
so you can get notified when the I/O operation have been completed.Prefer
It is recommended to preferaddListener(FutureListener)
toFutureCompletionStage.await()
addListener(FutureListener)
toFutureCompletionStage.await()
wherever possible to get notified when I/O operations are done and to do any follow-up tasks.addListener(FutureListener)
is non-blocking. It simply adds the specifiedChannelGroupFutureListener
to theChannelGroupFuture
, and I/O thread will notify the listeners when the I/O operations associated with the future is done.ChannelGroupFutureListener
yields the best performance and resource utilization because it does not block at all, but it could be tricky to implement a sequential logic if you are not used to event-driven programming.By contrast,
FutureCompletionStage.await()
is a blocking operation. Once called, the caller thread blocks until all I/O operations are done. It is easier to implement a sequential logic withFutureCompletionStage.await()
, but the caller thread blocks unnecessarily until all I/O operations are done and there's relatively expensive cost of inter-thread notification. Moreover, there's a chance of dead lock in a particular circumstance, which is described below.Do not call
FutureCompletionStage.await()
insideChannelHandler
The event handler methods in
ChannelHandler
is often called by an I/O thread. IfFutureCompletionStage.await()
is called by an event handler method, which is called by the I/O thread, the I/O operation it is waiting for might never be complete becauseFutureCompletionStage.await()
can block the I/O operation it is waiting for, which is a deadlock.// BAD - NEVER DO THIS
@Override
public void messageReceived(ChannelHandlerContext
ctx, ShutdownMessage msg) {ChannelGroup
allChannels = MyServer.getAllChannels();ChannelGroupFuture
future = allChannels.close(); future.asStage().await(); // Perform post-shutdown operation // ... } // GOOD@Override
public void messageReceived(ChannelHandlerContext ctx, ShutdownMessage msg) {ChannelGroup
allChannels = MyServer.getAllChannels();ChannelGroupFuture
future = allChannels.close(); future.addListener(newChannelGroupFutureListener
() { public void operationComplete(ChannelGroupFuture
future) { // Perform post-closure operation // ... } }); }In spite of the disadvantages mentioned above, there are certainly the cases where it is more convenient to call
FutureCompletionStage.await()
. In such a case, please make sure you do not callFutureCompletionStage.await()
in an I/O thread. Otherwise,IllegalStateException
will be raised to prevent a dead lock.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ChannelGroupFuture
addListener(FutureListener<? super Void> listener)
Adds the specified listener to this future.boolean
cancel()
Cancel this asynchronous operation, unless it has already been completed or is not cancellable.ChannelGroupException
cause()
EventExecutor
executor()
Future<Void>
find(Channel channel)
V
getNow()
Return the successful result of this asynchronous operation, if any.ChannelGroup
group()
Returns theChannelGroup
which is associated with this future.boolean
isCancellable()
Returnstrue
if and only if the operation can be cancelled viacancel()
.boolean
isCancelled()
Returntrue
if this operation has been cancelled.boolean
isDone()
Returntrue
if this operation has been completed either successfully, unsuccessfully, or through cancellation.boolean
isFailed()
Returnstrue
if and only if the operation was completed and failed.boolean
isPartialFailure()
Returnstrue
if and only if the I/O operations associated with this future have failed partially with some success.boolean
isPartialSuccess()
Returnstrue
if and only if the I/O operations associated with this future were partially successful with some failure.boolean
isSuccess()
Returnstrue
if and only if all I/O operations associated with this future were successful without any failure.Iterator<Future<Void>>
iterator()
-
Methods inherited from interface io.netty5.util.concurrent.Future
addListener, asStage, cascadeTo, flatMap, map
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
group
ChannelGroup group()
Returns theChannelGroup
which is associated with this future.
-
isSuccess
boolean isSuccess()
Returnstrue
if and only if all I/O operations associated with this future were successful without any failure.
-
cause
ChannelGroupException cause()
-
isPartialSuccess
boolean isPartialSuccess()
Returnstrue
if and only if the I/O operations associated with this future were partially successful with some failure.
-
isPartialFailure
boolean isPartialFailure()
Returnstrue
if and only if the I/O operations associated with this future have failed partially with some success.
-
addListener
ChannelGroupFuture addListener(FutureListener<? super Void> listener)
Description copied from interface:Future
Adds the specified listener to this future. The specified listener is notified when this future is done. If this future is already completed, the specified listener is notified immediately.- Specified by:
addListener
in interfaceFuture<Void>
- Parameters:
listener
- The listener to be called when this future completes. The listener will be passed this future as an argument.- Returns:
- this future object.
-
cancel
boolean cancel()
Cancel this asynchronous operation, unless it has already been completed or is not cancellable.A cancelled operation is considered to be done and failed.
If the cancellation was successful, the result of this operation will be that it has failed with a
CancellationException
.Cancellation will not cause any threads working on the operation to be interrupted.
- Returns:
true
if the operation was cancelled by this call, otherwisefalse
.
-
isFailed
boolean isFailed()
Returnstrue
if and only if the operation was completed and failed.
-
isCancelled
boolean isCancelled()
Returntrue
if this operation has been cancelled.- Returns:
true
if this operation has been cancelled, otherwisefalse
.
-
isDone
boolean isDone()
Returntrue
if this operation has been completed either successfully, unsuccessfully, or through cancellation.- Returns:
true
if this operation has completed, otherwisefalse
.
-
isCancellable
boolean isCancellable()
Returnstrue
if and only if the operation can be cancelled viacancel()
. Note that this is inherently racy, as the operation could be made uncancellable at any time.- Returns:
true
if this operation can be cancelled.
-
getNow
V getNow()
Return the successful result of this asynchronous operation, if any. If the operation has not yet been completed, then this will throwIllegalStateException
. If the operation has been cancelled or failed with an exception, then this returnsnull
. Note that asynchronous operations can also be completed successfully with anull
result.- Returns:
- the result of this operation, if completed successfully.
- Throws:
IllegalStateException
- if thisFuture
orPromise
has not completed yet.
-
executor
EventExecutor executor()
-
-