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
<christian.muel...@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

Reply via email to