Request/Reply Messaging Design

In Chapter 4 we introduced point-to-point messaging using a simple request/reply model. In this scenario the message producer (QBorrower) sent a loan request to the message consumer (QLender) and waited (blocking wait) for a response from the QLender on whether the loan was approved or denied. To implement the request/reply model we used a technique known as message correlation, where messages sent to the response queue were correlated with the original message using the JMSMessageID and JMSCorrelationID. The following are the original QBorrower and QLender listings used to implement the request/reply scenario.

public class QBorrower {
   ...
   public QBorrower(String queuecf, String requestQueue, 
                   String responseQueue) {    
      try {
         ...
         // Lookup the request and response queues
         requestQ = (Queue)ctx.lookup(requestQueue);
         responseQ = (Queue)ctx.lookup(responseQueue);
      }
      ...
   }

   private void sendLoanRequest(double salary, double loanAmt) {
      try {
         // Create JMS message
         MapMessage msg = qSession.createMapMessage();
         msg.setDouble("Salary", salary);
         msg.setDouble("LoanAmount", loanAmt);
         msg.setJMSReplyTo(responseQ);

         // Create the sender and send the message
         QueueSender qSender = qSession.createSender(requestQ);
         qSender.send(msg);
        
         // Wait to see if the loan request was accepted or declined
         String filter = 
            "JMSCorrelationID = '" + msg.getJMSMessageID() + "'";
         QueueReceiver qReceiver = qSession.createReceiver(responseQ, filter);
         TextMessage tmsg = (TextMessage)qReceiver.receive(30000); ...

Get Java Message Service, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.