On 3/3/10 6:45 PM, Carlo Camerino wrote:
HI,
I would like to ask on how to handle request reply with apache mina.
How do i ensure that when a thread sends a message, the thread gets
the reply for that message.
What happens is, at very highly concurrent scenario a thread gets the
response of another thread.
Mina is asynchronous. When you send a request, you can't simply wait for
the response. The Handler will get the response back and you have to
correlate this response with the request.
Now, each connection is associated with a session, so if you send a
request in a session, you have the guarantee that this session will
receive the response.
So one option is to store the response as an attribute of your session,
so that your thread can get it back later.
Remains one issue : you don't want to poll the session until the
response is stored into it, otherwise you'll lose the benefit of using
an asynchronous framework.
You have many ways to deal with this :
- you simply call session.read(), you'll get back a readFuture. You then
just have to wait() on it, and you'll get the response in the readFuture
when it's back
- store a callback in the session, and when the response arrives, the
callback will be called
- use a synchronization mechanism your main thread will wait on, and
when the message arrives, you release the lock so that the requester is
waken up
That's the three that come to my mind, but there are probably other
scenarii.
Hope it helps
--
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com