[hornetq-commits] JBoss hornetq SVN: r9631 - in trunk: examples/jms/embedded/src/org/hornetq/jms/example and 17 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Sep 2 19:21:39 EDT 2010


Author: bill.burke at jboss.com
Date: 2010-09-02 19:21:38 -0400 (Thu, 02 Sep 2010)
New Revision: 9631

Added:
   trunk/examples/jms/embedded-simple/
   trunk/examples/jms/embedded-simple/build.bat
   trunk/examples/jms/embedded-simple/build.sh
   trunk/examples/jms/embedded-simple/build.xml
   trunk/examples/jms/embedded-simple/readme.html
   trunk/examples/jms/embedded-simple/server0/
   trunk/examples/jms/embedded-simple/server0/hornetq-configuration.xml
   trunk/examples/jms/embedded-simple/server0/hornetq-jms.xml
   trunk/examples/jms/embedded-simple/server0/hornetq-users.xml
   trunk/examples/jms/embedded-simple/src/
   trunk/examples/jms/embedded-simple/src/org/
   trunk/examples/jms/embedded-simple/src/org/hornetq/
   trunk/examples/jms/embedded-simple/src/org/hornetq/jms/
   trunk/examples/jms/embedded-simple/src/org/hornetq/jms/example/
   trunk/examples/jms/embedded-simple/src/org/hornetq/jms/example/EmbeddedExample.java
   trunk/examples/jms/spring-integration/
   trunk/examples/jms/spring-integration/build.bat
   trunk/examples/jms/spring-integration/build.sh
   trunk/examples/jms/spring-integration/build.xml
   trunk/examples/jms/spring-integration/readme.html
   trunk/examples/jms/spring-integration/server0/
   trunk/examples/jms/spring-integration/server0/hornetq-configuration.xml
   trunk/examples/jms/spring-integration/server0/hornetq-jms.xml
   trunk/examples/jms/spring-integration/server0/hornetq-users.xml
   trunk/examples/jms/spring-integration/server0/spring-jms-beans.xml
   trunk/examples/jms/spring-integration/src/
   trunk/examples/jms/spring-integration/src/org/
   trunk/examples/jms/spring-integration/src/org/hornetq/
   trunk/examples/jms/spring-integration/src/org/hornetq/jms/
   trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/
   trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/ExampleListener.java
   trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/MessageSender.java
   trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/SpringExample.java
Modified:
   trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java
   trunk/src/main/org/hornetq/core/registry/MapBindingRegistry.java
   trunk/src/main/org/hornetq/core/server/embedded/EmbeddedHornetQ.java
   trunk/src/main/org/hornetq/jms/server/embedded/EmbeddedJMS.java
Log:
added new examples for spring and embedded, reworked embedded example



Modified: trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java
===================================================================
--- trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java	2010-09-02 23:20:36 UTC (rev 9630)
+++ trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -12,42 +12,34 @@
  */
 package org.hornetq.jms.example;
 
-import java.util.Date;
-import java.util.Hashtable;
-
-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.Context;
-import javax.naming.InitialContext;
-
 import org.hornetq.api.core.TransportConfiguration;
 import org.hornetq.core.config.Configuration;
 import org.hornetq.core.config.impl.ConfigurationImpl;
 import org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory;
 import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
-import org.hornetq.core.server.HornetQServer;
-import org.hornetq.core.server.HornetQServers;
-import org.hornetq.jms.server.JMSServerManager;
 import org.hornetq.jms.server.config.ConnectionFactoryConfiguration;
 import org.hornetq.jms.server.config.JMSConfiguration;
 import org.hornetq.jms.server.config.JMSQueueConfiguration;
 import org.hornetq.jms.server.config.impl.ConnectionFactoryConfigurationImpl;
 import org.hornetq.jms.server.config.impl.JMSConfigurationImpl;
 import org.hornetq.jms.server.config.impl.JMSQueueConfigurationImpl;
-import org.hornetq.jms.server.impl.JMSServerManagerImpl;
-import org.jnp.server.Main;
-import org.jnp.server.NamingBeanImpl;
+import org.hornetq.jms.server.embedded.EmbeddedJMS;
 
+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 java.util.Date;
+
 /**
  * This example demonstrates how to run a HornetQ embedded with JMS
  * 
  * @author <a href="clebert.suconic at jboss.com">Clebert Suconic</a>
  * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ * @author <a href="mailto:bburke at redhat.com">Bill Burke</a>
  */
 public class EmbeddedExample
 {
@@ -64,51 +56,30 @@
          configuration.getAcceptorConfigurations()
                       .add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
 
-         // Step 2. Create HornetQ core server
-         HornetQServer hornetqServer = HornetQServers.newHornetQServer(configuration);
-
-         // Step 3. Create and start the JNDI server
-         System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
-         NamingBeanImpl naming = new NamingBeanImpl();
-         naming.start();
-         Main jndiServer = new Main();
-         jndiServer.setNamingInfo(naming);
-         jndiServer.setPort(1099);
-         jndiServer.setBindAddress("localhost");
-         jndiServer.setRmiPort(1098);
-         jndiServer.setRmiBindAddress("localhost");
-         jndiServer.start();
-
-         // Step 4. Create the JMS configuration
+         // Step 2. Create the JMS configuration
          JMSConfiguration jmsConfig = new JMSConfigurationImpl();
 
-         // Step 5. Configure context used to bind the JMS resources to JNDI
-         Hashtable<String, String> env = new Hashtable<String, String>();
-         env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
-         env.put("java.naming.provider.url", "jnp://localhost:1099");
-         env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
-         Context context = new InitialContext(env);
-         jmsConfig.setContext(context);
-
-         // Step 6. Configure the JMS ConnectionFactory
+         // Step 3. Configure the JMS ConnectionFactory
          TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());
          ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
          jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
 
-         // Step 7. Configure the JMS Queue
+         // Step 4. Configure the JMS Queue
          JMSQueueConfiguration queueConfig = new JMSQueueConfigurationImpl("queue1", null, false, "/queue/queue1");
          jmsConfig.getQueueConfigurations().add(queueConfig);
 
-         // Step 8. Start the JMS Server using the HornetQ core server and the JMS configuration
-         JMSServerManager jmsServer = new JMSServerManagerImpl(hornetqServer, jmsConfig);
+         // Step 5. Start the JMS Server using the HornetQ core server and the JMS configuration
+         EmbeddedJMS jmsServer = new EmbeddedJMS();
+         jmsServer.setConfiguration(configuration);
+         jmsServer.setJmsConfiguration(jmsConfig);
          jmsServer.start();
          System.out.println("Started Embedded JMS Server");
 
-         // Step 9. Lookup JMS resources defined in the configuration
-         ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
-         Queue queue = (Queue)context.lookup("/queue/queue1");
+         // Step 6. Lookup JMS resources defined in the configuration
+         ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("/cf");
+         Queue queue = (Queue)jmsServer.lookup("/queue/queue1");
 
-         // Step 10. Send and receive a message using JMS API
+         // Step 7. Send and receive a message using JMS API
          Connection connection = null;
          try
          {
@@ -134,9 +105,6 @@
             jmsServer.stop();
             System.out.println("Stopped the JMS Server");
 
-            // Step 12. Stop the JNDI server
-            naming.stop();
-            jndiServer.stop();
             System.exit(0);
          }
       }

Added: trunk/examples/jms/embedded-simple/build.bat
===================================================================
--- trunk/examples/jms/embedded-simple/build.bat	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/build.bat	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,13 @@
+ at echo off
+
+set "OVERRIDE_ANT_HOME=..\..\..\tools\ant"
+
+if exist "..\..\..\src\bin\build.bat" (
+   rem running from TRUNK
+   call ..\..\..\src\bin\build.bat %*
+) else (
+   rem running from the distro
+   call ..\..\..\bin\build.bat %*
+)
+
+set "OVERRIDE_ANT_HOME="

Added: trunk/examples/jms/embedded-simple/build.sh
===================================================================
--- trunk/examples/jms/embedded-simple/build.sh	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/build.sh	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+OVERRIDE_ANT_HOME=../../../tools/ant
+export OVERRIDE_ANT_HOME
+
+if [ -f "../../../src/bin/build.sh" ]; then
+   # running from TRUNK
+   ../../../src/bin/build.sh "$@"
+else
+   # running from the distro
+   ../../../bin/build.sh "$@"
+fi
+
+
+


Property changes on: trunk/examples/jms/embedded-simple/build.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/examples/jms/embedded-simple/build.xml
===================================================================
--- trunk/examples/jms/embedded-simple/build.xml	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/build.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+      <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+      ]>
+<!--
+  ~ Copyright 2009 Red Hat, Inc.
+  ~ Red Hat licenses this file to you under the Apache License, version
+  ~ 2.0 (the "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+  ~ implied.  See the License for the specific language governing
+  ~ permissions and limitations under the License.
+  -->
+
+<project default="run" name="HornetQ Embedded Example">
+
+   <import file="../../common/build.xml"/>
+
+   <path id="local.classpath">
+	  <fileset dir="${hornetq.jars.dir}">
+	       <include name="hornetq-core.jar"/>
+	       <include name="hornetq-jms.jar"/>
+	  </fileset>
+	 	
+	  <fileset dir="${jars.dir}">
+	     <include name="**/jboss-jms-api.jar"/>
+	     <include name="**/netty.jar"/>
+	  </fileset>
+
+   	  <pathelement location="${classes.dir}"/>	   	
+   	  <pathelement location="server0"/>	   	
+   </path>
+
+   <target name="run" depends="compile">   	
+    <java classname="org.hornetq.jms.example.EmbeddedExample" fork="true" resultproperty="example-result">
+       <jvmarg value="-Xms50M"/>
+       <jvmarg value="-Xmx50M"/>
+       <classpath refid="local.classpath"/>
+    </java>
+    
+    <!-- if the example exited with a result value != 0, we fail the build -->
+    <fail message="Example EmbeddedExample failed">
+       <condition>
+          <not>
+             <equals arg1="${example-result}" arg2="0"/>
+          </not>
+       </condition>
+    </fail>
+    
+   </target>
+</project>

Added: trunk/examples/jms/embedded-simple/readme.html
===================================================================
--- trunk/examples/jms/embedded-simple/readme.html	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/readme.html	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,52 @@
+<html>
+   <head>
+      <title>HornetQ Embedded JMS Server Example</title>
+      <link rel="stylesheet" type="text/css" href="../../common/common.css" />
+      <link rel="stylesheet" type="text/css" href="../../common/prettify.css" />
+      <script type="text/javascript" src="../../common/prettify.js"></script>
+   </head>
+   <body onload="prettyPrint()">
+      <h1>Embedded JMS Server Example</h1>
+      
+      <p>This examples shows how to setup and run an embedded JMS server using HornetQ along with HornetQ configuration files.</p>
+     
+      <h2>Example step-by-step</h2>     
+      <p><i>To run the example, simply type <code>./build.sh</code> (or <code>build.bat</code> on windows) from this directory</i></p>
+
+      <ol>
+         <li>Create HornetQ core configuration files and make sure they are within your classpath.  By default, HornetQ
+             expects the classnames to be "hornetq-configuration.xml", "hornetq-jms.xml", and "hornetq-users.xml".</li>
+         <li>Create and start HornetQ JMS server</li>
+         <pre class="prettyprint">
+            <code>EmbeddedJMS jmsServer = new EmbeddedJMS();
+            jmsServer.start();</code>
+         </pre>
+        
+         <p>At this point the JMS server is started and any JMS clients can look up JMS resources from the JNDI to send/receive 
+            messages from the server. To keep the example simple, we will send and receive a JMS message from the same JVM 
+            used to run the JMS server.</p>
+              
+         <li>Lookup JMS resources defined in the configuration </li>
+         <pre class="prettyprint">
+            <code>ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
+            Queue queue = (Queue)context.lookup("/queue/queue1");</code>
+         </pre>
+         
+         <li>Send and receive a message using JMS API</li>
+         <p>See the <a href="../queue/readme.html">Queue Example</a> for detailed steps to send and receive a JMS message</p>
+           
+         <p>Finally, we stop the JMS server and its associated resources.</p>
+        
+         <li>Stop the JMS server</li>
+         <pre class="prettyprint">
+            <code>jmsServer.stop();</code>
+         </pre>
+        
+         <li>Stop the JNDI server</li>
+         <pre class="prettyprint">
+            <code>naming.stop();
+            jndiServer.stop();</code>
+         </pre>
+      </ol>
+   </body>
+</html>
\ No newline at end of file

Added: trunk/examples/jms/embedded-simple/server0/hornetq-configuration.xml
===================================================================
--- trunk/examples/jms/embedded-simple/server0/hornetq-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/server0/hornetq-configuration.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,34 @@
+<configuration xmlns="urn:hornetq"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd">
+
+    <persistence-enabled>false</persistence-enabled>
+    <!-- Connectors -->
+
+    <connectors>
+        <connector name="in-vm">
+            <factory-class>org.hornetq.core.remoting.impl.invm.InVMConnectorFactory</factory-class>
+        </connector>
+    </connectors>
+
+    <acceptors>
+        <acceptor name="in-vm">
+            <factory-class>org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</factory-class>
+        </acceptor>
+    </acceptors>
+
+    <!-- Other config -->
+
+    <security-settings>
+        <!--security for example queue-->
+        <security-setting match="#">
+            <permission type="createDurableQueue" roles="guest"/>
+            <permission type="deleteDurableQueue" roles="guest"/>
+            <permission type="createNonDurableQueue" roles="guest"/>
+            <permission type="deleteNonDurableQueue" roles="guest"/>
+            <permission type="consume" roles="guest"/>
+            <permission type="send" roles="guest"/>
+        </security-setting>
+    </security-settings>
+
+</configuration>

Added: trunk/examples/jms/embedded-simple/server0/hornetq-jms.xml
===================================================================
--- trunk/examples/jms/embedded-simple/server0/hornetq-jms.xml	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/server0/hornetq-jms.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,19 @@
+<configuration xmlns="urn:hornetq"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
+    <!--the connection factory used by the example-->
+    <connection-factory name="ConnectionFactory">
+        <connectors>
+            <connector-ref connector-name="in-vm"/>
+        </connectors>
+        <entries>
+            <entry name="ConnectionFactory"/>
+        </entries>
+    </connection-factory>
+
+    <!--the queue used by the example-->
+    <queue name="exampleQueue">
+        <entry name="/queue/exampleQueue"/>
+    </queue>
+
+</configuration>

Added: trunk/examples/jms/embedded-simple/server0/hornetq-users.xml
===================================================================
--- trunk/examples/jms/embedded-simple/server0/hornetq-users.xml	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/server0/hornetq-users.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,7 @@
+<configuration xmlns="urn:hornetq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-users.xsd">
+    <!-- the default user.  this is used where username is null-->
+    <defaultuser name="guest" password="guest">
+        <role name="guest"/>
+    </defaultuser>
+</configuration>
\ No newline at end of file

Added: trunk/examples/jms/embedded-simple/src/org/hornetq/jms/example/EmbeddedExample.java
===================================================================
--- trunk/examples/jms/embedded-simple/src/org/hornetq/jms/example/EmbeddedExample.java	                        (rev 0)
+++ trunk/examples/jms/embedded-simple/src/org/hornetq/jms/example/EmbeddedExample.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2009 Red Hat, Inc.
+ * Red Hat licenses this file to you under the Apache License, version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package org.hornetq.jms.example;
+
+import java.util.Date;
+import java.util.Hashtable;
+
+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 org.hornetq.jms.server.embedded.EmbeddedJMS;
+
+/**
+ * This example demonstrates how to run a HornetQ embedded with JMS
+ * 
+ * @author <a href="clebert.suconic at jboss.com">Clebert Suconic</a>
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ */
+public class EmbeddedExample
+{
+
+   public static void main(final String[] args)
+   {
+      try
+      {
+         EmbeddedJMS jmsServer = new EmbeddedJMS();
+         jmsServer.start();
+         System.out.println("Started Embedded JMS Server");
+
+         ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("/cf");
+         Queue queue = (Queue)jmsServer.lookup("/queue/exampleQueue");
+
+         // Step 10. Send and receive a message using JMS API
+         Connection connection = null;
+         try
+         {
+            connection = cf.createConnection();
+            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            MessageProducer producer = session.createProducer(queue);
+            TextMessage message = session.createTextMessage("Hello sent at " + new Date());
+            System.out.println("Sending message: " + message.getText());
+            producer.send(message);
+            MessageConsumer messageConsumer = session.createConsumer(queue);
+            connection.start();
+            TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
+            System.out.println("Received message:" + messageReceived.getText());
+         }
+         finally
+         {
+            if (connection != null)
+            {
+               connection.close();
+            }
+
+            // Step 11. Stop the JMS server
+            jmsServer.stop();
+            System.out.println("Stopped the JMS Server");
+            System.exit(0);
+         }
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+         System.exit(-1);
+      }
+   }
+
+}

Added: trunk/examples/jms/spring-integration/build.bat
===================================================================
--- trunk/examples/jms/spring-integration/build.bat	                        (rev 0)
+++ trunk/examples/jms/spring-integration/build.bat	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,13 @@
+ at echo off
+
+set "OVERRIDE_ANT_HOME=..\..\..\tools\ant"
+
+if exist "..\..\..\src\bin\build.bat" (
+   rem running from TRUNK
+   call ..\..\..\src\bin\build.bat %*
+) else (
+   rem running from the distro
+   call ..\..\..\bin\build.bat %*
+)
+
+set "OVERRIDE_ANT_HOME="

Added: trunk/examples/jms/spring-integration/build.sh
===================================================================
--- trunk/examples/jms/spring-integration/build.sh	                        (rev 0)
+++ trunk/examples/jms/spring-integration/build.sh	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+OVERRIDE_ANT_HOME=../../../tools/ant
+export OVERRIDE_ANT_HOME
+
+if [ -f "../../../src/bin/build.sh" ]; then
+   # running from TRUNK
+   ../../../src/bin/build.sh "$@"
+else
+   # running from the distro
+   ../../../bin/build.sh "$@"
+fi
+
+
+


Property changes on: trunk/examples/jms/spring-integration/build.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/examples/jms/spring-integration/build.xml
===================================================================
--- trunk/examples/jms/spring-integration/build.xml	                        (rev 0)
+++ trunk/examples/jms/spring-integration/build.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+      <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+      ]>
+<!--
+  ~ Copyright 2009 Red Hat, Inc.
+  ~ Red Hat licenses this file to you under the Apache License, version
+  ~ 2.0 (the "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+  ~ implied.  See the License for the specific language governing
+  ~ permissions and limitations under the License.
+  -->
+
+<project default="run" name="HornetQ Embedded Example">
+
+   <import file="../../common/build.xml"/>
+   <echo message = "Do not forget to add the Spring jars to your classpath!!!!!!!"/>
+   <path id="extra.classpath">
+ 	  <fileset dir="${jars.dir}">
+	     <include name="**/spring*.jar"/>
+	     <include name="**/commons-logging.jar"/>
+	  </fileset>
+   </path>
+	    
+   <path id="local.classpath">
+	  <fileset dir="${hornetq.jars.dir}">
+	       <include name="hornetq-core.jar"/>
+	       <include name="hornetq-jms.jar"/>
+	       <include name="hornetq-spring-integration.jar"/>
+	  </fileset>
+	 	
+	  <fileset dir="${jars.dir}">
+	     <include name="**/jboss-jms-api.jar"/>
+	     <include name="**/netty.jar"/>
+	     <include name="**/spring*.jar"/>
+	     <include name="**/commons-logging.jar"/>
+	  </fileset>
+
+   	  <pathelement location="${classes.dir}"/>	   	
+   	  <pathelement location="server0"/>	   	
+   </path>
+
+   <target name="run" depends="compile">   	
+    <java classname="org.hornetq.jms.example.SpringExample" fork="true" resultproperty="example-result">
+       <jvmarg value="-Xms50M"/>
+       <jvmarg value="-Xmx50M"/>
+       <classpath refid="local.classpath"/>
+    </java>
+    
+    <!-- if the example exited with a result value != 0, we fail the build -->
+    <fail message="Example SpringExample failed">
+       <condition>
+          <not>
+             <equals arg1="${example-result}" arg2="0"/>
+          </not>
+       </condition>
+    </fail>
+    
+   </target>
+</project>

Added: trunk/examples/jms/spring-integration/readme.html
===================================================================
--- trunk/examples/jms/spring-integration/readme.html	                        (rev 0)
+++ trunk/examples/jms/spring-integration/readme.html	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,17 @@
+<html>
+   <head>
+      <title>HornetQ Spring Example</title>
+      <link rel="stylesheet" type="text/css" href="../../common/common.css" />
+      <link rel="stylesheet" type="text/css" href="../../common/prettify.css" />
+      <script type="text/javascript" src="../../common/prettify.js"></script>
+   </head>
+   <body onload="prettyPrint()">
+      <h1>HornetQ Spring Example</h1>
+      
+      <p>This examples shows how to setup and run an embedded JMS server within a Spring ApplicationContext using HornetQ along with HornetQ configuration files.</p>
+     
+      <h2>Example step-by-step</h2>     
+      <p><i><b>YOU MUST DOWNLOAD THE SPRING LIBRARIES TO RUN THIS EXAMPLE!!!</b>  You must also modify the build.xml file to include the spring jars.  You'll see the placeholder that is already there.</i></p>
+      <p><i>To run the example, simply type <code>./build.sh</code> (or <code>build.bat</code> on windows) from this directory</i></p>
+   </body>
+</html>

Added: trunk/examples/jms/spring-integration/server0/hornetq-configuration.xml
===================================================================
--- trunk/examples/jms/spring-integration/server0/hornetq-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/spring-integration/server0/hornetq-configuration.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,34 @@
+<configuration xmlns="urn:hornetq"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd">
+
+    <persistence-enabled>false</persistence-enabled>
+    <!-- Connectors -->
+
+    <connectors>
+        <connector name="in-vm">
+            <factory-class>org.hornetq.core.remoting.impl.invm.InVMConnectorFactory</factory-class>
+        </connector>
+    </connectors>
+
+    <acceptors>
+        <acceptor name="in-vm">
+            <factory-class>org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</factory-class>
+        </acceptor>
+    </acceptors>
+
+    <!-- Other config -->
+
+    <security-settings>
+        <!--security for example queue-->
+        <security-setting match="#">
+            <permission type="createDurableQueue" roles="guest"/>
+            <permission type="deleteDurableQueue" roles="guest"/>
+            <permission type="createNonDurableQueue" roles="guest"/>
+            <permission type="deleteNonDurableQueue" roles="guest"/>
+            <permission type="consume" roles="guest"/>
+            <permission type="send" roles="guest"/>
+        </security-setting>
+    </security-settings>
+
+</configuration>

Added: trunk/examples/jms/spring-integration/server0/hornetq-jms.xml
===================================================================
--- trunk/examples/jms/spring-integration/server0/hornetq-jms.xml	                        (rev 0)
+++ trunk/examples/jms/spring-integration/server0/hornetq-jms.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,19 @@
+<configuration xmlns="urn:hornetq"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
+    <!--the connection factory used by the example-->
+    <connection-factory name="ConnectionFactory">
+        <connectors>
+            <connector-ref connector-name="in-vm"/>
+        </connectors>
+        <entries>
+            <entry name="ConnectionFactory"/>
+        </entries>
+    </connection-factory>
+
+    <!--the queue used by the example-->
+    <queue name="exampleQueue">
+        <entry name="/queue/exampleQueue"/>
+    </queue>
+
+</configuration>

Added: trunk/examples/jms/spring-integration/server0/hornetq-users.xml
===================================================================
--- trunk/examples/jms/spring-integration/server0/hornetq-users.xml	                        (rev 0)
+++ trunk/examples/jms/spring-integration/server0/hornetq-users.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,7 @@
+<configuration xmlns="urn:hornetq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="urn:hornetq /schema/hornetq-users.xsd">
+    <!-- the default user.  this is used where username is null-->
+    <defaultuser name="guest" password="guest">
+        <role name="guest"/>
+    </defaultuser>
+</configuration>
\ No newline at end of file

Added: trunk/examples/jms/spring-integration/server0/spring-jms-beans.xml
===================================================================
--- trunk/examples/jms/spring-integration/server0/spring-jms-beans.xml	                        (rev 0)
+++ trunk/examples/jms/spring-integration/server0/spring-jms-beans.xml	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+
+   <bean id="EmbeddedJms" class="org.hornetq.integration.spring.SpringJmsBootstrap" init-method="start" destroy-method="stop"/>
+
+    <bean id="listener" class="org.hornetq.jms.example.ExampleListener"/>
+    
+    <bean id="MessageSender" class="org.hornetq.jms.example.MessageSender">
+        <property name="connectionFactory" ref="ConnectionFactory"/>
+        <property name="destination" ref="/queue/exampleQueue"/>
+    </bean>
+
+    <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
+        <property name="connectionFactory" ref="ConnectionFactory"/>
+        <property name="destination" ref="/queue/exampleQueue"/>
+        <property name="messageListener" ref="listener"/>
+    </bean>
+    
+
+</beans>
+    
\ No newline at end of file

Added: trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/ExampleListener.java
===================================================================
--- trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/ExampleListener.java	                        (rev 0)
+++ trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/ExampleListener.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,28 @@
+package org.hornetq.jms.example;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
+
+/**
+ * @author <a href="mailto:bill at burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+public class ExampleListener implements MessageListener
+{
+   public static String lastMessage = null;
+
+   public void onMessage(Message message)
+   {
+      try
+      {
+         lastMessage = ((TextMessage)message).getText();
+      }
+      catch (JMSException e)
+      {
+         throw new RuntimeException(e);
+      }
+      System.out.println("MESSAGE RECEIVED: " + lastMessage);
+   }
+}

Added: trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/MessageSender.java
===================================================================
--- trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/MessageSender.java	                        (rev 0)
+++ trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/MessageSender.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,54 @@
+package org.hornetq.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+/**
+ * @author <a href="mailto:bill at burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+public class MessageSender
+{
+   private ConnectionFactory connectionFactory;
+   private Destination destination;
+
+   public ConnectionFactory getConnectionFactory()
+   {
+      return connectionFactory;
+   }
+
+   public void setConnectionFactory(ConnectionFactory connectionFactory)
+   {
+      this.connectionFactory = connectionFactory;
+   }
+
+   public Destination getDestination()
+   {
+      return destination;
+   }
+
+   public void setDestination(Destination destination)
+   {
+      this.destination = destination;
+   }
+
+   public void send(String msg)
+   {
+      try
+      {
+         Connection conn = connectionFactory.createConnection();
+         Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageProducer producer = session.createProducer(destination);
+         TextMessage message = session.createTextMessage(msg);
+         producer.send(message);
+      }
+      catch (Exception ex)
+      {
+         ex.printStackTrace();
+      }
+   }
+}

Added: trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/SpringExample.java
===================================================================
--- trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/SpringExample.java	                        (rev 0)
+++ trunk/examples/jms/spring-integration/src/org/hornetq/jms/example/SpringExample.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -0,0 +1,22 @@
+package org.hornetq.jms.example;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @author <a href="mailto:bill at burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+public class SpringExample
+{
+   public static void main(String[] args) throws Exception
+   {
+      System.out.println("Creating bean factory...");
+      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-jms-beans.xml"});
+      MessageSender sender = (MessageSender)context.getBean("MessageSender");
+      System.out.println("Sending message...");
+      sender.send("Hello world");
+      Thread.sleep(100);
+      context.destroy();
+   }
+}

Modified: trunk/src/main/org/hornetq/core/registry/MapBindingRegistry.java
===================================================================
--- trunk/src/main/org/hornetq/core/registry/MapBindingRegistry.java	2010-09-02 23:20:36 UTC (rev 9630)
+++ trunk/src/main/org/hornetq/core/registry/MapBindingRegistry.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -1,7 +1,7 @@
 package org.hornetq.core.registry;
 
 import org.hornetq.spi.BindingRegistry;
-import org.jboss.netty.util.internal.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * @author <a href="mailto:bill at burkecentral.com">Bill Burke</a>

Modified: trunk/src/main/org/hornetq/core/server/embedded/EmbeddedHornetQ.java
===================================================================
--- trunk/src/main/org/hornetq/core/server/embedded/EmbeddedHornetQ.java	2010-09-02 23:20:36 UTC (rev 9630)
+++ trunk/src/main/org/hornetq/core/server/embedded/EmbeddedHornetQ.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -1,5 +1,6 @@
 package org.hornetq.core.server.embedded;
 
+import org.hornetq.core.config.Configuration;
 import org.hornetq.core.config.impl.FileConfiguration;
 import org.hornetq.core.server.HornetQServer;
 import org.hornetq.core.server.impl.HornetQServerImpl;
@@ -17,7 +18,8 @@
 public class EmbeddedHornetQ
 {
    protected HornetQSecurityManager securityManager;
-   protected FileConfiguration configuration = new FileConfiguration();
+   protected String configResourcePath = null;
+   protected Configuration configuration;
    protected HornetQServer hornetQServer;
    protected MBeanServer mbeanServer;
 
@@ -28,7 +30,7 @@
     */
    public void setConfigResourcePath(String filename)
    {
-      configuration.setConfigurationUrl(filename);
+      configResourcePath = filename;
    }
 
    /**
@@ -51,6 +53,17 @@
       this.mbeanServer = mbeanServer;
    }
 
+   /**
+    * Set this object if you are not using file-based configuration.  The default implementation will load
+    * configuration from a file.
+    *
+    * @param configuration
+    */
+   public void setConfiguration(Configuration configuration)
+   {
+      this.configuration = configuration;
+   }
+
    public HornetQServer getHornetQServer()
    {
       return hornetQServer;
@@ -65,7 +78,14 @@
 
    protected void initStart() throws Exception
    {
-      configuration.start();
+      if (configuration == null)
+      {
+         if (configResourcePath == null) configResourcePath = "hornetq-configuration.xml";
+         FileConfiguration config = new FileConfiguration();
+         config.setConfigurationUrl(configResourcePath);
+         config.start();
+         configuration = config;
+      }
       if (securityManager == null)
       {
          securityManager = new HornetQSecurityManagerImpl();

Modified: trunk/src/main/org/hornetq/jms/server/embedded/EmbeddedJMS.java
===================================================================
--- trunk/src/main/org/hornetq/jms/server/embedded/EmbeddedJMS.java	2010-09-02 23:20:36 UTC (rev 9630)
+++ trunk/src/main/org/hornetq/jms/server/embedded/EmbeddedJMS.java	2010-09-02 23:21:38 UTC (rev 9631)
@@ -1,16 +1,20 @@
 package org.hornetq.jms.server.embedded;
 
+import org.hornetq.core.registry.JndiBindingRegistry;
 import org.hornetq.core.registry.MapBindingRegistry;
 import org.hornetq.core.server.embedded.EmbeddedHornetQ;
+import org.hornetq.jms.server.config.JMSConfiguration;
 import org.hornetq.jms.server.impl.JMSServerManagerImpl;
 import org.hornetq.spi.BindingRegistry;
 
+import javax.naming.Context;
+
 /**
  * Simple bootstrap class that parses hornetq config files (server and jms and security) and starts
  * a HornetQServer instance and populates it with configured JMS endpoints.
  * <p/>
  * JMS Endpoints are registered with a simple MapBindingRegistry.  If you want to use a different registry
- * you must set the registry property of this clas
+ * you must set the registry property of this class or call the setRegistry() method if you want to use JNDI
  *
  * @author <a href="mailto:bill at burkecentral.com">Bill Burke</a>
  * @version $Revision: 1 $
@@ -20,6 +24,8 @@
    protected JMSServerManagerImpl serverManager;
    protected BindingRegistry registry;
    protected String jmsConfigResourcePath;
+   protected JMSConfiguration jmsConfiguration;
+   protected Context context;
 
    /**
     * Classpath resource where JMS config file is.  Defaults to 'hornetq-jms.xml'
@@ -36,12 +42,37 @@
       return registry;
    }
 
+   /**
+    * Only set this property if you are using a custom BindingRegistry
+    *
+    * @param registry
+    */
    public void setRegistry(BindingRegistry registry)
    {
       this.registry = registry;
    }
 
    /**
+    * By default, this class uses file-based configuration.  Set this property to override it.
+    *
+    * @param jmsConfiguration
+    */
+   public void setJmsConfiguration(JMSConfiguration jmsConfiguration)
+   {
+      this.jmsConfiguration = jmsConfiguration;
+   }
+
+   /**
+    * If you want to use JNDI instead of an internal map, set this property
+    *
+    * @param context
+    */
+   public void setContext(Context context)
+   {
+      this.context = context;
+   }
+
+   /**
     * Lookup in the registry for registered object, i.e. a ConnectionFactory.  This is a convenience method.
     *
     * @param name
@@ -55,11 +86,17 @@
    public void start() throws Exception
    {
       super.initStart();
-      if (jmsConfigResourcePath == null) serverManager = new JMSServerManagerImpl(hornetQServer);
+      if (jmsConfiguration != null)
+      {
+         serverManager = new JMSServerManagerImpl(hornetQServer, jmsConfiguration);
+      }
+      else if (jmsConfigResourcePath == null) serverManager = new JMSServerManagerImpl(hornetQServer);
       else serverManager = new JMSServerManagerImpl(hornetQServer, jmsConfigResourcePath);
+      
       if (registry == null)
       {
-         registry = new MapBindingRegistry();
+         if (context != null) registry = new JndiBindingRegistry(context);
+         else registry = new MapBindingRegistry();
       }
       serverManager.setRegistry(registry);
       serverManager.start();



More information about the hornetq-commits mailing list