|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface ChannelFuture
The result of an asynchronous Channel
I/O operation.
All I/O operations in Netty are asynchronous. It means any I/O calls will
return immediately with no guarantee that the requested I/O operation has
been completed at the end of the call. Instead, you will be returned with
a ChannelFuture
instance which tells you when the requested I/O
operation has succeeded, failed, or cancelled.
Various methods are provided to let you check if the I/O operation 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 ChannelFutureListener
so you can get notified when the I/O operation has been completed.
addListener(ChannelFutureListener)
to await()
addListener(ChannelFutureListener)
to
await()
wherever possible to get notified when an I/O operation is
done and to do any follow-up tasks.
addListener(ChannelFutureListener)
is non-blocking. It simply adds
the specified ChannelFutureListener
to the ChannelFuture
, and
I/O thread will notify the listeners when the I/O operation associated with
the future is done. ChannelFutureListener
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 the operation is done. It is easier to implement
a sequential logic with await()
, but the caller thread blocks
unnecessarily until the I/O operation is 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 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (e.getMessage() instanceof GoodByeMessage) { ChannelFuture future = e.getChannel().close(); future.awaitUninterruptibly(); // Perform post-closure operation // ... } } // GOOD public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (e.getMessage() instanceof GoodByeMessage) { ChannelFuture future = e.getChannel().close(); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture 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 | |
---|---|
void |
addListener(ChannelFutureListener listener)
Adds the specified listener to this future. |
ChannelFuture |
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. |
ChannelFuture |
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. |
boolean |
cancel()
Cancels the I/O operation associated with this future and notifies all listeners if canceled successfully. |
Throwable |
getCause()
Returns the cause of the failed I/O operation if the I/O operation has failed. |
Channel |
getChannel()
Returns a channel where the I/O operation associated with this future takes place. |
boolean |
isCancelled()
Returns true if and only if this future was
cancelled by a cancel() method. |
boolean |
isDone()
Returns true if and only if this future is
complete, regardless of whether the operation was successful, failed,
or cancelled. |
boolean |
isSuccess()
Returns true if and only if the I/O operation was completed
successfully. |
void |
removeListener(ChannelFutureListener listener)
Removes the specified listener from this future. |
boolean |
setFailure(Throwable cause)
Marks this future as a failure and notifies all listeners. |
boolean |
setSuccess()
Marks this future as a success and notifies all listeners. |
Method Detail |
---|
Channel getChannel()
boolean isDone()
true
if and only if this future is
complete, regardless of whether the operation was successful, failed,
or cancelled.
boolean isCancelled()
true
if and only if this future was
cancelled by a cancel()
method.
boolean isSuccess()
true
if and only if the I/O operation was completed
successfully.
Throwable getCause()
null
if succeeded or this future is not
completed yet.boolean cancel()
true
if and only if the operation has been canceled.
false
if the operation can't be canceled or is already
completed.boolean setSuccess()
true
if and only if successfully marked this future as
a success. Otherwise false
because this future is
already marked as either a success or a failure.boolean setFailure(Throwable cause)
true
if and only if successfully marked this future as
a failure. Otherwise false
because this future is
already marked as either a success or a failure.void addListener(ChannelFutureListener listener)
void removeListener(ChannelFutureListener listener)
ChannelFuture await() throws InterruptedException
InterruptedException
- if the current thread was interruptedChannelFuture 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 limit
InterruptedException
- if the current thread was interruptedboolean await(long timeoutMillis) throws InterruptedException
true
if and only if the future was completed within
the specified time limit
InterruptedException
- 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 limit
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |