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 /** 23 * An {@link IoFuture} for asynchronous write requests. 24 * 25 * <h3>Example</h3> 26 * <pre> 27 * IoSession session = ...; 28 * WriteFuture future = session.write(...); 29 * 30 * // Wait until the message is completely written out to the O/S buffer. 31 * future.awaitUninterruptibly(); 32 * 33 * if( future.isWritten() ) 34 * { 35 * // The message has been written successfully. 36 * } 37 * else 38 * { 39 * // The message couldn't be written out completely for some reason. 40 * // (e.g. Connection is closed) 41 * } 42 * </pre> 43 * 44 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 45 */ 46 public interface WriteFuture extends IoFuture { 47 /** 48 * @return <tt>true</tt> if the write operation is finished successfully. 49 */ 50 boolean isWritten(); 51 52 /** 53 * @return the cause of the write failure if and only if the write 54 * operation has failed due to an {@link Exception}. Otherwise, 55 * <tt>null</tt> is returned. 56 */ 57 Throwable getException(); 58 59 /** 60 * Sets the message is written, and notifies all threads waiting for 61 * this future. This method is invoked by MINA internally. Please do 62 * not call this method directly. 63 */ 64 void setWritten(); 65 66 /** 67 * Sets the cause of the write failure, and notifies all threads waiting 68 * for this future. This method is invoked by MINA internally. Please 69 * do not call this method directly. 70 * 71 * @param cause The exception to store in the Future instance 72 */ 73 void setException(Throwable cause); 74 75 /** 76 * Wait for the asynchronous operation to complete. 77 * The attached listeners will be notified when the operation is 78 * completed. 79 * 80 * @return the created {@link WriteFuture} 81 * @throws InterruptedException If the wait is interrupted 82 */ 83 WriteFuture await() throws InterruptedException; 84 85 /** 86 * {@inheritDoc} 87 */ 88 WriteFuture awaitUninterruptibly(); 89 90 /** 91 * {@inheritDoc} 92 */ 93 WriteFuture addListener(IoFutureListener<?> listener); 94 95 /** 96 * {@inheritDoc} 97 */ 98 WriteFuture removeListener(IoFutureListener<?> listener); 99 }