[jboss-cvs] JBoss Messaging SVN: r6514 - in trunk/examples/javaee/jms-bridge: src/org/jboss/javaee/example and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 21 10:45:47 EDT 2009
Author: jmesnil
Date: 2009-04-21 10:45:45 -0400 (Tue, 21 Apr 2009)
New Revision: 6514
Modified:
trunk/examples/javaee/jms-bridge/readme.html
trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java
Log:
Java EE JMS Bridge Example
* enhanced readme (bridge configuration is missing) + minor code modif
Modified: trunk/examples/javaee/jms-bridge/readme.html
===================================================================
--- trunk/examples/javaee/jms-bridge/readme.html 2009-04-21 14:17:44 UTC (rev 6513)
+++ trunk/examples/javaee/jms-bridge/readme.html 2009-04-21 14:45:45 UTC (rev 6514)
@@ -6,6 +6,20 @@
<body>
<h1>JMS Bridge Example</h1>
+ <p>This example shows how to configure and run a JMS Bridge in JBoss AS 5.<br />
+ A bridge receives messages from a <em>source</em> JMS destination and resend them to a <em>target</em> destination.</p>
+ <p>The source and target destinations can be on different servers, even from different JMS providers. For example, you can use this
+ JMS Bridge to bridge a legacy JMS provider to JBoss Messaging during migration.</p>
+
+ <p>This example will show how to configure and run the simplest bridge:</p>
+ <ul>
+ <li>the source and target destinations are hosted by a single JBoss AS 5 instance</li>
+ <li>the bridge is run on the same JBoss AS 5 instance</li>
+ <li>every time a message is consumed by the bridge from the source, it is resent to the target</li>
+ <li>The application client will send a message to the source and consume the "same" message from the target to
+ show that the two destinations were indeed bridged.</li>
+ </ul>
+
<h2>Example configuration</h2>
<p>To run the example, you need to download JBoss AS 5.x and create a configuration for JBoss Messaging.</p>
@@ -14,7 +28,10 @@
<p>Please refer to JBoss Messaging documentation to deploy it for JBoss AS 5 <span class="missing-doc">add a link to the doc</span></p>
- <p>Add 2 queues to <code>${JBOSS_HOME}/server/jbm2/default/deploy/messaging.sar/jbm-jms.xml</code>:
+ <h3>JBoss Messaging configuration</h3>
+
+ <p>We will add two queues to JBoss Messaging JMS configuration in
+ <code>${JBOSS_HOME}/server/jbm2/default/deploy/messaging.sar/jbm-jms.xml</code></p>
<pre>
<code><queue name="source">
<entry name="/queue/source"/>
@@ -24,182 +41,129 @@
</queue></code>
</pre>
- <p>Copy <a href="config/jms-bridge-jboss-beans.xml">jms-bridge-jboss-beans.xml</a> to <code>${JBOSS_HOME}/server/jbm2/default/deploy/</code>
-
+ <h3>JMS Bridge configuration</h3>
+
+ <p>The JMS Bridge is configured using JBoss microcontainer.<br />
+ To setup the bridge in JBoss AS 5, copy <a href="config/jms-bridge-jboss-beans.xml">jms-bridge-jboss-beans.xml</a> to <code>${JBOSS_HOME}/server/jbm2/default/deploy/</code></p>
+
+ <p><span class="missing-doc">Explain the bridge configuration</span></p>
+
<h2>Example step-by-step</h2>
<p><em>Once JBoss AS 5 is configured and started, type <code>ant</code> to run the example</em>.</p>
- The example code is composed of two main classes:
- <ul>
- <li><code>EJBExample</code></li> - the example application
- <li><code>SendMessageBean</code></li> - a Stateless EJB
- </ul>
-
- <h3>Example Application</h3>
-
- <p>Let's take a look at EJBClientExample first.</p>
+ <p>The example is simple: the application will send a message to the <em>source</em> queue and consume the same message
+ from the <em>target</em> queue.</p>
+ <p>The bridge was configured in <a href="config/jms-bridge-jboss-beans.xml">jms-bridge-jboss-beans.xml</a> to bridge these two queues.</p>
<ol>
- <li>First 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 <a href="config/jndi.properties">jndi.properties</a></li>
+ <li>First we need to get an initial context so we can look up the JMS resources
</li>
<pre>
- <code>InitialContext initialContext = new InitialContext();</code>
+ <code>initialContext = new InitialContext();</code>
</pre>
- <li>We look up the EJB</li>
+ <li>We look up the JMS ConnectionFactory</li>
<pre>
- <code>SendMessageService service = (SendMessageService)initialContext.lookup("mdb-example/SendMessageBean/remote");</code>
+ <code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
- <li>We create the DB table which will be updated if it does not already exist</li>
+ <p><em>First, we will send a message to the <em>source</em> queue</em>.</p>
+
+ <li>We look up the JMS <em>source</em> queue</li>
<pre>
- <code>service.createTable();</code>
+ <code>Queue sourceQueue = (Queue)initialContext.lookup("/queue/source");</code>
</pre>
- <li>We invoke the EJB's <code>sendAndUpdate</code> method. This method will send a JMS text message (with the text passed in parameter)
- and insert a row in the database table with the text and the message's JMS Message ID</li>
+ <li>We create a JMS connection, a session and a message producer for the <em>source</em> queue</li>
<pre>
- <code>service.sendAndUpdate("This is a text message");</code>
+ <code>sourceConnection = cf.createConnection();
+ Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer sourceProducer = sourceSession.createProducer(sourceQueue);</code>
</pre>
- <p><em>We will now consume the JMS message which was sent by the EJB at step 4.</em></p>
-
- <li>We look up the JMS connection factory</li>
+ <li>We create and send a message to the <em>source</em> queue. We also display its Message ID.</li>
<pre>
- <code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
+ <code>TextMessage message = sourceSession.createTextMessage("this is a text message");
+ sourceProducer.send(message);
+ System.out.format("Sent message to %s: %s\n",
+ ((Queue)message.getJMSDestination()).getQueueName(),
+ message.getText());
+ System.out.format("Message ID : %s\n", message.getJMSMessageID());</code>
</pre>
- <li>We lookup the JMS queue</li>
+ <li>We close the <em>source</em> connection</li>
<pre>
- <code>Queue queue = (Queue)initialContext.lookup("queue/testQueue");</code>
+ <code>sourceConnection.close();</code>
</pre>
+
+ <p><em>Now that a message has been sent to the <em>source</em> queue, we will consume a message
+ from the <em>target</em> queue.<br />
+ If the bridge runs correctly, it will have consumed the message from the <em>source</em> and
+ resent it to the <em>target</em> so that we can consume a message from it.</em></p>
+
+ <li>We look up the JMS <em>target</em> queue</li>
+ <pre>
+ <code>Queue targetQueue = (Queue)initialContext.lookup("/queue/target");</code>
+ </pre>
- <li>We create a connection, a session and a message consumer for the queue</li>
+ <li>We create a connection, a session and a message consumer for the <em>target</em> queue</li>
<pre>
- <code>connection = cf.createConnection();
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer consumer = session.createConsumer(queue);</code>
+ <code>targetConnection = cf.createConnection();
+ Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);</code>
</pre>
- <li>We start the JMS connection</li>
+ <li>We start the JMS connection to receive messages from the <em>target</em></li>
<pre>
- <code>connection.start();</code>
+ <code>targetConnection.start();</code>
</pre>
- <li>We receive a message from the queue. It corresponds to the message sent by the EJB</li>
+ <li>We receive a message from the <em>target</em> queue. It has the same content than the message sent to the <em>source</em> queue</li>
<pre>
<code>TextMessage messageReceived = (TextMessage)consumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText() +
" (" + messageReceived.getJMSMessageID() + ")");
</code>
</pre>
+
+ <li>We now display the received message ID. It is not the same than the ID of the message sent to the <em>source</em> queue.
+ The message received from the <em>target</em> queue was sent by the bridge, not by the <em>source</em> message producer</li>
+ <pre>
+ <code>System.out.format("Message ID : %s\n", messageReceived.getJMSMessageID());</code>
+ </pre>
+
+ <li>If you need to retrieve the message ID of the message <em>sent to the source</em>, you can use the property <code>JBM_BRIDGE_MSG_ID_LIST</code></li>
+ <pre>
+ <code>System.out.format("Bridged Message ID : %s\n", messageReceived.getStringProperty("JBM_BRIDGE_MSG_ID_LIST"));</code>
+ </pre>
+
+ <li>And finally, <b>always</b> remember to close the JMS connections and resources 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 and resources 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>
-
<pre>
<code>finally
{
if (initialContext != null)
{
- initialContext.close();
+ initialContext.close();
}
- if (connection != null)
+ if (sourceConnection != null)
{
- connection.close();
+ sourceConnection.close();
}
+ if (targetConnection != null)
+ {
+ targetConnection.close();
+ }
}</code>
</pre>
</ol>
- <h3>EJB Example</h3>
-
- <p>Let's now take a look at the EJB example</p>
-
- <ol>
- <li>First, we create a new initial context</li>
- <pre>
- <code>ic = new InitialContext();</code>
- </pre>
-
- <li>We look up the JMS <em>XA</em> Connection Factory (which is bound to <code>java:/JmsXA</code>)</li>
- <pre>
- <code>ConnectionFactory cf = (ConnectionFactory)ic.lookup("java:/JmsXA");</code>
- </pre>
-
- <li>We look up the JMS Queue</li>
- <pre>
- <code>Queue queue = (Queue)ic.lookup("queue/testQueue");</code>
- </pre>
-
- <li>We create a JMS connection, a session and a message producer for the queue</li>
- <pre>
- <code>jmsConnection = cf.createConnection();
- Session session = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer messageProducer = session.createProducer(queue);</code>
- </pre>
-
- <li>We create a text message with the text passed in parameter of the EJB method</li>
- <pre>
- <code>TextMessage message = session.createTextMessage(text);</code>
- </pre>
-
- <li>We send the message to the queue</li>
- <pre>
- <code>messageProducer.send(message);
- System.out.println("Sent message: " + message.getText() + "(" + message.getJMSMessageID() + ")");</code>
- </pre>
-
- <li>We now lookup the JDBC <em>XA</em> DataSource</li>
- <pre>
- <code>DataSource ds = (DataSource)ic.lookup("java:/XADS");</code>
- </pre>
-
- <li>We retrieve a JDBC connection</li>
- <pre>
- <code>jdbcConnection = ds.getConnection();</code>
- </pre>
-
- <li>We create a prepared statement to insert the text and message's ID in the DB table</li>
- <pre>
- <code>PreparedStatement pr = jdbcConnection.prepareStatement("INSERT INTO " + TABLE
- + " (id, text) VALUES ('" + message.getJMSMessageID() + "', '" + text + "');");</code>
- </pre>
-
- <li>We execute the prepared statement</li>
- <pre>
- <code>pr.execute();</code>
- </pre>
-
- <li>We close the prepared statement</li>
- <pre>
- <code>pr.close();</code>
- </pre>
-
- <li>And finally, <b>always</b> remember to close all your connections and resources (for both JMS and JDBC) after use, in a <code>finally</code> block.</li>
- <pre>
- <code>finally
- {
- if (ic != null)
- {
- ic.close();
- }
- if (jmsConnection != null)
- {
- jmsConnection.close();
- }
- if (jdbcConnection != null)
- {
- jdbcConnection.close();
- }
- }</code>
- </pre>
-
<h2>More information</h2>
<ul>
<li><span class="missing-doc">JBoss Messsaging deployment in JBoss AS 5 </span></li>
- <li><a href="http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Administration_And_Configuration_Guide/5/html/alternative_DBs.html">Use Alternative Databases with JBoss AS</a></li>
+ <li><span class="missing-doc">JMS Bridge configuration</a></li>
</ul>
</body>
Modified: trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java
===================================================================
--- trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java 2009-04-21 14:17:44 UTC (rev 6513)
+++ trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java 2009-04-21 14:45:45 UTC (rev 6514)
@@ -56,12 +56,13 @@
Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sourceProducer = sourceSession.createProducer(sourceQueue);
- // Step 5. Create and send a text message on the *source* queue
+ // Step 5. Create and send a text message to the *source* queue
TextMessage message = sourceSession.createTextMessage("this is a text message");
sourceProducer.send(message);
System.out.format("Sent message to %s: %s\n",
((Queue)message.getJMSDestination()).getQueueName(),
message.getText());
+ System.out.format("Message ID : %s\n", message.getJMSMessageID());
// Step 6. Close the *source* connection
sourceConnection.close();
@@ -79,11 +80,13 @@
// Step 10. Receive a message from the *target* queue
TextMessage messageReceived = (TextMessage)targetConsumer.receive(5000);
- System.out.format("Received from %s: %s\n",
+ System.out.format("\nReceived from %s: %s\n",
((Queue)messageReceived.getJMSDestination()).getQueueName(),
messageReceived.getText());
+
// Step 11. Display the received message's ID
System.out.format("Message ID : %s\n", messageReceived.getJMSMessageID());
+
// Step 12. Display the message ID of the message received by the *bridge*
System.out.format("Bridged Message ID : %s\n", messageReceived.getStringProperty("JBM_BRIDGE_MSG_ID_LIST"));
}
More information about the jboss-cvs-commits
mailing list