Top Description Methods
io.netty.channel.group

public Interface ChannelGroupFuture

extends Future<Void>, Iterable<ChannelFuture>
Known Direct Implementers
io.netty.channel.group.DefaultChannelGroupFuture, io.netty.channel.group.VoidChannelGroupFuture
Imports
io.netty.channel.Channel, .ChannelFuture, .ChannelHandler, .ChannelHandlerContext, io.netty.util.concurrent.Future, .GenericFutureListener, java.util.Iterator

The result of an asynchronous 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(GenericFutureListener) to await()

It is recommended to prefer addListener(GenericFutureListener) to await() 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 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. 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
@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(new ChannelGroupFutureListener() {
        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 call await() in an I/O thread. Otherwise, IllegalStateException will be raised to prevent a dead lock.

Method Summary

Modifier and TypeMethod and Description
public ChannelGroupFuture
addListener(GenericFutureListener<? extends Future<? super Void>> listener)

Redeclares io.netty.util.concurrent.Future.addListener.

Adds the specified listener to this future.
public ChannelGroupFuture
addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners)

Redeclares io.netty.util.concurrent.Future.addListeners.

Adds the specified listeners to this future.
public ChannelGroupFuture
await()

Redeclares io.netty.util.concurrent.Future.await.

Waits for this future to be completed.
public ChannelGroupFuture
awaitUninterruptibly()

Redeclares io.netty.util.concurrent.Future.awaitUninterruptibly.

Waits for this future to be completed without interruption.
public ChannelGroupException
cause()

Redeclares io.netty.util.concurrent.Future.cause.

Returns the cause of the failed I/O operation if the I/O operation has failed.
public ChannelFuture

Returns:

the matching ChannelFuture if found. null otherwise.
find
(Channel channel)

Returns the ChannelFuture of the individual I/O operation which is associated with the specified Channel.

public ChannelGroup
group()

Returns the ChannelGroup which is associated with this future.

public boolean
isPartialFailure()

Returns true if and only if the I/O operations associated with this future have failed partially with some success.

public boolean
isPartialSuccess()

Returns true if and only if the I/O operations associated with this future were partially successful with some failure.

public boolean
isSuccess()

Redeclares io.netty.util.concurrent.Future.isSuccess.

Returns true if and only if all I/O operations associated with this future were successful without any failure.
public Iterator<ChannelFuture>
iterator()

Redeclares java.lang.Iterable.iterator.

Returns the Iterator that enumerates all ChannelFutures which are associated with this future.
public ChannelGroupFuture
removeListener(GenericFutureListener<? extends Future<? super Void>> listener)

Redeclares io.netty.util.concurrent.Future.removeListener.

Removes the first occurrence of the specified listener from this future.
public ChannelGroupFuture
removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners)

Redeclares io.netty.util.concurrent.Future.removeListeners.

Removes the first occurrence for each of the listeners from this future.
public ChannelGroupFuture
sync()

Redeclares io.netty.util.concurrent.Future.sync.

Waits for this future until it is done, and rethrows the cause of the failure if this future failed.
public ChannelGroupFuture
syncUninterruptibly()

Redeclares io.netty.util.concurrent.Future.syncUninterruptibly.

Waits for this future until it is done, and rethrows the cause of the failure if this future failed.
Inherited from io.netty.util.concurrent.Future:
awaitawaitawaitUninterruptiblyawaitUninterruptiblycancelgetNowisCancellable
Inherited from java.lang.Iterable:
forEachspliterator

Method Detail

addListenerback to summary
public ChannelGroupFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener)

Redeclares io.netty.util.concurrent.Future.addListener.

Doc from io.netty.util.concurrent.Future.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.

Annotations
@Override
addListenersback to summary
public ChannelGroupFuture addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners)

Redeclares io.netty.util.concurrent.Future.addListeners.

Doc from io.netty.util.concurrent.Future.addListeners.

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.

Annotations
@Override
awaitback to summary
public ChannelGroupFuture await() throws InterruptedException

Redeclares io.netty.util.concurrent.Future.await.

Doc from io.netty.util.concurrent.Future.await.

Waits for this future to be completed.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread was interrupted
awaitUninterruptiblyback to summary
public ChannelGroupFuture awaitUninterruptibly()

Redeclares io.netty.util.concurrent.Future.awaitUninterruptibly.

Doc from io.netty.util.concurrent.Future.awaitUninterruptibly.

Waits for this future to be completed without interruption. This method catches an InterruptedException and discards it silently.

Annotations
@Override
causeback to summary
public ChannelGroupException cause()

Redeclares io.netty.util.concurrent.Future.cause.

Doc from io.netty.util.concurrent.Future.cause.

Returns the cause of the failed I/O operation if the I/O operation has failed.

Returns:ChannelGroupException

the cause of the failure. null if succeeded or this future is not completed yet.

Annotations
@Override
findback to summary
public ChannelFuture find(Channel channel)

Returns the ChannelFuture of the individual I/O operation which is associated with the specified Channel.

Returns:ChannelFuture

the matching ChannelFuture if found. null otherwise.

groupback to summary
public ChannelGroup group()

Returns the ChannelGroup which is associated with this future.

isPartialFailureback to summary
public boolean isPartialFailure()

Returns true if and only if the I/O operations associated with this future have failed partially with some success.

isPartialSuccessback to summary
public boolean isPartialSuccess()

Returns true if and only if the I/O operations associated with this future were partially successful with some failure.

isSuccessback to summary
public boolean isSuccess()

Redeclares io.netty.util.concurrent.Future.isSuccess.

Returns true if and only if all I/O operations associated with this future were successful without any failure.

Annotations
@Override
iteratorback to summary
public Iterator<ChannelFuture> iterator()

Redeclares java.lang.Iterable.iterator.

Returns the Iterator that enumerates all ChannelFutures which are associated with this future. Please note that the returned Iterator is is unmodifiable, which means a ChannelFuture cannot be removed from this future.

Returns:Iterator<ChannelFuture>

Doc from java.lang.Iterable.iterator.

an Iterator.

Annotations
@Override
removeListenerback to summary
public ChannelGroupFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener)

Redeclares io.netty.util.concurrent.Future.removeListener.

Doc from io.netty.util.concurrent.Future.removeListener.

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.

Annotations
@Override
removeListenersback to summary
public ChannelGroupFuture removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners)

Redeclares io.netty.util.concurrent.Future.removeListeners.

Doc from io.netty.util.concurrent.Future.removeListeners.

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.

Annotations
@Override
syncback to summary
public ChannelGroupFuture sync() throws InterruptedException

Redeclares io.netty.util.concurrent.Future.sync.

Doc from io.netty.util.concurrent.Future.sync.

Waits for this future until it is done, and rethrows the cause of the failure if this future failed.

Annotations
@Override
syncUninterruptiblyback to summary
public ChannelGroupFuture syncUninterruptibly()

Redeclares io.netty.util.concurrent.Future.syncUninterruptibly.

Doc from io.netty.util.concurrent.Future.syncUninterruptibly.

Waits for this future until it is done, and rethrows the cause of the failure if this future failed.

Annotations
@Override