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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 17 04:01:33 EDT 2009


Author: gaohoward
Date: 2009-04-17 04:01:32 -0400 (Fri, 17 Apr 2009)
New Revision: 6467

Added:
   trunk/examples/jms/interceptor/
   trunk/examples/jms/interceptor/build.xml
   trunk/examples/jms/interceptor/readme.html
   trunk/examples/jms/interceptor/server0/
   trunk/examples/jms/interceptor/server0/client-jndi.properties
   trunk/examples/jms/interceptor/server0/jbm-configuration.xml
   trunk/examples/jms/interceptor/server0/jbm-jms.xml
   trunk/examples/jms/interceptor/server0/jbm-queues.xml
   trunk/examples/jms/interceptor/server0/jbm-security.xml
   trunk/examples/jms/interceptor/server0/jbm-standalone-beans.xml
   trunk/examples/jms/interceptor/src/
   trunk/examples/jms/interceptor/src/org/
   trunk/examples/jms/interceptor/src/org/jboss/
   trunk/examples/jms/interceptor/src/org/jboss/jms/
   trunk/examples/jms/interceptor/src/org/jboss/jms/example/
   trunk/examples/jms/interceptor/src/org/jboss/jms/example/InterceptorExample.java
   trunk/examples/jms/interceptor/src/org/jboss/jms/example/SimpleInterceptor.java
Modified:
   trunk/.classpath
Log:
added interceptor example


Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-17 07:47:34 UTC (rev 6466)
+++ trunk/.classpath	2009-04-17 08:01:32 UTC (rev 6467)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry excluding="**/.svn/**/*" kind="src" path="src/main"/>
+	<classpathentry kind="src" path="examples/jms/interceptor/src"/>
 	<classpathentry kind="src" path="build/src"/>
 	<classpathentry kind="src" path="tests/jms-tests/config"/>
 	<classpathentry kind="src" path="tests/config"/>

Added: trunk/examples/jms/interceptor/build.xml
===================================================================
--- trunk/examples/jms/interceptor/build.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/build.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+      <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+      ]>
+
+<!-- =========================================================================================== -->
+<!--                                                                                             -->
+<!-- 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 Interceptor Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.InterceptorExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.InterceptorExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>

Added: trunk/examples/jms/interceptor/readme.html
===================================================================
--- trunk/examples/jms/interceptor/readme.html	                        (rev 0)
+++ trunk/examples/jms/interceptor/readme.html	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,152 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Interceptor Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>JMS Interceptor Example</h1>
+     <br>
+     <p>This example shows you how to implement and configure a simple interceptor with JBoss Messaging.</p>
+     
+     <p>JBoss Messaging allows an application to use an interceptor to hook into the messaging system. All that needs to do is to implement the
+     Interceptor interface, as defined below: </p>
+     <pre>
+     <code>
+         public interface Interceptor
+         {   
+            boolean intercept(Packet packet, RemotingConnection connection) throws MessagingException;
+         }
+     </code>
+     </pre>
+     <p>Once you have your own interceptor class, add it to the jbm-configuration.xml, as follows:</p>
+     <pre>
+     <code>
+        &lt;configuration&gt;
+        ...
+           &lt;remoting-interceptors&gt;
+              &lt;class-name&gt;org.jboss.jms.example.SimpleInterceptor&lt;/class-name&gt;
+           &lt;/remoting-interceptors&gt;
+        ...
+        &lt;/configuration&gt;
+     </code>
+     </pre>
+     
+     <p>With interceptor, you can handle various events in message processing. In this example, a simple interceptor, SimpleInterceptor, is implemented and configured. 
+     When the example is running, the interceptor will print out each events that are passed in the interceptor. And it will add a string property to the message being 
+     delivered. You can see that after the message is received, there will be a new string property appears in the received message.</p>
+     
+     <br>
+     <h2>Example step-by-step</h2>
+     <p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
+     <br>
+     <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 <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
+        <pre>
+           <code>InitialContext initialContext = getContext(0);</code>
+        </pre>
+
+        <li>We look-up the JMS queue object from JNDI</li>
+        <pre>
+           <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+        </pre>
+
+        <li>We look-up the JMS connection factory object from JNDI</li>
+        <pre>
+           <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
+        </pre>
+
+        <li>We create a JMS connection</li>
+        <pre>
+           <code>connection = cf.createConnection();</code>
+        </pre>
+
+        <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
+        <pre>
+           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+        </pre>
+
+        <li>We create a JMS message producer on the session. This will be used to send the messages.</li>
+        <pre>
+          <code>MessageProducer messageProducer = session.createProducer(topic);</code>
+       </pre>
+
+        <li>We create a JMS text message that we are going to send.</li>
+        <pre>
+           <code>TextMessage message = session.createTextMessage("This is a text message");</code>
+        </pre>
+
+        <li>We send message to the queue</li>
+        <pre>
+           <code>messageProducer.send(message);</code>
+        </pre>
+
+        <li>We create a JMS Message Consumer to receive the message.</li>
+          <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
+        </pre>
+
+        <li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
+        <pre>
+           <code>connection.start();</code>
+        </pre>
+
+        <li>The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'</li>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);</code>
+        </pre>
+
+        <li>We print out the message and the new property that has been added by the interceptor</li>
+        <pre>
+           <code>System.out.println("Received message [" + messageReceived.getText() + "] with String property: " + messageReceived.getStringProperty("newproperty"));</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>
+     <ol>
+        <li>The SimpleInterceptor:</li>
+        <pre>
+           <code>
+           public class SimpleInterceptor implements Interceptor
+           {
+              public boolean intercept(Packet packet, RemotingConnection connection) throws MessagingException
+              {
+                 System.out.println("SimpleInterceptor gets called!");
+                 System.out.println("Packet: " + packet.getClass().getName());
+                 System.out.println("RemotingConnection: " + connection.getRemoteAddress());
+
+                 if (packet instanceof SessionSendMessage)
+                 {
+                    SessionSendMessage realPacket = (SessionSendMessage)packet;
+                    Message msg = realPacket.getServerMessage();
+                    msg.putStringProperty(new SimpleString("newproperty"), new SimpleString("Hello from interceptor!"));
+                 }
+                 return true;
+              }
+           }
+           </code>
+        </pre>
+     </ol>
+  </body>
+</html>
+
+
+
+
+
+

Added: trunk/examples/jms/interceptor/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/interceptor/server0/client-jndi.properties	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/client-jndi.properties	2009-04-17 08:01:32 UTC (rev 6467)
@@ -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/interceptor/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/interceptor/server0/jbm-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/jbm-configuration.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,33 @@
+<deployment xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-configuration.xsd">
+   <configuration>
+
+      <remoting-interceptors>
+         <class-name>org.jboss.jms.example.SimpleInterceptor</class-name>
+      </remoting-interceptors>
+
+      <!-- Connectors -->
+
+      <connectors>
+         <connector name="netty-connector">
+            <factory-class>org.jboss.messaging.integration.transports.netty.NettyConnectorFactory</factory-class>
+            <param key="jbm.remoting.netty.port" value="5446" type="Integer"/>
+         </connector>
+      </connectors>
+      
+      <!-- Acceptors -->
+      <acceptors>
+         <acceptor name="netty-acceptor">
+            <factory-class>org.jboss.messaging.integration.transports.netty.NettyAcceptorFactory</factory-class>
+            <param key="jbm.remoting.netty.port" value="5446" type="Integer"/>
+         </acceptor>
+      </acceptors>
+
+      <!-- Other config -->
+
+      <journal-min-files>2</journal-min-files>
+
+   </configuration>
+
+</deployment>

Added: trunk/examples/jms/interceptor/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/interceptor/server0/jbm-jms.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/jbm-jms.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,20 @@
+<deployment xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-jms.xsd ">
+   <!--the connection factory used by the example-->
+   <connection-factory name="ConnectionFactory">
+      <connector-ref connector-name="netty-connector"/>
+      <entries>
+         <entry name="ConnectionFactory"/>
+         <entry name="XAConnectionFactory"/>
+         <entry name="java:/ConnectionFactory"/>
+         <entry name="java:/XAConnectionFactory"/>
+      </entries>
+   </connection-factory>
+
+   <!--the queue used by the example-->
+   <queue name="exampleQueue">
+      <entry name="/queue/exampleQueue"/>
+   </queue>
+
+</deployment>

Added: trunk/examples/jms/interceptor/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/interceptor/server0/jbm-queues.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/jbm-queues.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,15 @@
+<settings xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-queues.xsd ">
+
+   <!--security for example queue-->
+   <security match="jms.queue.exampleQueue">
+      <permission type="createDurableQueue" roles="guest"/>
+      <permission type="deleteDurableQueue" roles="guest"/>
+      <permission type="createTempQueue" roles="guest"/>
+      <permission type="deleteTempQueue" roles="guest"/>
+      <permission type="consume" roles="guest"/>
+      <permission type="send" roles="guest"/>
+   </security>
+
+</settings>

Added: trunk/examples/jms/interceptor/server0/jbm-security.xml
===================================================================
--- trunk/examples/jms/interceptor/server0/jbm-security.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/jbm-security.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,7 @@
+<deployment xmlns="urn:jboss:messaging" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-security.xsd ">
+   <!-- the default user.  this is used where username is null-->
+   <defaultuser name="guest" password="guest">
+      <role name="guest"/>
+   </defaultuser>
+</deployment>
\ No newline at end of file

Added: trunk/examples/jms/interceptor/server0/jbm-standalone-beans.xml
===================================================================
--- trunk/examples/jms/interceptor/server0/jbm-standalone-beans.xml	                        (rev 0)
+++ trunk/examples/jms/interceptor/server0/jbm-standalone-beans.xml	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,57 @@
+<?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.jboss.messaging.core.config.impl.FileConfiguration"/>
+
+   <!-- The security manager -->
+   <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl">
+      <start ignored="true"/>
+      <stop ignored="true"/>
+   </bean>
+
+   <!-- The core server -->
+   <bean name="MessagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl"> 
+      <constructor>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+         <parameter>
+            <inject bean="MBeanServer"/>
+         </parameter>
+         <parameter>
+            <inject bean="JBMSecurityManager"/>
+         </parameter>        
+      </constructor>         
+   </bean>
+   
+   <!-- The JMS server -->
+   <bean name="JMSServerManager" class="org.jboss.messaging.jms.server.impl.JMSServerManagerImpl">
+      <constructor>         
+         <parameter>
+            <inject bean="MessagingServer"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+</deployment>

Added: trunk/examples/jms/interceptor/src/org/jboss/jms/example/InterceptorExample.java
===================================================================
--- trunk/examples/jms/interceptor/src/org/jboss/jms/example/InterceptorExample.java	                        (rev 0)
+++ trunk/examples/jms/interceptor/src/org/jboss/jms/example/InterceptorExample.java	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,104 @@
+/*
+   * 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.jms.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;
+
+/**
+ * A simple JMS example that shows how to implement and use interceptors with JBoss Messaging.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class InterceptorExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new InterceptorExample().run(args);
+   }
+
+   public boolean runExample() throws Exception
+   {
+      Connection connection = null;
+      InitialContext initialContext = null;
+      try
+      {
+         //Step 1. Create an initial context to perform the JNDI lookup.
+         initialContext = getContext(0);
+
+         //Step 2. Perform a lookup on the queue
+         Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
+
+         //Step 3. Perform a lookup on the Connection Factory
+         ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
+
+         //Step 4.Create a JMS Connection
+         connection = cf.createConnection();
+
+         //Step 5. Create a JMS Session
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         //Step 6. Create a JMS Message Producer
+         MessageProducer producer = session.createProducer(queue);
+
+         //Step 7. Create a Text Message
+         TextMessage message = session.createTextMessage("This is a text message");
+
+         System.out.println("Sending message [" + message.getText() + "] with String property: " + message.getStringProperty("newproperty"));
+
+         //Step 8. Send the Message
+         producer.send(message);
+
+         //Step 9. Create a JMS Message Consumer
+         MessageConsumer messageConsumer = session.createConsumer(queue);
+
+         //Step 10. Start the Connection
+         connection.start();
+
+         //Step 11. Receive the message
+         TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
+
+         System.out.println("Received message [" + messageReceived.getText() + "] with String property: " + messageReceived.getStringProperty("newproperty"));
+
+         return true;
+      }
+      finally
+      {
+         //Step 12. Be sure to close our JMS resources!
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
+         if(connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+
+}

Added: trunk/examples/jms/interceptor/src/org/jboss/jms/example/SimpleInterceptor.java
===================================================================
--- trunk/examples/jms/interceptor/src/org/jboss/jms/example/SimpleInterceptor.java	                        (rev 0)
+++ trunk/examples/jms/interceptor/src/org/jboss/jms/example/SimpleInterceptor.java	2009-04-17 08:01:32 UTC (rev 6467)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2009, 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.jms.example;
+
+import org.jboss.messaging.core.exception.MessagingException;
+import org.jboss.messaging.core.message.Message;
+import org.jboss.messaging.core.remoting.Interceptor;
+import org.jboss.messaging.core.remoting.Packet;
+import org.jboss.messaging.core.remoting.RemotingConnection;
+import org.jboss.messaging.core.remoting.impl.wireformat.SessionSendMessage;
+import org.jboss.messaging.utils.SimpleString;
+
+/**
+ * A simple Interceptor implementation
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class SimpleInterceptor implements Interceptor
+{
+   
+   public boolean intercept(Packet packet, RemotingConnection connection) throws MessagingException
+   {
+      System.out.println("SimpleInterceptor gets called!");
+      System.out.println("Packet: " + packet.getClass().getName());
+      System.out.println("RemotingConnection: " + connection.getRemoteAddress());
+
+      if (packet instanceof SessionSendMessage)
+      {
+         SessionSendMessage realPacket = (SessionSendMessage)packet;
+         Message msg = realPacket.getServerMessage();
+         msg.putStringProperty(new SimpleString("newproperty"), new SimpleString("Hello from interceptor!"));
+      }
+      return true;
+   }
+
+}
+
+




More information about the jboss-cvs-commits mailing list