[hornetq-commits] JBoss hornetq SVN: r11549 - in trunk/examples/jms: stomp1.1 and 6 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Sun Oct 16 23:11:45 EDT 2011


Author: gaohoward
Date: 2011-10-16 23:11:44 -0400 (Sun, 16 Oct 2011)
New Revision: 11549

Added:
   trunk/examples/jms/stomp1.1/
   trunk/examples/jms/stomp1.1/build.bat
   trunk/examples/jms/stomp1.1/build.sh
   trunk/examples/jms/stomp1.1/build.xml
   trunk/examples/jms/stomp1.1/readme.html
   trunk/examples/jms/stomp1.1/server0/
   trunk/examples/jms/stomp1.1/server0/client-jndi.properties
   trunk/examples/jms/stomp1.1/server0/hornetq-beans.xml
   trunk/examples/jms/stomp1.1/server0/hornetq-configuration.xml
   trunk/examples/jms/stomp1.1/server0/hornetq-jms.xml
   trunk/examples/jms/stomp1.1/server0/hornetq-users.xml
   trunk/examples/jms/stomp1.1/src/
   trunk/examples/jms/stomp1.1/src/org/
   trunk/examples/jms/stomp1.1/src/org/hornetq/
   trunk/examples/jms/stomp1.1/src/org/hornetq/jms/
   trunk/examples/jms/stomp1.1/src/org/hornetq/jms/example/
   trunk/examples/jms/stomp1.1/src/org/hornetq/jms/example/StompExample.java
Log:
Adding a Stomp 1.1 example


Added: trunk/examples/jms/stomp1.1/build.bat
===================================================================
--- trunk/examples/jms/stomp1.1/build.bat	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/build.bat	2011-10-17 03:11:44 UTC (rev 11549)
@@ -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/stomp1.1/build.sh
===================================================================
--- trunk/examples/jms/stomp1.1/build.sh	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/build.sh	2011-10-17 03:11:44 UTC (rev 11549)
@@ -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/stomp1.1/build.sh
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/examples/jms/stomp1.1/build.xml
===================================================================
--- trunk/examples/jms/stomp1.1/build.xml	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/build.xml	2011-10-17 03:11:44 UTC (rev 11549)
@@ -0,0 +1,34 @@
+<?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 Stomp Example">
+
+   <import file="../../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.hornetq.jms.example.StompExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.hornetq.jms.example.StompExample"/>
+         <param name="hornetq.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/jms/stomp1.1/readme.html
===================================================================
--- trunk/examples/jms/stomp1.1/readme.html	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/readme.html	2011-10-17 03:11:44 UTC (rev 11549)
@@ -0,0 +1,119 @@
+<html>
+  <head>
+    <title>HornetQ Stomp 1.1 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>Stomp 1.1 Example</h1>
+
+     <p>This example shows you how to configure HornetQ to send and receive Stomp messages using Stomp 1.1 protocol.</p>
+     <p>The example will start a HornetQ server configured with Stomp and JMS.</p>
+     <p>The client will open a socket to initiate a Stomp 1.1 connection and then send one Stomp message (using TCP directly). 
+       The client will then consume a message from a JMS Queue and check it is the message sent with Stomp.</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>We create a TCP socket to connect to the Stomp port
+        <pre class="prettyprint">
+          Socket socket = new Socket("localhost", 61613); 
+        </pre>
+
+        <li>We negotiate a Stomp 1.1 connection to the server</li>
+        <pre class="prettyprint">
+         String connectFrame = "CONNECT\n" + 
+            "accept-version:1.1\n" +
+            "host:localhost\n" +
+            "login:guest\n" +
+            "passcode:guest\n" +
+            "request-id:1\n" +
+            "\n" +
+            END_OF_FRAME;
+          sendFrame(socket, connectFrame);
+        </pre>
+        
+        <li>We receive a response showing that the connection version</li>
+        <pre>
+         String response = receiveFrame(socket);
+         System.out.println("response: " + response);
+        </pre>
+
+        <li>We send a SEND frame (a Stomp message) to the destination <code>jms.queue.exampleQueue</code> 
+          (which corresponds to the HornetQ address for the JMS Queue <code>exampleQueue</code>) with a text body</li>
+        <pre class="prettyprint">
+          String text = "Hello World from Stomp 1.1 !";
+          String message = "SEND\n" + 
+             "destination:jms.queue.exampleQueue\n" +
+             "\n" +
+             text +
+             END_OF_FRAME;
+          sendFrame(socket, message);
+          System.out.println("Sent Stomp message: " + text);
+        </pre>
+
+        <li>We send a DISCONNECT frame to disconnect from the server</li>
+        <pre class="prettyprint">
+          String disconnectFrame = "DISCONNECT\n" +
+             "\n" +
+             Stomp.NULL;
+          sendFrame(socket, disconnectFrame);
+        </pre>
+
+        <li>We close the TCP socket</li>
+        <pre class="prettyprint">
+          socket.close();
+        </pre>
+
+        <li>We create an initial context to perform the JNDI lookup.</li>
+        <pre class="prettyprint">
+          initialContext = getContext(0);
+       </pre>
+
+        <li>We perform a lookup on the queue and the connection factory</li>
+        <pre class="prettyprint">
+          Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
+          ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+        </pre>
+
+        <li>We create a JMS Connection, Session and a MessageConsumer on the queue</li>
+        <pre class="prettyprint">
+          connection = cf.createConnection();
+          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+          MessageConsumer consumer = session.createConsumer(queue);
+        </pre>
+
+        <li>We start the connection</li>
+        <pre class="prettyprint">
+           <code>connection.start();</code>
+        </pre>
+
+        <li>We receive the message. Stomp messages are mapped to JMS TextMessage.</li>
+        <pre class="prettyprint">
+          TextMessage messageReceived = (TextMessage)consumer.receive(5000);
+          System.out.println("Received JMS message: " + messageReceived.getText());
+        </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 class="prettyprint">
+           <code>finally
+           {
+              if (initialContext != null)
+              {
+                initialContext.close();
+              }
+              if (connection != null)
+              {
+                 connection.close();
+              }
+           }</code>
+        </pre>
+
+
+
+     </ol>
+  </body>
+</html>

Added: trunk/examples/jms/stomp1.1/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/stomp1.1/server0/client-jndi.properties	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/server0/client-jndi.properties	2011-10-17 03:11:44 UTC (rev 11549)
@@ -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/jms/stomp1.1/server0/hornetq-beans.xml
===================================================================
--- trunk/examples/jms/stomp1.1/server0/hornetq-beans.xml	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/server0/hornetq-beans.xml	2011-10-17 03:11:44 UTC (rev 11549)
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+   <bean name="Naming" class="org.jnp.server.NamingBeanImpl"/>
+
+   <!-- JNDI server. Disable this if you don't want JNDI -->
+   <bean name="JNDIServer" class="org.jnp.server.Main">
+      <property name="namingInfo">
+         <inject bean="Naming"/>
+      </property>
+      <property name="port">1099</property>
+      <property name="bindAddress">localhost</property>
+      <property name="rmiPort">1098</property>
+      <property name="rmiBindAddress">localhost</property>
+   </bean>
+   
+   <!-- MBean server -->
+   <bean name="MBeanServer" class="javax.management.MBeanServer">
+      <constructor factoryClass="java.lang.management.ManagementFactory"
+                   factoryMethod="getPlatformMBeanServer"/>
+   </bean> 
+
+   <!-- The core configuration -->
+   <bean name="Configuration" class="org.hornetq.core.config.impl.FileConfiguration"/>
+
+   <!-- The security manager -->
+   <bean name="HornetQSecurityManager" class="org.hornetq.spi.core.security.HornetQSecurityManagerImpl">
+      <start ignored="true"/>
+      <stop ignored="true"/>
+   </bean>
+
+   <!-- The core server -->
+   <bean name="HornetQServer" class="org.hornetq.core.server.impl.HornetQServerImpl">
+      <constructor>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+         <parameter>
+            <inject bean="MBeanServer"/>
+         </parameter>
+         <parameter>
+            <inject bean="HornetQSecurityManager"/>
+         </parameter>        
+      </constructor>
+      <start ignored="true"/>
+      <stop ignored="true"/>
+   </bean>
+   
+   <!-- The JMS server -->
+   <bean name="JMSServerManager" class="org.hornetq.jms.server.impl.JMSServerManagerImpl">
+      <constructor>         
+         <parameter>
+            <inject bean="HornetQServer"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+</deployment>

Added: trunk/examples/jms/stomp1.1/server0/hornetq-configuration.xml
===================================================================
--- trunk/examples/jms/stomp1.1/server0/hornetq-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/server0/hornetq-configuration.xml	2011-10-17 03:11:44 UTC (rev 11549)
@@ -0,0 +1,42 @@
+<configuration xmlns="urn:hornetq"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd">
+
+   <!-- Connectors -->
+
+   <connectors>
+      <connector name="netty-connector">
+         <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
+      </connector>
+   </connectors>
+   
+   <!-- Acceptors -->
+   <acceptors>
+      <!-- a regular Netty acceptor used by the JMS client -->
+      <acceptor name="netty-acceptor">
+         <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>        
+      </acceptor>
+      <!-- the stomp-acceptor is configured for the Stomp protocol and -->
+      <!-- will listen on port 61613 (default Stomp port)              -->
+      <acceptor name="stomp-acceptor">
+         <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>        
+         <param key="protocol" value="stomp" />
+         <param key="port" value="61613" />
+      </acceptor>
+   </acceptors>
+
+   <!-- Other config -->
+
+   <security-settings>
+      <!--security for example queue-->
+      <security-setting match="jms.queue.exampleQueue">
+         <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/stomp1.1/server0/hornetq-jms.xml
===================================================================
--- trunk/examples/jms/stomp1.1/server0/hornetq-jms.xml	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/server0/hornetq-jms.xml	2011-10-17 03:11:44 UTC (rev 11549)
@@ -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="netty-connector"/>
+      </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/stomp1.1/server0/hornetq-users.xml
===================================================================
--- trunk/examples/jms/stomp1.1/server0/hornetq-users.xml	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/server0/hornetq-users.xml	2011-10-17 03:11:44 UTC (rev 11549)
@@ -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/stomp1.1/src/org/hornetq/jms/example/StompExample.java
===================================================================
--- trunk/examples/jms/stomp1.1/src/org/hornetq/jms/example/StompExample.java	                        (rev 0)
+++ trunk/examples/jms/stomp1.1/src/org/hornetq/jms/example/StompExample.java	2011-10-17 03:11:44 UTC (rev 11549)
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2010 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.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+import org.hornetq.common.example.HornetQExample;
+
+/**
+ * An example where a Stomp 1.1 client sends a message on a TCP socket
+ * and consumes it from a JMS MessageConsumer.
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ * @author <a href="mailto:hgao at redhat.com">Howard Gao</a>
+ */
+public class StompExample extends HornetQExample
+{
+   private static final String END_OF_FRAME = "\u0000";
+
+   public static void main(final String[] args)
+   {
+      new StompExample().run(args);
+   }
+
+   @Override
+   public boolean runExample() throws Exception
+   {
+      Connection connection = null;
+      InitialContext initialContext = null;
+
+      try
+      {
+         // Step 1. Create a TCP socket to connect to the Stomp port
+         Socket socket = new Socket("localhost", 61613);
+
+         // Step 2. Send a CONNECT frame to connect to the server
+         String connectFrame = "CONNECT\n" + 
+            "accept-version:1.1\n" +
+            "host:localhost\n" +
+            "login:guest\n" +
+            "passcode:guest\n" +
+            "request-id:1\n" +
+            "\n" +
+            END_OF_FRAME;
+         sendFrame(socket, connectFrame);
+         
+         String response = receiveFrame(socket);
+         System.out.println("response: " + response);
+
+         // Step 3. Send a SEND frame (a Stomp message) to the
+         // jms.queue.exampleQueue address with a text body
+         String text = "Hello World from Stomp 1.1 !";
+         String message = "SEND\n" + 
+            "destination:jms.queue.exampleQueue\n" +
+            "\n" +
+            text +
+            END_OF_FRAME;
+         sendFrame(socket, message);
+         System.out.println("Sent Stomp message: " + text);
+
+         // Step 4. Send a DISCONNECT frame to disconnect from the server
+         String disconnectFrame = "DISCONNECT\n" +
+            "\n" +
+            END_OF_FRAME;
+         sendFrame(socket, disconnectFrame);
+
+         // Step 5. Slose the TCP socket
+         socket.close();
+
+         // We will now consume from JMS the message sent with Stomp.
+
+         // Step 6. Create an initial context to perform the JNDI lookup.
+         initialContext = getContext(0);
+
+         // Step 7. Perform a lookup on the queue and the connection factory
+         Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
+         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+         // Step 8.Create a JMS Connection, Session and a MessageConsumer on the queue
+         connection = cf.createConnection();
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageConsumer consumer = session.createConsumer(queue);
+
+         // Step 9. Start the Connection
+         connection.start();
+
+         // Step 10. Receive the message
+         TextMessage messageReceived = (TextMessage)consumer.receive(5000);
+         System.out.println("Received JMS message: " + messageReceived.getText());
+
+         return true;
+      }
+      finally
+      {
+         // Step 11. Be sure to close our JMS resources!
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
+         if (connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+
+   private static void sendFrame(Socket socket, String data) throws Exception
+   {
+      byte[] bytes = data.getBytes("UTF-8");
+      OutputStream outputStream = socket.getOutputStream();
+      for (int i = 0; i < bytes.length; i++)
+      {
+         outputStream.write(bytes[i]);
+      }
+      outputStream.flush();
+   }
+   
+   private static String receiveFrame(Socket socket) throws Exception
+   {
+      InputStream inputStream = socket.getInputStream();
+      byte[] buffer = new byte[1024];
+      int size = inputStream.read(buffer);
+      
+      byte[] data = new byte[size];
+      System.arraycopy(buffer, 0, data, 0, size);
+      
+      String frame = new String(data, "UTF-8");
+      return frame;
+      
+   }
+
+}



More information about the hornetq-commits mailing list