Interface ChannelGroupFuture
-
- All Superinterfaces:
io.netty.util.concurrent.Future<Void>,Future<Void>,Iterable<ChannelFuture>
public interface ChannelGroupFuture extends io.netty.util.concurrent.Future<Void>, Iterable<ChannelFuture>
The result of an asynchronousChannelGroupoperation.ChannelGroupFutureis composed ofChannelFutures which represent the outcome of the individual I/O operations that affect theChannels in theChannelGroup.All I/O operations in
ChannelGroupare 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 aChannelGroupFutureinstance 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
ChannelGroupFutureListenerso you can get notified when the I/O operation have been completed.Prefer
It is recommended to preferaddListener(GenericFutureListener)toawait()addListener(GenericFutureListener)toawait()wherever possible to get notified when I/O operations are done and to do any follow-up tasks.addListener(GenericFutureListener)is non-blocking. It simply adds the specifiedChannelGroupFutureListenerto theChannelGroupFuture, and I/O thread will notify the listeners when the I/O operations associated with the future is done.ChannelGroupFutureListeneryields 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 withawait(), 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()insideChannelHandlerThe event handler methods in
ChannelHandleris often called by an I/O thread. Ifawait()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 becauseawait()can block the I/O operation it is waiting for, which is a dead lock.// BAD - NEVER DO THIS
@Overridepublic void messageReceived(ChannelHandlerContextctx, ShutdownMessage msg) {ChannelGroupallChannels = MyServer.getAllChannels();ChannelGroupFuturefuture = allChannels.close(); future.awaitUninterruptibly(); // 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
await(). In such a case, please make sure you do not callawait()in an I/O thread. Otherwise,IllegalStateExceptionwill be raised to prevent a dead lock.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ChannelGroupFutureaddListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>> listener)ChannelGroupFutureaddListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>>... listeners)ChannelGroupFutureawait()ChannelGroupFutureawaitUninterruptibly()ChannelGroupExceptioncause()ChannelFuturefind(Channel channel)Returns theChannelFutureof the individual I/O operation which is associated with the specifiedChannel.ChannelGroupgroup()Returns theChannelGroupwhich is associated with this future.booleanisPartialFailure()Returnstrueif and only if the I/O operations associated with this future have failed partially with some success.booleanisPartialSuccess()Returnstrueif and only if the I/O operations associated with this future were partially successful with some failure.booleanisSuccess()Returnstrueif and only if all I/O operations associated with this future were successful without any failure.Iterator<ChannelFuture>iterator()Returns theIteratorthat enumerates allChannelFutures which are associated with this future.ChannelGroupFutureremoveListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>> listener)ChannelGroupFutureremoveListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>>... listeners)ChannelGroupFuturesync()ChannelGroupFuturesyncUninterruptibly()-
Methods inherited from interface io.netty.util.concurrent.Future
await, await, awaitUninterruptibly, awaitUninterruptibly, cancel, getNow, isCancellable
-
Methods inherited from interface java.util.concurrent.Future
get, get, isCancelled, isDone
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
group
ChannelGroup group()
Returns theChannelGroupwhich is associated with this future.
-
find
ChannelFuture find(Channel channel)
Returns theChannelFutureof the individual I/O operation which is associated with the specifiedChannel.- Returns:
- the matching
ChannelFutureif found.nullotherwise.
-
isSuccess
boolean isSuccess()
Returnstrueif and only if all I/O operations associated with this future were successful without any failure.- Specified by:
isSuccessin interfaceio.netty.util.concurrent.Future<Void>
-
cause
ChannelGroupException cause()
- Specified by:
causein interfaceio.netty.util.concurrent.Future<Void>
-
isPartialSuccess
boolean isPartialSuccess()
Returnstrueif and only if the I/O operations associated with this future were partially successful with some failure.
-
isPartialFailure
boolean isPartialFailure()
Returnstrueif and only if the I/O operations associated with this future have failed partially with some success.
-
addListener
ChannelGroupFuture addListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>> listener)
- Specified by:
addListenerin interfaceio.netty.util.concurrent.Future<Void>
-
addListeners
ChannelGroupFuture addListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>>... listeners)
- Specified by:
addListenersin interfaceio.netty.util.concurrent.Future<Void>
-
removeListener
ChannelGroupFuture removeListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>> listener)
- Specified by:
removeListenerin interfaceio.netty.util.concurrent.Future<Void>
-
removeListeners
ChannelGroupFuture removeListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super Void>>... listeners)
- Specified by:
removeListenersin interfaceio.netty.util.concurrent.Future<Void>
-
await
ChannelGroupFuture await() throws InterruptedException
- Specified by:
awaitin interfaceio.netty.util.concurrent.Future<Void>- Throws:
InterruptedException
-
awaitUninterruptibly
ChannelGroupFuture awaitUninterruptibly()
- Specified by:
awaitUninterruptiblyin interfaceio.netty.util.concurrent.Future<Void>
-
syncUninterruptibly
ChannelGroupFuture syncUninterruptibly()
- Specified by:
syncUninterruptiblyin interfaceio.netty.util.concurrent.Future<Void>
-
sync
ChannelGroupFuture sync() throws InterruptedException
- Specified by:
syncin interfaceio.netty.util.concurrent.Future<Void>- Throws:
InterruptedException
-
iterator
Iterator<ChannelFuture> iterator()
Returns theIteratorthat enumerates allChannelFutures which are associated with this future. Please note that the returnedIteratoris unmodifiable, which means aChannelFuturecannot be removed from this future.- Specified by:
iteratorin interfaceIterable<ChannelFuture>
-
-