[jboss-cvs] JBoss Messaging SVN: r6256 - in trunk: examples/jms and 6 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Apr 1 06:55:19 EDT 2009
Author: jmesnil
Date: 2009-04-01 06:55:19 -0400 (Wed, 01 Apr 2009)
New Revision: 6256
Added:
trunk/examples/jms/queue-requestor/
trunk/examples/jms/queue-requestor/build.xml
trunk/examples/jms/queue-requestor/readme.html
trunk/examples/jms/queue-requestor/src/
trunk/examples/jms/queue-requestor/src/org/
trunk/examples/jms/queue-requestor/src/org/jboss/
trunk/examples/jms/queue-requestor/src/org/jboss/jms/
trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/
trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/QueueRequestorExample.java
trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/TextReverserService.java
Modified:
trunk/.classpath
Log:
JMS QueueRequestor example
Modified: trunk/.classpath
===================================================================
--- trunk/.classpath 2009-04-01 08:41:08 UTC (rev 6255)
+++ trunk/.classpath 2009-04-01 10:55:19 UTC (rev 6256)
@@ -6,6 +6,7 @@
<classpathentry kind="src" path="examples/jms/transactional/src"/>
<classpathentry kind="src" path="examples/jms/topic/src"/>
<classpathentry kind="src" path="examples/jms/queue/src"/>
+ <classpathentry kind="src" path="examples/jms/queue-requestor/src"/>
<classpathentry kind="src" path="examples/jms/durable/src"/>
<classpathentry kind="src" path="examples/jms/common/src"/>
<classpathentry kind="src" path="build/src"/>
Added: trunk/examples/jms/queue-requestor/build.xml
===================================================================
--- trunk/examples/jms/queue-requestor/build.xml (rev 0)
+++ trunk/examples/jms/queue-requestor/build.xml 2009-04-01 10:55:19 UTC (rev 6256)
@@ -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 JMS Queue Requestor Example">
+
+ <import file="../common/build.xml"/>
+
+ <target name="run">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.QueueRequestorExample"/>
+ </antcall>
+ </target>
+
+ <target name="runRemote">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.QueueRequestorExample"/>
+ <param name="jbm.example.runServer" value="false"/>
+ </antcall>
+ </target>
+
+</project>
Added: trunk/examples/jms/queue-requestor/readme.html
===================================================================
--- trunk/examples/jms/queue-requestor/readme.html (rev 0)
+++ trunk/examples/jms/queue-requestor/readme.html 2009-04-01 10:55:19 UTC (rev 6256)
@@ -0,0 +1,111 @@
+<html>
+ <head>
+ <title>JBoss Messaging JMS QueueRequestor Example</title>
+ <link rel="stylesheet" type="text/css" href="../common/common.css">
+ </head>
+ <body>
+ <h1>JMS QueueRequestor Example</h1>
+ <br>
+ <p>This example shows you how to use a QueueRequestor with JBoss Messaging.</p>
+ <p>A QueueRequestor is a JMS utility object to simplify the use of request/reply.</p>
+ <br>
+ <p>The example consists in two classes:</p>
+ <dl>
+ <dt><code>TextReverserService</code></dt>
+ <dd>A JMS MessageListener which consumes text messages and sends replies containing the reversed text</dd>
+ <dt><code>QueueRequestorExample</code></dt>
+ <dd>A JMS Client which uses a QueueRequestor to send text requests to a queue and receive replies with the reversed text</dd>
+ </dl>
+
+
+ <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 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();</code>
+ </pre>
+
+ <li>We look up the JMS queue from JNDI</li>
+ <pre>
+ <code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
+ </pre>
+
+ <li>We look up JMS queue connection factory from JNDI</li>
+ <pre>
+ <code>QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
+ </pre>
+
+ <li>We create the TextReverserService which will consume messages from the queue</li>
+ <pre>
+ <code> TextReverserService reverserService = new TextReverserService(cf, queue);</code>
+ </pre>
+
+ <li>We Create a JMS queue connection</li>
+ <pre>
+ <code>connection = cf.createQueueConnection();</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>We create a JMS queue session. The session is created as non transacted and will auto acknowledge messages (this is mandatory to use it to create a QueueRequestor)</li>
+ <pre>
+ <code>QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+ </pre>
+
+ <li>We create a JMS QueueRequestor using the session and the queue</li>
+ <pre>
+ <code>QueueRequestor queueRequestor = new QueueRequestor(session, queue);</code>
+ </pre>
+
+ <li>We create a JMS text message to send as a request</li>
+ <pre>
+ <code>TextMessage request = session.createTextMessage("Hello, World!");</code>
+ </pre>
+
+ <li>We use the queue requestor to send the request and block until a reply is received</li>
+ <pre>
+ <code>TextMessage reply = (TextMessage)queueRequestor.request(request);</code>
+ </pre>
+
+ <li>The reply's text contains the reverse of the request's text</li>
+ <pre>
+ <code>System.out.println("Send request: " + request.getText());
+ System.out.println("Received reply:" + reply.getText());</code>
+ </pre>
+
+ <li>We close the queue requestor to release its JMS resources</li>
+ <pre>
+ <code>queueRequestor.close()</code>
+ </pre>
+
+ <li>We do the same for the text reverser service</li>
+ <pre>
+ <code>reverserService.close()</code>
+ </pre>
+
+ <li>and finally <b>always</b> remember to close your JMS connections after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its session, consumer, producer and browser objects</li>
+
+ <pre>
+ <code>
+
+ finally
+ {
+ if (connection != null)
+ {
+ // Be sure to close our JMS resources!
+ connection.close();
+ }
+ }
+ </code>
+ </pre>
+
+
+
+ </ol>
+ </body>
+</html>
\ No newline at end of file
Added: trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/QueueRequestorExample.java
===================================================================
--- trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/QueueRequestorExample.java (rev 0)
+++ trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/QueueRequestorExample.java 2009-04-01 10:55:19 UTC (rev 6256)
@@ -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.jms.example;
+
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.QueueRequestor;
+import javax.jms.QueueSession;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+/**
+ * A simple JMS example that shows how to use queues requestors.
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ */
+public class QueueRequestorExample extends JMSExample
+{
+ public static void main(String[] args)
+ {
+ new QueueRequestorExample().run(args);
+ }
+
+ public void runExample() throws Exception
+ {
+ QueueConnection connection = null;
+ try
+ {
+ // Step 1. Create an initial context to perform the JNDI lookup.
+ InitialContext initialContext = getContext();
+
+ // Step 2. Perfom a lookup on the queue
+ Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
+
+ // Step 3. Look-up the JMS queue connection factory
+ QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+ // Step 4. Create a TextReverserService which consumes messages from the queue and sends message with reversed text
+ TextReverserService reverserService = new TextReverserService(cf, queue);
+
+ // Step 5. Create a JMS QueueConnection
+ connection = cf.createQueueConnection();
+
+ // Step 6. Start the connection
+ connection.start();
+
+ // Step 7. Create a JMS queue session with AUTO_ACKNOWLEDGE mode
+ QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // Step 8. Create a JMS queue requestor to send requests to the queue
+ QueueRequestor queueRequestor = new QueueRequestor(session, queue);
+
+ // Step 9. Create a JMS message to send as a request
+ TextMessage request = session.createTextMessage("Hello, World!");
+
+ // Step 10. Use the requestor to send the request and wait to receive a reply
+ TextMessage reply = (TextMessage)queueRequestor.request(request);
+
+ // Step 11. The reply's text contains the reversed request's text
+ System.out.println("Send request: " + request.getText());
+ System.out.println("Received reply:" + reply.getText());
+
+ // Step.12 close the queue requestor
+ queueRequestor.close();
+
+ // Step 13. close the text reverser service
+ reverserService.close();
+ }
+ finally
+ {
+ if (connection != null)
+ {
+ try
+ {
+ // Step 14. Be sure to close the JMS resources!
+ connection.close();
+ }
+ catch (JMSException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}
Added: trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/TextReverserService.java
===================================================================
--- trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/TextReverserService.java (rev 0)
+++ trunk/examples/jms/queue-requestor/src/org/jboss/jms/example/TextReverserService.java 2009-04-01 10:55:19 UTC (rev 6256)
@@ -0,0 +1,133 @@
+/*
+ * 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 javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+/**
+ * A TextReverserService is a MessageListener which consume text messages from a destination
+ * and replies with text messages containing the reversed text.
+ * It sends replies to the destination specified by the JMS ReplyTo header of the consumed messages.
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ *
+ */
+public class TextReverserService implements MessageListener
+{
+
+ // Constants -----------------------------------------------------
+
+ // Attributes ----------------------------------------------------
+
+ private Session session;
+
+ private Connection connection;
+
+ // Static --------------------------------------------------------
+
+ private static String reverse(String text)
+ {
+ return new StringBuffer(text).reverse().toString();
+ }
+
+ // Constructors --------------------------------------------------
+
+ public TextReverserService(ConnectionFactory cf, Destination destination) throws JMSException
+ {
+ // create a JMS connection
+ connection = cf.createConnection();
+ // create a JMS session
+ session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ // create a JMS MessageConsumer to consume message from the destination
+ MessageConsumer consumer = session.createConsumer(destination);
+ // let TextReverter implement MessageListener to consume messages
+ consumer.setMessageListener(this);
+
+ // start the connection to start consuming messages
+ connection.start();
+ }
+
+ // MessageListener implementation --------------------------------
+
+ public void onMessage(Message request)
+ {
+ TextMessage textMessage = (TextMessage)request;
+ try
+ {
+ // retrieve the request's text
+ String text = textMessage.getText();
+ // create a reply containing the reversed text
+ TextMessage reply = session.createTextMessage(reverse(text));
+
+ // retrieve the destination to reply to
+ Destination replyTo = request.getJMSReplyTo();
+ // create a producer to send the reply
+ MessageProducer producer = session.createProducer(replyTo);
+ // send the reply
+ producer.send(reply);
+ // close the producer
+ producer.close();
+ }
+ catch (JMSException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ // Public --------------------------------------------------------
+
+ public void close()
+ {
+ if (connection != null)
+ {
+ try
+ {
+ // be sure to close the JMS resources
+ connection.close();
+ }
+ catch (JMSException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ // Package protected ---------------------------------------------
+
+ // Protected -----------------------------------------------------
+
+ // Private -------------------------------------------------------
+
+ // Inner classes -------------------------------------------------
+
+}
More information about the jboss-cvs-commits
mailing list