[jboss-cvs] JBoss Messaging SVN: r6511 - in trunk: examples/javaee and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 21 09:28:46 EDT 2009


Author: jmesnil
Date: 2009-04-21 09:28:46 -0400 (Tue, 21 Apr 2009)
New Revision: 6511

Added:
   trunk/examples/javaee/jms-bridge/
   trunk/examples/javaee/jms-bridge/build.xml
   trunk/examples/javaee/jms-bridge/config/
   trunk/examples/javaee/jms-bridge/config/jms-bridge-jboss-beans.xml
   trunk/examples/javaee/jms-bridge/config/jndi.properties
   trunk/examples/javaee/jms-bridge/readme.html
   trunk/examples/javaee/jms-bridge/src/
   trunk/examples/javaee/jms-bridge/src/org/
   trunk/examples/javaee/jms-bridge/src/org/jboss/
   trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/
   trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/
   trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java
Modified:
   trunk/.classpath
Log:
Java EE JMS Bridge Example

* added example showing how to configure and run a JMS Bridge to bridge 2 JMS destinations
* README is not complete
* the bridge deployment configuration (jms-bridge-jboss-beans.xml) needs more work

Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-21 13:25:55 UTC (rev 6510)
+++ trunk/.classpath	2009-04-21 13:28:46 UTC (rev 6511)
@@ -62,6 +62,7 @@
 	<classpathentry kind="src" path="examples/jms/xa-receive/src"/>
 	<classpathentry kind="src" path="examples/jms/xa-send/src"/>
 	<classpathentry kind="src" path="examples/javaee/ejb-jms-transaction/src"/>
+	<classpathentry kind="src" path="examples/javaee/jms-bridge/src"/>
 	<classpathentry kind="src" path="examples/javaee/mdb/src"/>
 	<classpathentry kind="src" path="examples/javaee/xarecovery/src"/>
 	<classpathentry kind="lib" path="thirdparty/apache-log4j/lib/log4j.jar"/>


Property changes on: trunk/examples/javaee/jms-bridge
___________________________________________________________________
Name: svn:ignore
   + build


Added: trunk/examples/javaee/jms-bridge/build.xml
===================================================================
--- trunk/examples/javaee/jms-bridge/build.xml	                        (rev 0)
+++ trunk/examples/javaee/jms-bridge/build.xml	2009-04-21 13:28:46 UTC (rev 6511)
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- =========================================================================================== -->
+<!--                                                                                             -->
+<!-- JBoss, Home of Professional Open Source                                                     -->
+<!-- Copyright 2005, JBoss Inc., and individual contributors as indicated                        -->
+<!-- by the @authors tag. See the copyright.txt in the distribution for a                        -->
+<!-- full listing of individual contributors.                                                    -->
+<!--                                                                                             -->
+<!-- This is free software; you can redistribute it and/or modify it                             -->
+<!-- under the terms of the GNU Lesser General Public License as                                 -->
+<!-- published by the Free Software Foundation; either version 2.1 of                            -->
+<!-- the License, or (at your option) any later version.                                         -->
+<!--                                                                                             -->
+<!-- This software is distributed in the hope that it will be useful,                            -->
+<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of                              -->
+<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU                            -->
+<!-- Lesser General Public License for more details.                                             -->
+<!--                                                                                             -->
+<!-- You should have received a copy of the GNU Lesser General Public                            -->
+<!-- License along with this software; if not, write to the Free                                 -->
+<!-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA                          -->
+<!-- 02110-1301 USA, or see the FSF site: http://www.fsf.org.                                    -->
+<!--                                                                                             -->
+<!-- =========================================================================================== -->
+
+
+<project default="run" name="JBoss Messaging JMS Bridge Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.javaee.example.JMSBridgeExample"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/javaee/jms-bridge/config/jms-bridge-jboss-beans.xml
===================================================================
--- trunk/examples/javaee/jms-bridge/config/jms-bridge-jboss-beans.xml	                        (rev 0)
+++ trunk/examples/javaee/jms-bridge/config/jms-bridge-jboss-beans.xml	2009-04-21 13:28:46 UTC (rev 6511)
@@ -0,0 +1,151 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+       <bean name="JMSBridge" class="org.jboss.messaging.jms.bridge.impl.JMSBridgeImpl">
+           <!-- JBoss Messaging must be started before the bridge -->
+           <depends>MessagingServer</depends>
+           <constructor>
+               <parameter>
+                   <inject bean="SourceCFF"/>
+               </parameter>
+               <parameter>
+                   <inject bean="TargetCFF"/>
+               </parameter>
+               <parameter>
+                   <inject bean="SourceDestinationFactory"/>
+               </parameter>
+               <parameter>
+                   <inject bean="TargetDestinationFactory"/>
+               </parameter>
+               <parameter><null /></parameter>
+               <parameter><null /></parameter>
+               <parameter><null /></parameter>
+               <parameter><null /></parameter>
+               <parameter><null /></parameter>
+               <parameter>5000</parameter>
+               <parameter>10</parameter>
+               <parameter>2</parameter>
+               <parameter>1</parameter>
+               <parameter>-1</parameter>
+               <parameter><null /></parameter>
+               <parameter><null /></parameter>
+               <parameter>true</parameter>
+               <parameter>true</parameter>
+           </constructor>
+           <property name="transactionManager">
+               <inject bean="RealTransactionManager"/>
+           </property>
+       </bean>
+
+       <bean name="SourceCFF" class="org.jboss.messaging.jms.bridge.impl.JNDIConnectionFactoryFactory">
+           <constructor>
+               <parameter>
+                   <map class="java.util.Hashtable" keyClass="java.lang.String" valueClass="java.lang.String">
+                         <entry>
+                             <key>java.naming.factory.initial</key>
+                             <value>org.jnp.interfaces.NamingContextFactory</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.provider.url</key>
+                             <value>jnp://localhost:1099</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.factory.url.pkgs</key>
+                             <value>org.jboss.naming:org.jnp.interfaces"</value>
+                         </entry>
+                     </map>
+               </parameter>
+               <parameter>/ConnectionFactory</parameter>
+           </constructor>  
+       </bean>
+
+       <bean name="TargetCFF" class="org.jboss.messaging.jms.bridge.impl.JNDIConnectionFactoryFactory">
+           <constructor>
+               <parameter>
+                   <map class="java.util.Hashtable" keyClass="java.lang.String" valueClass="java.lang.String">
+                         <entry>
+                             <key>java.naming.factory.initial</key>
+                             <value>org.jnp.interfaces.NamingContextFactory</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.provider.url</key>
+                             <value>jnp://localhost:1099</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.factory.url.pkgs</key>
+                             <value>org.jboss.naming:org.jnp.interfaces"</value>
+                         </entry>
+                     </map>
+               </parameter>
+               <parameter>/ConnectionFactory</parameter>
+           </constructor>  
+       </bean>
+
+       <bean name="SourceDestinationFactory" class="org.jboss.messaging.jms.bridge.impl.JNDIDestinationFactory">
+           <constructor>
+               <parameter>
+                    <map class="java.util.Hashtable" keyClass="java.lang.String" valueClass="java.lang.String">
+                          <entry>
+                              <key>java.naming.factory.initial</key>
+                              <value>org.jnp.interfaces.NamingContextFactory</value>
+                          </entry>
+                          <entry>
+                              <key>java.naming.provider.url</key>
+                              <value>jnp://localhost:1099</value>
+                          </entry>
+                          <entry>
+                              <key>java.naming.factory.url.pkgs</key>
+                              <value>org.jboss.naming:org.jnp.interfaces"</value>
+                          </entry>
+                      </map>
+               </parameter>
+               <parameter>/queue/source</parameter>
+           </constructor>  
+       </bean>
+
+       <bean name="TargetDestinationFactory" class="org.jboss.messaging.jms.bridge.impl.JNDIDestinationFactory">
+           <constructor>
+               <parameter>
+                   <map class="java.util.Hashtable" keyClass="java.lang.String" valueClass="java.lang.String">
+                         <entry>
+                             <key>java.naming.factory.initial</key>
+                             <value>org.jnp.interfaces.NamingContextFactory</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.provider.url</key>
+                             <value>jnp://localhost:1099</value>
+                         </entry>
+                         <entry>
+                             <key>java.naming.factory.url.pkgs</key>
+                             <value>org.jboss.naming:org.jnp.interfaces"</value>
+                         </entry>
+                     </map>
+               </parameter>
+               <parameter>/queue/target</parameter>
+           </constructor>  
+       </bean>
+
+    <!--
+       <bean name="JNDIProperties" class="java.util.Hashtable">
+           <property key="map">
+            <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+               <entry>
+                   <key>java.naming.factory.initial</key>
+                   <value>org.jnp.interfaces.NamingContextFactory</value>
+               </entry>
+               <entry>
+                   <key>java.naming.provider.url</key>
+                   <value>jnp://localhost:1099</value>
+               </entry>
+               <entry>
+                   <key>java.naming.factory.url.pkgs</key>
+                   <value>org.jboss.naming:org.jnp.interfaces"</value>
+               </entry>
+           </map>
+       </property>
+       </bean>
+    --> 
+       
+
+</deployment>
\ No newline at end of file

Added: trunk/examples/javaee/jms-bridge/config/jndi.properties
===================================================================
--- trunk/examples/javaee/jms-bridge/config/jndi.properties	                        (rev 0)
+++ trunk/examples/javaee/jms-bridge/config/jndi.properties	2009-04-21 13:28:46 UTC (rev 6511)
@@ -0,0 +1,3 @@
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.provider.url=jnp://localhost:1099
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Added: trunk/examples/javaee/jms-bridge/readme.html
===================================================================
--- trunk/examples/javaee/jms-bridge/readme.html	                        (rev 0)
+++ trunk/examples/javaee/jms-bridge/readme.html	2009-04-21 13:28:46 UTC (rev 6511)
@@ -0,0 +1,206 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Bridge Example</title>
+    <link rel="stylesheet" type="text/css" href="../../jms/common/common.css">
+  </head>
+  <body>
+     <h1>JMS Bridge Example</h1>
+     
+     <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>
+         
+     <h3>JBoss AS configuration</h3>
+     
+     <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>:
+     <pre>
+         <code>&lt;queue name="source"&gt;
+            &lt;entry name="/queue/source"/&gt;
+         &lt;/queue&gt;
+         &lt;queue name="target"&gt;
+            &lt;entry name="/queue/target"/&gt;
+         &lt;/queue&gt;</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>
+     
+     <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>
+         
+     <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>
+         <pre>
+             <code>InitialContext initialContext = new InitialContext();</code>
+         </pre>
+
+         <li>We look up the EJB</li>
+         <pre>
+             <code>SendMessageService service = (SendMessageService)initialContext.lookup("mdb-example/SendMessageBean/remote");</code>
+         </pre>
+
+         <li>We create the DB table which will be updated if it does not already exist</li>
+         <pre>
+             <code>service.createTable();</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>
+         <pre>
+             <code>service.sendAndUpdate("This is a text message");</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>
+         <pre>
+             <code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
+         </pre>
+
+         <li>We lookup the JMS queue</li>
+         <pre>
+             <code>Queue queue = (Queue)initialContext.lookup("queue/testQueue");</code>
+         </pre>
+
+         <li>We create a connection, a session and a message consumer for the queue</li>
+         <pre>
+             <code>connection = cf.createConnection();
+             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+             MessageConsumer consumer = session.createConsumer(queue);</code>
+         </pre>
+
+         <li>We start the JMS connection</li>
+         <pre>
+             <code>connection.start();</code>
+         </pre>
+
+         <li>We receive a message from the queue. It corresponds to the message sent by the EJB</li>
+         <pre>
+             <code>TextMessage messageReceived = (TextMessage)consumer.receive(5000);
+             System.out.println("Received message: " + messageReceived.getText() +
+                                         " (" +  messageReceived.getJMSMessageID() + ")");
+             </code>
+         </pre>
+
+         <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();
+                }
+                if (connection != null)
+                {
+                   connection.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>
+     </ul>
+
+  </body>
+</html>
\ No newline at end of file

Added: trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java
===================================================================
--- trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java	                        (rev 0)
+++ trunk/examples/javaee/jms-bridge/src/org/jboss/javaee/example/JMSBridgeExample.java	2009-04-21 13:28:46 UTC (rev 6511)
@@ -0,0 +1,107 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.javaee.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+/**
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ */
+public class JMSBridgeExample
+{
+   public static void main(String[] args) throws Exception
+   {
+      InitialContext initialContext = null;
+      Connection sourceConnection = null;
+      Connection targetConnection = null;
+      try
+      {
+         // Step 1. Obtain an Initial Context
+         initialContext = new InitialContext();
+
+         // Step 2. Lookup the JMS connection factory
+         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+         // Step 3. Lookup the source queue
+         Queue sourceQueue = (Queue)initialContext.lookup("/queue/source");
+
+         // Step 4. Create a connection, a session and a message producer for the *source* queue
+         sourceConnection = cf.createConnection();
+         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
+         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());
+
+         // Step 6. Close the *source* connection
+         sourceConnection.close();
+
+         // Step 7. Lookup the *target* queue
+         Queue targetQueue = (Queue)initialContext.lookup("/queue/target");
+
+         // Step 8. Create a connection, a session and a message consumer for the *target* queue
+         targetConnection = cf.createConnection();
+         Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);
+
+         // Step 9. Start the connection to receive messages from the *targe* queue
+         targetConnection.start();
+
+         // Step 10. Receive a message from the *target* queue
+         TextMessage messageReceived = (TextMessage)targetConsumer.receive(5000);
+         System.out.format("Received 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"));
+      }
+      finally
+      {
+         // Step 13. Be sure to close the resources!
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
+         if (sourceConnection != null)
+         {
+            sourceConnection.close();
+         }
+         if (targetConnection != null)
+         {
+            targetConnection.close();
+         }
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list