You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by peter ellis <pe...@googlemail.com> on 2007/01/11 18:14:42 UTC

ConnectFuture

Can anyone help explain to me how the ConnectFuture works!???

-- 
Peter Ellis
Java Developer

Re: ConnectFuture

Posted by sufiyan-gmail <ci...@gmail.com>.
Peter,

A good way to start is to have a look at the examples in MINA [1] and
API docs[2].

If i am not mistaken, a connectFuture returns a call back method
allowing you to specify the code needed.
Judging from your example code, connector.connect(..) only needs to be
called in once and should be outside of the infinite loop.

[1]http://mina.apache.org/documentation.html
[2]http://mina.apache.org/report/1.0/apidocs/

On Thu, 2007-01-11 at 17:14 +0000, peter ellis wrote:

> Can anyone help explain to me how the ConnectFuture works!???
> 

Re: ConnectFuture

Posted by peter ellis <pe...@googlemail.com>.
Cool I think I understand what its doing now :-D (sorry i was getting
frustrated with the code as i didnt understand what it was
doing...pressure from the project manager also doesnt help :-( )

On 1/12/07, Niklas Therning <ni...@trillian.se> wrote:
> 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
>
>


-- 
Peter Ellis
Java Developer

Re: ConnectFuture

Posted by Niklas Therning <ni...@trillian.se>.
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