You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Erinc Arikan <er...@gmail.com> on 2010/04/09 20:57:28 UTC
Re: Nonblocking Synchronous Calls to another server from Message
Handler
Ok I will make my sync calls asynchronously by using FutureTask from
java.util.concurrent in my Message Handler when I am processing the message.
I can also have an application level hashmap to save the futuretask along
with the session and I can periodically check if the futuretasks in hashmap
are done on a separate thread, if any of them is complete I can get the
response object and write it back to the session, hopefully messages will be
encoded fine in the pipeline as if they are being encoded with the
synchronous approach.
So code will be something like that:
ExecutorService es = Executors.newFixedThreadPool(1);
FutureTask <Message> future = new FutureTask<Message>(
new Callable<Message>()
{
public Message call()
{
try {
return
Util.sendSynchAndWait(voidMessage,30000);
} catch (SyncTimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
);
es.execute(future);
//Pseudo code to save the futuretask and session into a hashmap
MapEntry me = new MapEntry(future, session);
asyncMap.add(key,me);
I feel like it can be done this way, but I still feel that this solution is
too cumbersome, I'd like to know if there's a better approach to deal with
this issue.
Erinc
On Fri, Apr 9, 2010 at 12:14 AM, Erinc Arikan <er...@gmail.com> wrote:
> Thanks for the response FIkri, do you have the code sample to make sync
> call asynchronously so that I can be notified when the response is ready, do
> you use something like readfuture?
>
> Sent from my iPhone
>
>
> On Apr 8, 2010, at 10:45 PM, fikri.ilyas@gmail.com wrote:
>
> Hi,
>>
>> I have the same architecture as you mention. Currently my application
>> receive a request from client (with asynchronous handler, and it can handles
>> synchronous client as well) and forward the message to another server and
>> when the response is ready, it will notify back to where the client session
>> is.
>>
>> I'm using a hashmap to store the sessions that comes from the acceptor's
>> handler with a unique key (can be the session itself or a unique I'd from
>> the client message) and pass that key along if you forward to another server
>> synchronously. And when the response is ready, get the previous session with
>> the key and send back to where the client is (don't forget to remove the
>> session from the hashmap).
>> Powered by Telkomsel BlackBerry®
>>
>> -----Original Message-----
>> From: Erinc Arikan <er...@gmail.com>
>> Date: Thu, 8 Apr 2010 18:24:16
>> To: <us...@mina.apache.org>
>> Subject: Nonblocking Synchronous Calls to another server from Message
>> Handler
>>
>> Hi;
>>
>> I apologize if I am asking something obvious but I've been searching about
>> this for a day or two, still couldn't figure out how I can treat
>> synchronous
>> remote calls as if they are asynchronous in my message handler. I want to
>> make the call and instead of waiting for the response, I want to be
>> notified
>> when response becomes available, so that I can select the correct session
>> and send the response back to the correct client. Apparently there's not
>> much sample code about this in the internet.
>>
>> My scenario is as follows:
>>
>> Client sends the message , Mina Server decodes message to Pojo and passes
>> it
>> to Message Handler where a Sync call is made to another server, Other
>> server processes the message for a second or two, returns it back to
>> message
>> handler, and Mina Server gets the response back to the client. As you can
>> see if I will wait for the response in the message handler, that makes
>> things super inefficient.
>>
>> In a recent discussion in a topic, Emmanuel Lecharny stated that there's a
>> way to make this synchronous call in a async fashion. Whenever response
>> comes back, I can find he associated session and send the response back
>> correctly.
>>
>> I know there's ReadFuture and WriteFuture which works with session to
>> accomplish read and writes on session. But my problem is I am not waiting
>> on
>> a session event, I am waiting on a remote sync call to return an object,
>> so
>> that I can pass that object back to the client.
>>
>> My sync call is something like that
>>
>> RemoteProducerTool producer = RemoteProducerTool.getTool(syncUrl,
>> syncUser,
>> syncPwd, syncSubject);
>> hostResponse = producer.sendSyncMessage(frame, timeout);
>>
>> If somebody can give a small example about how to make that call
>> asynchronously and how to locate the correct session and send response
>> back
>> to correct client I'd be grateful. Believe me you won't be just helping
>> me,
>> you'll be helping everybody who will try to do something like that one
>> day.
>>
>> Erinc
>>
>>