View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel;
17  
18  import io.netty.util.concurrent.Future;
19  import io.netty.util.concurrent.GenericFutureListener;
20  
21  
22  /**
23   * Listens to the result of a {@link ChannelFuture}.  The result of the
24   * asynchronous {@link Channel} I/O operation is notified once this listener
25   * is added by calling {@link ChannelFuture#addListener(GenericFutureListener)}.
26   *
27   * <h3>Return the control to the caller quickly</h3>
28   *
29   * {@link #operationComplete(Future)} is directly called by an I/O
30   * thread.  Therefore, performing a time consuming task or a blocking operation
31   * in the handler method can cause an unexpected pause during I/O.  If you need
32   * to perform a blocking operation on I/O completion, try to execute the
33   * operation in a different thread using a thread pool.
34   */
35  public interface ChannelFutureListener extends GenericFutureListener<ChannelFuture> {
36  
37      /**
38       * A {@link ChannelFutureListener} that closes the {@link Channel} which is
39       * associated with the specified {@link ChannelFuture}.
40       */
41      ChannelFutureListener CLOSE = new ChannelFutureListener() {
42          @Override
43          public void operationComplete(ChannelFuture future) {
44              future.channel().close();
45          }
46      };
47  
48      /**
49       * A {@link ChannelFutureListener} that closes the {@link Channel} when the
50       * operation ended up with a failure or cancellation rather than a success.
51       */
52      ChannelFutureListener CLOSE_ON_FAILURE = new ChannelFutureListener() {
53          @Override
54          public void operationComplete(ChannelFuture future) {
55              if (!future.isSuccess()) {
56                  future.channel().close();
57              }
58          }
59      };
60  
61      /**
62       * A {@link ChannelFutureListener} that forwards the {@link Throwable} of the {@link ChannelFuture} into the
63       * {@link ChannelPipeline}. This mimics the old behavior of Netty 3.
64       */
65      ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE = new ChannelFutureListener() {
66          @Override
67          public void operationComplete(ChannelFuture future) {
68              if (!future.isSuccess()) {
69                  future.channel().pipeline().fireExceptionCaught(future.cause());
70              }
71          }
72      };
73  
74      // Just a type alias
75  }