Skip to Main Content
Java Message Service, 2nd Edition
book

Java Message Service, 2nd Edition

by Mark Richards, Richard Monson-Haefel, David A Chappell
May 2009
Intermediate to advanced content levelIntermediate to advanced
330 pages
10h 34m
English
O'Reilly Media, Inc.
Content preview from Java Message Service, 2nd Edition

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); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reactive Systems in Java

Reactive Systems in Java

Clement Escoffier, Ken Finnigan
Java 8 in Action

Java 8 in Action

Mario Fusco, Alan Mycroft, Raoul-Gabriel Urma
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9780596802264Supplemental ContentErrata Page