mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niklas Therning <nik...@trillian.se>
Subject Re: ConnectFuture
Date Fri, 12 Jan 2007 08:09:48 GMT
peter ellis wrote:
> Can anyone help explain to me how the ConnectFuture works!???
>
In MINA most IO operations are asynchronous. A call to
connector.connect(...) will start a connect operation in a separate
thread and the call will most likely return before the connect operation
has finished. The ConnectFuture returned by connect() can be used to
determine if the connect operation has finished and if it was successful
or not.

Calling future.join() will wait for the operation to finish (either
succeed or fail). Calling future.getSession() after the connect
operation has finished will get the MINA IoSession corresponding to the
established connection. If the connect operation failed getSession()
throws an exception.

You can also listen for completion events by adding a IoFutureListener
to the future object. This way you won't have to call future.join()
which blocks the current thread:

connector.connect(...).addListener(new IoFutureListener() {
    public void operationComplete(IoFuture f) {
       ConnectFuture future = (ConnectFuture) f;
       if (future.isConnected()) {
          // The connect operation succeeded
       } else {
          // The connect operation failed
       }
    }
});

You can read more about futures here: http://c2.com/cgi/wiki?FutureValue

HTH

-- 
Niklas Therning
www.spamdrain.net


Mime
View raw message