public interface ChannelGroupFuture extends Iterable<ChannelFuture>
ChannelGroup
operation.
ChannelGroupFuture
is composed of ChannelFuture
s which
represent the outcome of the individual I/O operations that affect the
Channel
s 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(ChannelGroupFutureListener)
to await()
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.
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@Override
public void messageReceived(ChannelHandlerContext
ctx,MessageEvent
e) { if (e.getMessage() instanceof ShutdownMessage) {ChannelGroup
allChannels = MyServer.getAllChannels();ChannelGroupFuture
future = allChannels.close(); future.awaitUninterruptibly(); // Perform post-shutdown operation // ... } } // GOOD@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (e.getMessage() instanceof ShutdownMessage) {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 call await()
in an I/O thread. Otherwise,
IllegalStateException
will be raised to prevent a dead lock.
Modifier and Type | Method and Description |
---|---|
void |
addListener(ChannelGroupFutureListener listener)
Adds the specified listener to this future.
|
ChannelGroupFuture |
await()
Waits for this future to be completed.
|
boolean |
await(long timeoutMillis)
Waits for this future to be completed within the
specified time limit.
|
boolean |
await(long timeout,
TimeUnit unit)
Waits for this future to be completed within the
specified time limit.
|
ChannelGroupFuture |
awaitUninterruptibly()
Waits for this future to be completed without
interruption.
|
boolean |
awaitUninterruptibly(long timeoutMillis)
Waits for this future to be completed within the
specified time limit without interruption.
|
boolean |
awaitUninterruptibly(long timeout,
TimeUnit unit)
Waits for this future to be completed within the
specified time limit without interruption.
|
ChannelFuture |
find(Channel channel)
Returns the
ChannelFuture of the individual I/O operation which
is associated with the specified Channel . |
ChannelFuture |
find(Integer channelId)
Returns the
ChannelFuture of the individual I/O operation which
is associated with the Channel whose ID matches the specified
integer. |
ChannelGroup |
getGroup()
Returns the
ChannelGroup which is associated with this future. |
boolean |
isCompleteFailure()
Returns
true if and only if all I/O operations associated with
this future have failed without any success. |
boolean |
isCompleteSuccess()
Returns
true if and only if all I/O operations associated with
this future were successful without any failure. |
boolean |
isDone()
Returns
true if and only if this future is
complete, regardless of whether the operation was successful, failed,
or canceled. |
boolean |
isPartialFailure()
Returns
true if and only if the I/O operations associated with
this future have failed partially with some success. |
boolean |
isPartialSuccess()
Returns
true if and only if the I/O operations associated with
this future were partially successful with some failure. |
Iterator<ChannelFuture> |
iterator()
Returns the
Iterator that enumerates all ChannelFuture s
which are associated with this future. |
void |
removeListener(ChannelGroupFutureListener listener)
Removes the specified listener from this future.
|
ChannelGroup getGroup()
ChannelGroup
which is associated with this future.ChannelFuture find(Integer channelId)
ChannelFuture
of the individual I/O operation which
is associated with the Channel
whose ID matches the specified
integer.ChannelFuture
if found.
null
otherwise.ChannelFuture find(Channel channel)
ChannelFuture
of the individual I/O operation which
is associated with the specified Channel
.ChannelFuture
if found.
null
otherwise.boolean isDone()
true
if and only if this future is
complete, regardless of whether the operation was successful, failed,
or canceled.boolean isCompleteSuccess()
true
if and only if all I/O operations associated with
this future were successful without any failure.boolean isPartialSuccess()
true
if and only if the I/O operations associated with
this future were partially successful with some failure.boolean isCompleteFailure()
true
if and only if all I/O operations associated with
this future have failed without any success.boolean isPartialFailure()
true
if and only if the I/O operations associated with
this future have failed partially with some success.void addListener(ChannelGroupFutureListener listener)
void removeListener(ChannelGroupFutureListener listener)
ChannelGroupFuture await() throws InterruptedException
InterruptedException
- if the current thread was interruptedChannelGroupFuture awaitUninterruptibly()
InterruptedException
and
discards it silently.boolean await(long timeout, TimeUnit unit) throws InterruptedException
true
if and only if the future was completed within
the specified time limitInterruptedException
- if the current thread was interruptedboolean await(long timeoutMillis) throws InterruptedException
true
if and only if the future was completed within
the specified time limitInterruptedException
- if the current thread was interruptedboolean awaitUninterruptibly(long timeout, TimeUnit unit)
InterruptedException
and discards it silently.true
if and only if the future was completed within
the specified time limitboolean awaitUninterruptibly(long timeoutMillis)
InterruptedException
and discards it silently.true
if and only if the future was completed within
the specified time limitIterator<ChannelFuture> iterator()
Iterator
that enumerates all ChannelFuture
s
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.iterator
in interface Iterable<ChannelFuture>
Copyright © 2008-2016 The Netty Project. All Rights Reserved.