You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Carlo Camerino <ca...@gmail.com> on 2010/03/03 18:45:02 UTC

How To Handle Request Reply

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.

Is there a way that I can set a correlation ID?

What's the best way of handling this?

What's the best way to handle request reply using apache mina?

Thanks
Carlo

Re: How To Handle Request Reply

Posted by Ashish <pa...@gmail.com>.
Yes its very much applicable, but there is very little or no
documentation around this.

thanks
ashish

On Thu, Mar 4, 2010 at 4:20 PM, Brad Harvey <ha...@gmail.com> wrote:
> Is this still applicable?
>
> https://issues.apache.org/jira/browse/DIRMINA-92?focusedCommentId=12488311&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12488311
>
>
> Emmanuel Lecharny wrote:
>>
>> 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
>>
>

Re: How To Handle Request Reply

Posted by Brad Harvey <ha...@gmail.com>.
Is this still applicable?

https://issues.apache.org/jira/browse/DIRMINA-92?focusedCommentId=12488311&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12488311


Emmanuel Lecharny wrote:
> 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
>

Re: How To Handle Request Reply

Posted by Emmanuel Lecharny <el...@gmail.com>.
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