Interface ChannelGroupFuture
-
- All Superinterfaces:
Future<java.lang.Void>
,java.util.concurrent.Future<java.lang.Void>
,java.lang.Iterable<ChannelFuture>
public interface ChannelGroupFuture extends Future<java.lang.Void>, java.lang.Iterable<ChannelFuture>
The result of an asynchronousChannelGroup
operation.ChannelGroupFuture
is composed ofChannelFuture
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(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 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,
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()
insideChannelHandler
The event handler methods in
ChannelHandler
is 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
@Override
public void messageReceived(ChannelHandlerContext
ctx, ShutdownMessage msg) {ChannelGroup
allChannels = MyServer.getAllChannels();ChannelGroupFuture
future = allChannels.close(); future.awaitUninterruptibly(); // 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
await()
. In such a case, please make sure you do not callawait()
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(GenericFutureListener<? extends Future<? super java.lang.Void>> listener)
Adds the specified listener to this future.ChannelGroupFuture
addListeners(GenericFutureListener<? extends Future<? super java.lang.Void>>... listeners)
Adds the specified listeners to this future.ChannelGroupFuture
await()
Waits for this future to be completed.ChannelGroupFuture
awaitUninterruptibly()
Waits for this future to be completed without interruption.ChannelGroupException
cause()
Returns the cause of the failed I/O operation if the I/O operation has failed.ChannelFuture
find(Channel channel)
Returns theChannelFuture
of the individual I/O operation which is associated with the specifiedChannel
.ChannelGroup
group()
Returns theChannelGroup
which is associated with this future.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.java.util.Iterator<ChannelFuture>
iterator()
Returns theIterator
that enumerates allChannelFuture
s which are associated with this future.ChannelGroupFuture
removeListener(GenericFutureListener<? extends Future<? super java.lang.Void>> listener)
Removes the first occurrence of the specified listener from this future.ChannelGroupFuture
removeListeners(GenericFutureListener<? extends Future<? super java.lang.Void>>... listeners)
Removes the first occurrence for each of the listeners from this future.ChannelGroupFuture
sync()
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.ChannelGroupFuture
syncUninterruptibly()
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.-
Methods inherited from interface io.netty.util.concurrent.Future
await, await, awaitUninterruptibly, awaitUninterruptibly, cancel, getNow, isCancellable
-
-
-
-
Method Detail
-
group
ChannelGroup group()
Returns theChannelGroup
which is associated with this future.
-
find
ChannelFuture find(Channel channel)
Returns theChannelFuture
of the individual I/O operation which is associated with the specifiedChannel
.- Returns:
- the matching
ChannelFuture
if found.null
otherwise.
-
isSuccess
boolean isSuccess()
Returnstrue
if and only if all I/O operations associated with this future were successful without any failure.
-
cause
ChannelGroupException cause()
Description copied from interface:Future
Returns the cause of the failed I/O operation if the I/O operation has failed.
-
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(GenericFutureListener<? extends Future<? super java.lang.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<java.lang.Void>
-
addListeners
ChannelGroupFuture addListeners(GenericFutureListener<? extends Future<? super java.lang.Void>>... listeners)
Description copied from interface:Future
Adds the specified listeners to this future. The specified listeners are notified when this future is done. If this future is already completed, the specified listeners are notified immediately.- Specified by:
addListeners
in interfaceFuture<java.lang.Void>
-
removeListener
ChannelGroupFuture removeListener(GenericFutureListener<? extends Future<? super java.lang.Void>> listener)
Description copied from interface:Future
Removes the first occurrence of the specified listener from this future. The specified listener is no longer notified when this future is done. If the specified listener is not associated with this future, this method does nothing and returns silently.- Specified by:
removeListener
in interfaceFuture<java.lang.Void>
-
removeListeners
ChannelGroupFuture removeListeners(GenericFutureListener<? extends Future<? super java.lang.Void>>... listeners)
Description copied from interface:Future
Removes the first occurrence for each of the listeners from this future. The specified listeners are no longer notified when this future is done. If the specified listeners are not associated with this future, this method does nothing and returns silently.- Specified by:
removeListeners
in interfaceFuture<java.lang.Void>
-
await
ChannelGroupFuture await() throws java.lang.InterruptedException
Description copied from interface:Future
Waits for this future to be completed.
-
awaitUninterruptibly
ChannelGroupFuture awaitUninterruptibly()
Description copied from interface:Future
Waits for this future to be completed without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceFuture<java.lang.Void>
-
syncUninterruptibly
ChannelGroupFuture syncUninterruptibly()
Description copied from interface:Future
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.- Specified by:
syncUninterruptibly
in interfaceFuture<java.lang.Void>
-
sync
ChannelGroupFuture sync() throws java.lang.InterruptedException
Description copied from interface:Future
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.
-
iterator
java.util.Iterator<ChannelFuture> iterator()
Returns theIterator
that enumerates allChannelFuture
s which are associated with this future. Please note that the returnedIterator
is unmodifiable, which means aChannelFuture
cannot be removed from this future.- Specified by:
iterator
in interfacejava.lang.Iterable<ChannelFuture>
-
-