Interface ChannelFuture
-
- All Known Implementing Classes:
AbstractChannel.ChannelCloseFuture,ChannelRunnableWrapper,CompleteChannelFuture,DefaultChannelFuture,FailedChannelFuture,SslHandler.SSLEngineInboundCloseFuture,SucceededChannelFuture
public interface ChannelFutureThe result of an asynchronousChannelI/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
ChannelFutureinstance which gives you the information about the result or status of the I/O operation.A
ChannelFutureis either uncompleted or completed. When an I/O operation begins, a new future object is created. The new future is uncompleted initially - it is neither succeeded, failed, nor cancelled because the I/O operation is not finished yet. If the I/O operation is finished either successfully, with failure, or by cancellation, the future is marked as completed with more specific information, such as the cause of the failure. Please note that even failure and cancellation belong to the completed state.+---------------------------+ | Completed successfully | +---------------------------+ +----> isDone() = true | +--------------------------+ | | isSuccess() = true | | Uncompleted | | +===========================+ +--------------------------+ | | Completed with failure | | isDone() = false | | +---------------------------+ | isSuccess() = false |----+----> isDone() = true | | isCancelled() = false | | | getCause() = non-null | | getCause() = null | | +===========================+ +--------------------------+ | | Completed by cancellation | | +---------------------------+ +----> isDone() = true | | isCancelled() = true | +---------------------------+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 addChannelFutureListeners so you can get notified when the I/O operation is completed.Prefer
It is recommended to preferaddListener(ChannelFutureListener)toawait()addListener(ChannelFutureListener)toawait()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 specifiedChannelFutureListenerto theChannelFuture, and I/O thread will notify the listeners when the I/O operation associated with the future is done.ChannelFutureListeneryields 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 withawait(), 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.Do not call
await()insideChannelHandlerThe event handler methods in
ChannelHandleris often called by an I/O thread unless anExecutionHandleris in theChannelPipeline. 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,MessageEvente) { if (e.getMessage() instanceof GoodByeMessage) {ChannelFuturefuture = e.getChannel().close(); future.awaitUninterruptibly(); // Perform post-closure operation // ... } } // GOOD@Overridepublic void messageReceived(ChannelHandlerContextctx,MessageEvente) { if (e.getMessage() instanceof GoodByeMessage) {ChannelFuturefuture = e.getChannel().close(); future.addListener(newChannelFutureListener() { public void operationComplete(ChannelFuturefuture) { // 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.Do not confuse I/O timeout and await timeout
The timeout value you specify withawait(long),await(long, TimeUnit),awaitUninterruptibly(long), orawaitUninterruptibly(long, TimeUnit)are not related with I/O timeout at all. If an I/O operation times out, the future will be marked as 'completed with failure,' as depicted in the diagram above. For example, connect timeout should be configured via a transport-specific option:// BAD - NEVER DO THIS
ClientBootstrapb = ...;ChannelFuturef = b.connect(...); f.awaitUninterruptibly(10, TimeUnit.SECONDS); if (f.isCancelled()) { // Connection attempt cancelled by user } else if (!f.isSuccess()) { // You might get a NullPointerException here because the future // might not be completed yet. f.getCause().printStackTrace(); } else { // Connection established successfully } // GOODClientBootstrapb = ...; // Configure the connect timeout option. b.setOption("connectTimeoutMillis", 10000);ChannelFuturef = b.connect(...); f.awaitUninterruptibly(); // Now we are sure the future is completed. assert f.isDone(); if (f.isCancelled()) { // Connection attempt cancelled by user } else if (!f.isSuccess()) { f.getCause().printStackTrace(); } else { // Connection established successfully }
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddListener(ChannelFutureListener listener)Adds the specified listener to this future.ChannelFutureawait()Waits for this future to be completed.booleanawait(long timeoutMillis)Waits for this future to be completed within the specified time limit.booleanawait(long timeout, java.util.concurrent.TimeUnit unit)Waits for this future to be completed within the specified time limit.ChannelFutureawaitUninterruptibly()Waits for this future to be completed without interruption.booleanawaitUninterruptibly(long timeoutMillis)Waits for this future to be completed within the specified time limit without interruption.booleanawaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Waits for this future to be completed within the specified time limit without interruption.booleancancel()Cancels the I/O operation associated with this future and notifies all listeners if canceled successfully.java.lang.ThrowablegetCause()Returns the cause of the failed I/O operation if the I/O operation has failed.ChannelgetChannel()Returns a channel where the I/O operation associated with this future takes place.booleanisCancelled()Returnstrueif and only if this future was cancelled by acancel()method.booleanisDone()Returnstrueif and only if this future is complete, regardless of whether the operation was successful, failed, or cancelled.booleanisSuccess()Returnstrueif and only if the I/O operation was completed successfully.voidremoveListener(ChannelFutureListener listener)Removes the specified listener from this future.booleansetFailure(java.lang.Throwable cause)Marks this future as a failure and notifies all listeners.booleansetProgress(long amount, long current, long total)Notifies the progress of the operation to the listeners that implementsChannelFutureProgressListener.booleansetSuccess()Marks this future as a success and notifies all listeners.ChannelFuturesync()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.ChannelFuturesyncUninterruptibly()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.
-
-
-
Method Detail
-
getChannel
Channel getChannel()
Returns a channel where the I/O operation associated with this future takes place.
-
isDone
boolean isDone()
Returnstrueif and only if this future is complete, regardless of whether the operation was successful, failed, or cancelled.
-
isCancelled
boolean isCancelled()
Returnstrueif and only if this future was cancelled by acancel()method.
-
isSuccess
boolean isSuccess()
Returnstrueif and only if the I/O operation was completed successfully.
-
getCause
java.lang.Throwable getCause()
Returns the cause of the failed I/O operation if the I/O operation has failed.- Returns:
- the cause of the failure.
nullif succeeded or this future is not completed yet.
-
cancel
boolean cancel()
Cancels the I/O operation associated with this future and notifies all listeners if canceled successfully.- Returns:
trueif and only if the operation has been canceled.falseif the operation can't be canceled or is already completed.
-
setSuccess
boolean setSuccess()
Marks this future as a success and notifies all listeners.- Returns:
trueif and only if successfully marked this future as a success. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
setFailure
boolean setFailure(java.lang.Throwable cause)
Marks this future as a failure and notifies all listeners.- Returns:
trueif and only if successfully marked this future as a failure. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
setProgress
boolean setProgress(long amount, long current, long total)Notifies the progress of the operation to the listeners that implementsChannelFutureProgressListener. Please note that this method will not do anything and returnfalseif this future is complete already.- Returns:
trueif and only if notification was made.
-
addListener
void addListener(ChannelFutureListener listener)
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.
-
removeListener
void removeListener(ChannelFutureListener listener)
Removes 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.
-
sync
ChannelFuture sync() throws java.lang.InterruptedException
Waits for this future until it is done, and rethrows the cause of the failure if this future failed. If the cause of the failure is a checked exception, it is wrapped with a newChannelExceptionbefore being thrown.- Throws:
java.lang.InterruptedException
-
syncUninterruptibly
ChannelFuture syncUninterruptibly()
Waits for this future until it is done, and rethrows the cause of the failure if this future failed. If the cause of the failure is a checked exception, it is wrapped with a newChannelExceptionbefore being thrown.
-
await
ChannelFuture await() throws java.lang.InterruptedException
Waits for this future to be completed.- Throws:
java.lang.InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
ChannelFuture awaitUninterruptibly()
Waits for this future to be completed without interruption. This method catches anInterruptedExceptionand discards it silently.
-
await
boolean await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionWaits for this future to be completed within the specified time limit.- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
java.lang.InterruptedException- if the current thread was interrupted
-
await
boolean await(long timeoutMillis) throws java.lang.InterruptedExceptionWaits for this future to be completed within the specified time limit.- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
java.lang.InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
boolean awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Returns:
trueif and only if the future was completed within the specified time limit
-
awaitUninterruptibly
boolean awaitUninterruptibly(long timeoutMillis)
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Returns:
trueif and only if the future was completed within the specified time limit
-
-