Interface ChannelGroupFuture
- All Superinterfaces:
Iterable<ChannelFuture>
- All Known Implementing Classes:
DefaultChannelGroupFuture
ChannelGroup operation.
ChannelGroupFuture is composed of ChannelFutures 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.
Prefer addListener(ChannelGroupFutureListener) to await()
It is recommended to prefer addListener(ChannelGroupFutureListener) to
await() wherever possible to get notified when I/O operations are
done and to do any follow-up tasks.
addListener(ChannelGroupFutureListener) 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, 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 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 await() inside ChannelHandler
The event handler methods in ChannelHandler is often called by
an I/O thread unless an ExecutionHandler is in the
ChannelPipeline. If 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 await() can block the I/O
operation it is waiting for, which is a dead lock.
// BAD - NEVER DO THIS@Overridepublic void messageReceived(ChannelHandlerContextctx,MessageEvente) { if (e.getMessage() instanceof ShutdownMessage) {ChannelGroupallChannels = MyServer.getAllChannels();ChannelGroupFuturefuture = allChannels.close(); future.awaitUninterruptibly(); // Perform post-shutdown operation // ... } } // GOOD@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (e.getMessage() instanceof ShutdownMessage) {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 await(). In such a case, please
make sure you do not call await() in an I/O thread. Otherwise,
IllegalStateException will be raised to prevent a dead lock.
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(ChannelGroupFutureListener listener) Adds the specified listener to this future.await()Waits for this future to be completed.booleanawait(long timeoutMillis) Waits for this future to be completed within the specified time limit.booleanWaits for this future to be completed within the specified time limit.Waits for this future to be completed without interruption.booleanawaitUninterruptibly(long timeoutMillis) Waits for this future to be completed within the specified time limit without interruption.booleanawaitUninterruptibly(long timeout, TimeUnit unit) Waits for this future to be completed within the specified time limit without interruption.Returns theChannelFutureof the individual I/O operation which is associated with theChannelwhose ID matches the specified integer.Returns theChannelFutureof the individual I/O operation which is associated with the specifiedChannel.getGroup()Returns theChannelGroupwhich is associated with this future.booleanReturnstrueif and only if all I/O operations associated with this future have failed without any success.booleanReturnstrueif and only if all I/O operations associated with this future were successful without any failure.booleanisDone()Returnstrueif and only if this future is complete, regardless of whether the operation was successful, failed, or canceled.booleanReturnstrueif and only if the I/O operations associated with this future have failed partially with some success.booleanReturnstrueif and only if the I/O operations associated with this future were partially successful with some failure.iterator()Returns theIteratorthat enumerates allChannelFutures which are associated with this future.voidremoveListener(ChannelGroupFutureListener listener) Removes the specified listener from this future.Methods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
getGroup
ChannelGroup getGroup()Returns theChannelGroupwhich is associated with this future. -
find
Returns theChannelFutureof the individual I/O operation which is associated with theChannelwhose ID matches the specified integer.- Returns:
- the matching
ChannelFutureif found.nullotherwise.
-
find
Returns theChannelFutureof the individual I/O operation which is associated with the specifiedChannel.- Returns:
- the matching
ChannelFutureif found.nullotherwise.
-
isDone
boolean isDone()Returnstrueif and only if this future is complete, regardless of whether the operation was successful, failed, or canceled. -
isCompleteSuccess
boolean isCompleteSuccess()Returnstrueif and only if all I/O operations associated with this future were successful without any failure. -
isPartialSuccess
boolean isPartialSuccess()Returnstrueif and only if the I/O operations associated with this future were partially successful with some failure. -
isCompleteFailure
boolean isCompleteFailure()Returnstrueif and only if all I/O operations associated with this future have failed without any success. -
isPartialFailure
boolean isPartialFailure()Returnstrueif and only if the I/O operations associated with this future have failed partially with some success. -
addListener
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. -
removeListener
Removes the specified listener from this future. The specified listener is no longer notified when this future is done. If this future is already completed, this method has no effect and returns silently. -
await
Waits for this future to be completed.- Throws:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
ChannelGroupFuture awaitUninterruptibly()Waits for this future to be completed without interruption. This method catches anInterruptedExceptionand discards it silently. -
await
Waits for this future to be completed within the specified time limit.- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
InterruptedException- if the current thread was interrupted
-
await
Waits for this future to be completed within the specified time limit.- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Returns:
trueif and only if the future was completed within the specified time limit
-
awaitUninterruptibly
boolean awaitUninterruptibly(long timeoutMillis) Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Returns:
trueif and only if the future was completed within the specified time limit
-
iterator
Iterator<ChannelFuture> iterator()Returns theIteratorthat enumerates allChannelFutures which are associated with this future. Please note that the returnedIteratoris is unmodifiable, which means aChannelFuturecannot be removed from this future.- Specified by:
iteratorin interfaceIterable<ChannelFuture>
-