mina-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ashish <paliwalash...@gmail.com>
Subject Re: Mina: Client which sends/receives messages synchronous
Date Sun, 10 Jan 2010 04:28:18 GMT
On Thu, Jan 7, 2010 at 6:44 PM, Christian Mueller
<christian.mueller@gmail.com> wrote:
> Hey Mina users!
>
> I'm a newbie for Apache Mina (2.0.0 RC1). I will build a framework for sending and receiving
ucp messages (see http://en.wikipedia.org/wiki/Universal_Computer_Protocol) for sending and
receiving short messages. I have done a Client which send/receives ucp messages asynchronous
(which was really easy with Apache Mina - thanks! :-) ).
>
> But for convenience, I also will provide the possibility to sends/receives ucp messages
synchronous. My first try looks as following:
>
>    public UCPMessage sendSync(UCPMessage req) throws Throwable {
>        WriteFuture writeFuture = session.write(req);
>        writeFuture = writeFuture.awaitUninterruptibly();
>
>        if (writeFuture.getException() != null) {
>            throw writeFuture.getException();
>        }
>
>        ReadFuture readFuture = session.read();
>        readFuture.awaitUninterruptibly();
>        if (readFuture.getException() != null) {
>            throw readFuture.getException();
>        }
>
>        return (UCPMessage) readFuture.getMessage();
>    }
>
> Is this the right way?
>
> The java doc for IoSession says, that the IoSession is thread safe. If I use 'session.read()',
I think my client is not thread safe anymore, because 'session.read()' returns the next response
from the queue, whatever if this the response for this request or for another request (which
is send with the same client). I'm right?

This will work as long as you don't send multiple request for same
session. Its like you send a message and receive a response for the
session and then send next one. Its a bit risky coz you will hold up
your threads or your server may send a delayed response.

Alternatively you can provide a layer between your message
send/receive and the processing. Something like an Observer pattern.
Its fairly common in SNMP stacks, if you can look into any of them.

HTH !

> Do you have a better solution for a synchronous client which is maybe thread safe?


> Thanks for your help,
> Christian



-- 
thanks
ashish

Blog: http://www.ashishpaliwal.com/blog
My Photo Galleries: http://www.pbase.com/ashishpaliwal

Mime
View raw message