[jboss-cvs] JBoss Messaging SVN: r6303 - in trunk/examples/jms/request-reply: src/org/jboss/jms/example and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Apr 3 12:23:34 EDT 2009
Author: gaohoward
Date: 2009-04-03 12:23:33 -0400 (Fri, 03 Apr 2009)
New Revision: 6303
Modified:
trunk/examples/jms/request-reply/readme.html
trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
Log:
add a request map, close the initial context in the end.
Modified: trunk/examples/jms/request-reply/readme.html
===================================================================
--- trunk/examples/jms/request-reply/readme.html 2009-04-03 15:53:41 UTC (rev 6302)
+++ trunk/examples/jms/request-reply/readme.html 2009-04-03 16:23:33 UTC (rev 6303)
@@ -6,7 +6,7 @@
<body>
<h1>JMS Request-Reply Example</h1>
<br>
- <p>This example shows you how to handle a request message and receive a reply. To get a reply message, the requesting client creates a temporary queue. Then it sends out the request message with JMSReplyTo set to the temporary queue and a JMSCorrelationID to identify the request. The request message is handled by a SimpleRequestServer, who is listening to the request queue for incoming requests. If a request message has arrived, it extracts the reply queue from the request message by JMSReplyTo header, and sends back a reply message. To let the client know to which request message a reply message is related, the server also set the JMSCorrelationID header to the reply message.</p>
+ <p>This example shows you how to handle a request message and receive a reply. To get a reply message, the requesting client creates a temporary queue. Then it sends out the request message with JMSReplyTo set to the temporary queue. The request message is handled by a SimpleRequestServer, who is listening to the request queue for incoming requests. If a request message has arrived, it extracts the reply queue from the request message by JMSReplyTo header, and sends back a reply message. To let the client know to which request message a reply message is related, the server also set the JMSCorrelationID with the request message's JMSMessageID header to the reply message.</p>
<p>Request/Reply style messaging is supported through standard JMS message headers JMSReplyTo and JMSCorrelationID. This is often used in request-reply style communications between applications.
Whenever a client sends a message that expects a response, it can use this mechanism to implement. please consult the JMS 1.1 specification for full details.</p>
@@ -24,7 +24,7 @@
<li>We need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
<pre>
- <code>InitialContext initialContext = getContext();</code>
+ <code>initialContext = getContext();</code>
</pre>
<li>We lookup the queue for sending the request message</li>
@@ -77,14 +77,14 @@
<code>requestMsg.setJMSReplyTo(replyQueue);</code>
</pre>
- <li>We set the CorrelationID so that it can be linked with corresponding reply message</li>
+ <li>We sent the request message</li>
<pre>
- <code>requestMsg.setJMSCorrelationID("jbm-id: 0000001");</code>
+ <code>producer.send(requestMsg);</code>
</pre>
- <li>We sent the request message</li>
+ <li>We put the request message to the map. Later we use it to check out which request message a reply message is for. Here we use the MessageID as the correlation id (JMSCorrelationID). You don't have to use it though. You can use some arbitrary string for example.</li>
<pre>
- <code>producer.send(requestMsg);</code>
+ <code>requestMap.put(requestMsg.getJMSMessageID(), requestMsg);</code>
</pre>
<li>We receive the reply message</li>
@@ -92,6 +92,11 @@
<code>TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive();</code>
</pre>
+ <li>We check out which request message is this reply message sent for. Here we just have one request message for illustrative purpose. In real world there may be many requests and many replies.</li>
+ <pre>
+ <code>TextMessage matchedMessage = requestMap.get(replyMessageReceived.getJMSCorrelationID());</code>
+ </pre>
+
<li>We close the consumer and producer on the replyQueue</li>
<pre>
<code>replyConsumer.close();</code>
@@ -107,7 +112,7 @@
<code>server.shutdown();</code>
</pre>
- <li>And finally, <b>always</b> remember to close your JMS connections after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
+ <li>And finally, <b>always</b> remember to close your JMS connections after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects. Also remember close the JNDI initial context.</li>
<pre>
<code>
@@ -116,9 +121,14 @@
{
if (connection != null)
{
- // Step 19. Be sure to close our JMS resources!
+ // Step 20. Be sure to close our JMS resources!
connection.close();
}
+ //Step 21. Also close the initialContext!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
}
</code>
</pre>
Modified: trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
===================================================================
--- trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java 2009-04-03 15:53:41 UTC (rev 6302)
+++ trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java 2009-04-03 16:23:33 UTC (rev 6303)
@@ -21,6 +21,9 @@
*/
package org.jboss.jms.example;
+import java.util.HashMap;
+import java.util.Map;
+
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
@@ -42,6 +45,8 @@
*/
public class RequestReplyExample extends JMSExample
{
+ private Map<String, TextMessage> requestMap = new HashMap<String, TextMessage>();
+
public static void main(String[] args)
{
new RequestReplyExample().run(args);
@@ -50,15 +55,16 @@
public void runExample() throws Exception
{
Connection connection = null;
+ InitialContext initialContext = null;
+
try
{
-
//Step 1. Start the request server
SimpleRequestServer server = new SimpleRequestServer();
server.start();
//Step 2. Create an initial context to perform the JNDI lookup.
- InitialContext initialContext = getContext();
+ initialContext = getContext();
//Step 3. Lookup the queue for sending the request message
Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");
@@ -90,20 +96,28 @@
//Step 12. Set the ReplyTo header so that the request receiver knows where to send the reply.
requestMsg.setJMSReplyTo(replyQueue);
- //Step 13. Set the CorrelationID so that it can be linked with corresponding reply message
- requestMsg.setJMSCorrelationID("jbm-id: 0000001");
-
- //Step 14. Sent the request message
+ //Step 13. Sent the request message
producer.send(requestMsg);
System.out.println("Request message sent.");
+ //Step 14. Put the request message to the map. Later we can use it to
+ //check out which request message a reply message is for. Here we use the MessageID as the
+ //correlation id (JMSCorrelationID). You don't have to use it though. You can use some arbitrary string for example.
+ requestMap.put(requestMsg.getJMSMessageID(), requestMsg);
+
//Step 15. Receive the reply message.
TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive();
System.out.println("Received reply: " + replyMessageReceived.getText());
System.out.println("CorrelatedId: " + replyMessageReceived.getJMSCorrelationID());
+ //Step 16. Check out which request message is this reply message sent for.
+ //Here we just have one request message for illustrative purpose. In real world there may be many requests and many replies.
+ TextMessage matchedMessage = requestMap.get(replyMessageReceived.getJMSCorrelationID());
+
+ System.out.println("We found matched request: " + matchedMessage.getText());
+
//Step 17. close the consumer.
replyConsumer.close();
@@ -116,11 +130,16 @@
}
finally
{
- //Step 19. Be sure to close our JMS resources!
+ //Step 20. Be sure to close our JMS resources!
if(connection != null)
{
connection.close();
}
+ //Step 21. Also close the initialContext!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
}
}
@@ -175,8 +194,8 @@
// Create the reply message
TextMessage replyMessage = session.createTextMessage("A reply message");
- // Set the CorrelationID
- replyMessage.setJMSCorrelationID(request.getJMSCorrelationID());
+ // Set the CorrelationID, using message id.
+ replyMessage.setJMSCorrelationID(request.getJMSMessageID());
// Send out the reply message
replyProducer.send(replyDestination, replyMessage);
More information about the jboss-cvs-commits
mailing list