ChannelGroup operation.
ChannelGroupFuture is composed of Futures which
represent the outcome of the individual I/O operations that affect the
Channels in the ChannelGroup.
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 a ChannelGroupFuture 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.
addListener(FutureListener) to FutureCompletionStage#await()addListener(FutureListener) to
FutureCompletionStage#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 specified ChannelGroupFutureListener to the
ChannelGroupFuture, 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 with FutureCompletionStage#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.
FutureCompletionStage#await() inside ChannelHandler
The event handler methods in ChannelHandler is often called by
an I/O thread. If FutureCompletionStage#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 because FutureCompletionStage#await() can block the I/O
operation it is waiting for, which is a deadlock.
// BAD - NEVER DO THIS@Overridepublic void messageReceived(ChannelHandlerContextctx, ShutdownMessage msg) {ChannelGroupallChannels = MyServer.getAllChannels();ChannelGroupFuturefuture = allChannels.close(); future.asStage().await(); // Perform post-shutdown operation // ... } // GOOD@Overridepublic void messageReceived(ChannelHandlerContext ctx, ShutdownMessage msg) {ChannelGroupallChannels = MyServer.getAllChannels();ChannelGroupFuturefuture = allChannels.close(); future.addListener(newChannelGroupFutureListener() { public void operationComplete(ChannelGroupFuturefuture) { // 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 call FutureCompletionStage#await() in an I/O thread. Otherwise,
IllegalStateException will be raised to prevent a dead lock.
| Modifier and Type | Method and Description |
|---|---|
| public ChannelGroupFuture | addListener(FutureListener<? super Void>
The listener to be called when this future completes. The listener will be passed this future as
an argument. listener)Redeclares io. |
| public ChannelGroupException | cause()
Redeclares io. |
| public Future | |
| public ChannelGroup | |
| public boolean | isPartialFailure()
Returns |
| public boolean | isPartialSuccess()
Returns |
| public boolean | isSuccess()
Redeclares io. true if and only if all I/O operations associated with
this future were successful without any failure.
|
| public Iterator | iterator()
Redeclares java. Iterator that enumerates all Futures
which are associated with this future.
|
| addListener | back to summary |
|---|---|
| public ChannelGroupFuture addListener(FutureListener<? super Void> listener) Redeclares io. Doc from io. 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.
| |
| cause | back to summary |
|---|---|
| public ChannelGroupException cause() Redeclares io. Doc from io. Returns the cause of the failed operation if the operation has failed.
| |
| find | back to summary |
|---|---|
| public Future Returns the | |
| group | back to summary |
|---|---|
| public ChannelGroup group() Returns the | |
| isPartialFailure | back to summary |
|---|---|
| public boolean isPartialFailure() Returns | |
| isPartialSuccess | back to summary |
|---|---|
| public boolean isPartialSuccess() Returns | |
| isSuccess | back to summary |
|---|---|
| public boolean isSuccess() Redeclares io. Returns
| |
| iterator | back to summary |
|---|---|
| public Iterator Redeclares java. Returns the
| |