You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Christian Mueller <ch...@gmail.com> on 2010/01/07 14:14:19 UTC
Mina: Client which sends/receives messages synchronous
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?
Do you have a better solution for a synchronous client which is maybe thread safe?
Thanks for your help,
Christian
Re: Mina: Client which sends/receives messages synchronous
Posted by Ashish <pa...@gmail.com>.
Thanks !
A session is created automatically for you when a Client connects.
There has to be a way to correlate, not sue what.
If there is none you have to create one in ur app, like this ip+port
aka session has sent a message and waiting for reply.
Until reply is received don't send any more messages.
Try SNMP4J (http://www.snmp4j.org) it has similar abstraction built in.
thanks
ashish
On Thu, Jan 21, 2010 at 9:13 PM, Christian Müller
<ch...@gmail.com> wrote:
> Hello Ashish!
>
> Thanks for the link, but I could not found a suitable already existing
> solution in the Mina related projects for my requirement.
> So, I build a unit test which sends "parallel" 100 requests and everything
> works fine. Thanks for your help and the great work of the Mina team.
>
> But how could a solution looks like (in an multithreaded environment), if
> the underling protocol doesn't support a correlation id? Should I open a new
> Session for each request? Sounds not efficient...
>
> Regards,
> Christian
>
> P.S. Congratulations to become a PMC member!
>
--
thanks
ashish
Blog: http://www.ashishpaliwal.com/blog
My Photo Galleries: http://www.pbase.com/ashishpaliwal
Re: Mina: Client which sends/receives messages synchronous
Posted by Christian Müller <ch...@gmail.com>.
Hello Ashish!
Thanks for the link, but I could not found a suitable already existing
solution in the Mina related projects for my requirement.
So, I build a unit test which sends "parallel" 100 requests and everything
works fine. Thanks for your help and the great work of the Mina team.
But how could a solution looks like (in an multithreaded environment), if
the underling protocol doesn't support a correlation id? Should I open a new
Session for each request? Sounds not efficient...
Regards,
Christian
P.S. Congratulations to become a PMC member!
Re: Mina: Client which sends/receives messages synchronous
Posted by Ashish <pa...@gmail.com>.
Just try this out :-)
As far as this pattern is concerned, this is common and you can get
hold of this in some of the MINA based projects.
This is a good place to look for http://mina.apache.org/related-projects.html
You may get a ready to use solution.
Thanks
ashish
On Wed, Jan 13, 2010 at 4:37 AM, Christian Mueller
<ch...@gmail.com> wrote:
>
> Hey Ashis,
>
> what do you think about this solution:
>
> public class UCPClient {
>
> private Map<Integer, BlockingQueue<UCPMessageResponse>> concurrentMap =
> new ConcurrentHashMap<Integer, BlockingQueue<UCPMessageResponse>>();
>
> // some other code
>
> public UCPMessageResponse send(UCPMessageRequest request) throws
> Throwable {
> BlockingQueue<UCPMessageResponse> queue = new
> LinkedBlockingQueue<UCPMessageResponse>(1);
> UCPMessageResponse res = null;
>
> try {
> if (sendSync) {
>
> concurrentMap.put(Integer.valueOf(request.getTransactionReference()),
> queue);
> }
>
> WriteFuture writeFuture = session.write(request);
>
> if (sendSync) {
> boolean isSent = writeFuture.await(transactionTimeout,
> TimeUnit.MILLISECONDS);
>
> if (!isSent) {
> throw new TimeoutException("Could not sent the request
> in " + transactionTimeout + " milliseconds.");
>
> }
>
> if (writeFuture.getException() != null) {
> throw writeFuture.getException();
> }
>
> res = queue.poll(transactionTimeout, TimeUnit.MILLISECONDS);
>
> if (res == null) {
> throw new TimeoutException("Could not receive the
> response in " + transactionTimeout + " milliseconds.");
> }
> }
> } finally {
> if (sendSync) {
>
> concurrentMap.remove(Integer.valueOf(request.getTransactionReference()));
> }
> }
>
> return res;
> }
> }
>
> and the IoHandler:
>
> public class InnerHandler implements IoHandler {
>
> // some other code
>
> public void messageReceived(IoSession session, Object message) throws
> Exception {
> if (sendSync) {
> UCPMessageResponse res = (UCPMessageResponse) message;
> BlockingQueue<UCPMessageResponse> queue =
> concurrentMap.get(res.getTransactionReference());
> queue.offer(res);
> }
> }
> }
>
> Regards,
> Christian
>
> --
> View this message in context: http://old.nabble.com/Mina%3A-Client-which-sends-receives-messages-synchronous-tp27059797p27136508.html
> Sent from the Apache MINA User Forum mailing list archive at Nabble.com.
>
>
--
thanks
ashish
Blog: http://www.ashishpaliwal.com/blog
My Photo Galleries: http://www.pbase.com/ashishpaliwal
Re: Mina: Client which sends/receives messages synchronous
Posted by Christian Mueller <ch...@gmail.com>.
Hey Ashis,
what do you think about this solution:
public class UCPClient {
private Map<Integer, BlockingQueue<UCPMessageResponse>> concurrentMap =
new ConcurrentHashMap<Integer, BlockingQueue<UCPMessageResponse>>();
// some other code
public UCPMessageResponse send(UCPMessageRequest request) throws
Throwable {
BlockingQueue<UCPMessageResponse> queue = new
LinkedBlockingQueue<UCPMessageResponse>(1);
UCPMessageResponse res = null;
try {
if (sendSync) {
concurrentMap.put(Integer.valueOf(request.getTransactionReference()),
queue);
}
WriteFuture writeFuture = session.write(request);
if (sendSync) {
boolean isSent = writeFuture.await(transactionTimeout,
TimeUnit.MILLISECONDS);
if (!isSent) {
throw new TimeoutException("Could not sent the request
in " + transactionTimeout + " milliseconds.");
}
if (writeFuture.getException() != null) {
throw writeFuture.getException();
}
res = queue.poll(transactionTimeout, TimeUnit.MILLISECONDS);
if (res == null) {
throw new TimeoutException("Could not receive the
response in " + transactionTimeout + " milliseconds.");
}
}
} finally {
if (sendSync) {
concurrentMap.remove(Integer.valueOf(request.getTransactionReference()));
}
}
return res;
}
}
and the IoHandler:
public class InnerHandler implements IoHandler {
// some other code
public void messageReceived(IoSession session, Object message) throws
Exception {
if (sendSync) {
UCPMessageResponse res = (UCPMessageResponse) message;
BlockingQueue<UCPMessageResponse> queue =
concurrentMap.get(res.getTransactionReference());
queue.offer(res);
}
}
}
Regards,
Christian
--
View this message in context: http://old.nabble.com/Mina%3A-Client-which-sends-receives-messages-synchronous-tp27059797p27136508.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.
Re: Mina: Client which sends/receives messages synchronous
Posted by Ashish <pa...@gmail.com>.
Don't have sample, but yeah this is what I would have done.
thanks
ashish
On Tue, Jan 12, 2010 at 4:25 AM, Christian Müller
<ch...@gmail.com> wrote:
> Hi Ashis,
>
> thanks for your reply.
> What is your recommendation, if I have to deal with multiple
> request/response cycles in one session (the UCP protocol is a
> request-response based protocol) and I have to provide a synchronous client
> API? I have "googled", "nabbled", "osdired", ... but I didn't found a
> solution for my "problem". :-(
>
> I would like to offer an API like this one:
>
> public class UCPClient {
>
> private IoSession session;
>
> // initialize the session, ...
>
> public UCPResponse send(UCPRequest request) {
> session.write(request);
>
> // correlate response
>
> return response;
> }
> }
>
> Do you have any sample code for me?
>
> Regards,
> Christian
>
--
thanks
ashish
Blog: http://www.ashishpaliwal.com/blog
My Photo Galleries: http://www.pbase.com/ashishpaliwal
Re: Mina: Client which sends/receives messages synchronous
Posted by Christian Müller <ch...@gmail.com>.
Hi Ashis,
thanks for your reply.
What is your recommendation, if I have to deal with multiple
request/response cycles in one session (the UCP protocol is a
request-response based protocol) and I have to provide a synchronous client
API? I have "googled", "nabbled", "osdired", ... but I didn't found a
solution for my "problem". :-(
I would like to offer an API like this one:
public class UCPClient {
private IoSession session;
// initialize the session, ...
public UCPResponse send(UCPRequest request) {
session.write(request);
// correlate response
return response;
}
}
Do you have any sample code for me?
Regards,
Christian
Re: Mina: Client which sends/receives messages synchronous
Posted by Ashish <pa...@gmail.com>.
On Thu, Jan 7, 2010 at 6:44 PM, Christian Mueller
<ch...@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
InetSocketAddress and localhost
Posted by "Simmons, Aaron" <as...@rosettastone.com>.
I'm using mina 2.0.0-M6 to connect to a client on localhost. Up to now I've been creating InetSocketAddress without specifying the hostname, like this:
protected static void connect(int port) {
// create address with implicit hostname
InetSocketAddress addr= new InetSocketAddress(port);
logger.debug("trying to connect to {}", addr);
NioSocketConnector connector= new NioSocketConnector();
connector.setHandler(new IoHandlerAdapter());
ConnectFuture cf= connector.connect(addr);
cf.awaitUninterruptibly();
logger.debug("connected to {}", addr);
IoSession session= cf.getSession();
session.close(true);
}
Most of the time this works fine. However, on some machines this fails-- it will throw a "Connection Refused" ConnectException, even though the server port is open.
I discovered that in this case I must create InetSocketAddress by explicitly specifying 'localhost' for the hostname or it won't work. e.g.,
InetSocketAddress addr= new InetSocketAddress("localhost", port);
I have two Ubuntu hardy machines with very similar configurations, one in which the implicit hostname works and one in which you must specify 'localhost'. What is going on here? What is supposed to happen when you leave the hostname out of InetSocketAddress?
Thanks,
aaron