View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.future;
21  
22  import org.apache.mina.core.session.IoSession;
23  
24  /**
25   * An {@link IoFuture} for {@link IoSession#read() asynchronous read requests}. 
26   *
27   * <h3>Example</h3>
28   * <pre>
29   * IoSession session = ...;
30   * // useReadOperation must be enabled to use read operation.
31   * session.getConfig().setUseReadOperation(true);
32   * 
33   * ReadFuture future = session.read();
34   * // Wait until a message is received.
35   * future.await();
36   * try {
37   *     Object message = future.getMessage();
38   * } catch (Exception e) {
39   *     ...
40   * }
41   * </pre>
42   *
43   * @author The Apache MINA Project (dev@mina.apache.org)
44   */
45  public interface ReadFuture extends IoFuture {
46      
47      /**
48       * Returns the received message.  It returns <tt>null</tt> if this
49       * future is not ready or the associated {@link IoSession} has been closed. 
50       * 
51       * @throws RuntimeException if read or any relevant operation has failed.
52       */
53      Object getMessage();
54      
55      /**
56       * Returns <tt>true</tt> if a message was received successfully.
57       */
58      boolean isRead();
59      
60      /**
61       * Returns <tt>true</tt> if the {@link IoSession} associated with this
62       * future has been closed.
63       */
64      boolean isClosed();
65      
66      /**
67       * Returns the cause of the read failure if and only if the read
68       * operation has failed due to an {@link Exception}.  Otherwise,
69       * <tt>null</tt> is returned.
70       */
71      Throwable getException();
72  
73      /**
74       * Sets the message is written, and notifies all threads waiting for
75       * this future.  This method is invoked by MINA internally.  Please do
76       * not call this method directly.
77       */
78      void setRead(Object message);
79      
80      /**
81       * Sets the associated {@link IoSession} is closed.  This method is invoked
82       * by MINA internally.  Please do not call this method directly.
83       */
84      void setClosed();
85      
86      /**
87       * Sets the cause of the read failure, and notifies all threads waiting
88       * for this future.  This method is invoked by MINA internally.  Please
89       * do not call this method directly.
90       */
91      void setException(Throwable cause);
92  
93      ReadFuture await() throws InterruptedException;
94  
95      ReadFuture awaitUninterruptibly();
96  
97      ReadFuture addListener(IoFutureListener<?> listener);
98  
99      ReadFuture removeListener(IoFutureListener<?> listener);
100 }