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.

Reply via email to