Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Thursday, October 15, 2015

Messaging Pattern: Request-Reply

Messaging Pattern: Request-Reply





Request-Reply has two participants:


  1. Consumer – Sends a request message and waits for a reply message.
  2. Provider – Receives the request message and responds with a reply message


Example: SOAP 1.1 Messages


SOAP messages come in Request-Reply pairs. A SOAP request message indicates a service the
sender wishes to invoke on the receiver, whereas a SOAP response message contains the result of
the service invocation. The response message either contains a result value or a fault–the SOAP
equivalent of an exception.

Example: SOAP 1.2 Response Message Exchange Pattern


SOAP 1.2 introduces an explicit Request-Response Message Exchange Pattern. This patterns
describes a separate, potentially asynchronous response to a SOAP request.

Example: JMS Requestor Objects

JMS also provides QueueRequestor, a simple class for sending requests and receiving replies. A
requestor contains a QueueSender for sending requests and a QueueReceiver for receiving replies.
Each requestor creates its own temporary queue for receiving replies and specifies that in the
request's reply-to property. A requestor makes sending a request and receiving a reply very simple:

Queue Sender

        // get the initial context
| InitialContext ctx = new InitialContext();
|
| // lookup the queue object
| Queue queue = (Queue) ctx.lookup("queue/queue0");
|
| // lookup the queue connection factory
| QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
| lookup("queue/connectionFactory");
|
| // create a queue connection
| QueueConnection queueConn = connFactory.createQueueConnection();
|
| // create a queue session
| QueueSession queueSession = queueConn.createQueueSession(false,
| Session.DUPS_OK_ACKNOWLEDGE);
|
| // create a queue sender
| QueueSender queueSender = queueSession.createSender(queue);
| queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
|
| // create a simple message to say "Hello"
| TextMessage message = queueSession.createTextMessage("Hello");
|
| // send the message
| queueSender.send(message);
|
| // print what we did
| System.out.println("sent: " + message.getText());
|
| // close the queue connection
| queueConn.close();

Queue Receiver


        // get the initial context
| InitialContext ctx = new InitialContext();
|
| // lookup the queue object
| Queue queue = (Queue) ctx.lookup("queue/queue0");
|
| // lookup the queue connection factory
| QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
| lookup("queue/connectionFactory");
|
| // create a queue connection
| QueueConnection queueConn = connFactory.createQueueConnection();
|
| // create a queue session
| QueueSession queueSession = queueConn.createQueueSession(false,
| Session.AUTO_ACKNOWLEDGE);
|
| // create a queue receiver
| QueueReceiver queueReceiver = queueSession.createReceiver(queue);
|
| // start the connection
| queueConn.start();
|
| // receive a message
| TextMessage message = (TextMessage) queueReceiver.receive();
|
| // print the message
| System.out.println("received: " + message.getText());
|
| // close the queue connection
| queueConn.close();

No comments:

Post a Comment