[jboss-cvs] JBoss Messaging SVN: r6249 - in trunk/examples/jms: topic-selector and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Mar 31 22:34:03 EDT 2009
Author: clebert.suconic at jboss.com
Date: 2009-03-31 22:34:03 -0400 (Tue, 31 Mar 2009)
New Revision: 6249
Added:
trunk/examples/jms/topic-selector/
trunk/examples/jms/topic-selector/build.xml
trunk/examples/jms/topic-selector/readme.html
trunk/examples/jms/topic-selector/src/
trunk/examples/jms/topic-selector/src/org/
trunk/examples/jms/topic-selector/src/org/jboss/
trunk/examples/jms/topic-selector/src/org/jboss/jms/
trunk/examples/jms/topic-selector/src/org/jboss/jms/example/
trunk/examples/jms/topic-selector/src/org/jboss/jms/example/TopicSelector.java
Log:
Adding topic-selector test (1st commit.. not final yet)
Property changes on: trunk/examples/jms/topic-selector
___________________________________________________________________
Name: svn:ignore
+ build
Added: trunk/examples/jms/topic-selector/build.xml
===================================================================
--- trunk/examples/jms/topic-selector/build.xml (rev 0)
+++ trunk/examples/jms/topic-selector/build.xml 2009-04-01 02:34:03 UTC (rev 6249)
@@ -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 Topic Example">
+
+ <import file="../common/build.xml"/>
+
+ <target name="run">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.TopicSelector"/>
+ </antcall>
+ </target>
+
+ <target name="runRemote">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.TopicSelector"/>
+ <param name="jbm.example.runServer" value="false"/>
+ </antcall>
+ </target>
+
+</project>
Added: trunk/examples/jms/topic-selector/readme.html
===================================================================
--- trunk/examples/jms/topic-selector/readme.html (rev 0)
+++ trunk/examples/jms/topic-selector/readme.html 2009-04-01 02:34:03 UTC (rev 6249)
@@ -0,0 +1,120 @@
+<html>
+ <head>
+ <title>JBoss Messaging JMS Topic With Selectors Example</title>
+ <link rel="stylesheet" type="text/css" href="../common/common.css">
+ </head>
+ <body>
+ <h1>JMS Topic Example</h1>
+ <br>
+ <p>This example shows you how to send message to a JMS Topic, and subscribe them using selectors with JBoss Messaging.</p>
+ <p>Topics and selectors are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p>
+ <p>A Topic is used to send messages using the publish-subscribe model, from a producer to 1 or more consumers.</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();</code>
+ </pre>
+
+ <li>We look-up the JMS topic object from JNDI</li>
+ <pre>
+ <code>Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");</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>Create one subscriber with a specific filter</li>
+ <pre>
+ <code>TopicSubscriber subscriberA = session.createDurableSubscriber(topic, "sub-a1", "userId=1", false);</code>
+ </pre>
+
+ <li>Create another subscriber</li>
+ <pre>
+ <code>TopicSubscriber subscriberB = session.createDurableSubscriber(topic, "sub-a2", "userId=2", false);</code>
+ </pre>
+
+ <li>Start the JMS Connections. This step will activate the subscribers to receive messages</li>
+ <pre>
+ <code>connection.start();</code>
+ </pre>
+
+ <li>Send two messages </li>
+ <pre>
+<code> for (int i = 1; i <= 2; i++)
+ {
+ // Create a text message
+ TextMessage message1 = session.createTextMessage("This is a text message " + i);
+
+ // Set a property
+ message1.setIntProperty("userId", i);
+
+ // Send the message
+ messageProducer.send(message1);
+ }</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>Receive the messages on subscription a and b</li>
+ <pre>
+ <code>TextMessage messageReceivedA = (TextMessage)subscriberA.receive();</code>
+ <code>System.out.println(messageReceivedA.getText());</code>
+ <code>TextMessage messageReceivedB = (TextMessage)subscriberB.receive();</code>
+ <code>System.out.println(messageReceivedB.getText());</code>
+ </pre>
+
+
+ <li>Close the consumers</li>
+ <pre><code>subscriberA.close();</code></pre>
+ <pre><code>subscriberB.close();</code></pre>
+
+ <li>Delete the subscriptions when you're done</li>
+ <pre><code>session.unsubscribe("sub-a1");</code></pre>
+ <pre><code>session.unsubscribe("sub-a2");</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 sessions, consumers, producer and browser objects</li>
+
+ <pre>
+ <code>
+
+ finally
+ {
+ if (connection != null)
+ {
+ // Step 14. Be sure to close our JMS resources!
+ connection.close();
+ }
+ }
+ </code>
+ </pre>
+
+
+
+ </ol>
+ </body>
+</html>
\ No newline at end of file
Added: trunk/examples/jms/topic-selector/src/org/jboss/jms/example/TopicSelector.java
===================================================================
--- trunk/examples/jms/topic-selector/src/org/jboss/jms/example/TopicSelector.java (rev 0)
+++ trunk/examples/jms/topic-selector/src/org/jboss/jms/example/TopicSelector.java 2009-04-01 02:34:03 UTC (rev 6249)
@@ -0,0 +1,130 @@
+/*
+ * 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.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.Topic;
+import javax.jms.TopicSubscriber;
+import javax.naming.InitialContext;
+
+/**
+ * A simple JMS Topic example that creates a producer and consumer on a queue and sends and receives a message.
+ *
+ * @author <a href="csuconic at redhat.com">Clebert Suconic</a>
+ */
+public class TopicSelector extends JMSExample
+{
+ public static void main(String[] args)
+ {
+ new TopicSelector().run(args);
+ }
+
+
+
+
+
+ public void runExample() throws Exception
+ {
+ Connection connection = null;
+ try
+ {
+
+ // Step 1. Create an initial context to perform the JNDI lookup.
+ InitialContext initialContext = getContext();
+
+ // Step 2. Look-up the JMS topic
+ Topic topic = (Topic)initialContext.lookup("/topic/exampleTopic");
+
+ // Step 3. Look-up the JMS 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 messageProducer = session.createProducer(topic);
+
+ // Step 7. Create one consumer with a specific Filter
+ TopicSubscriber subscriberA = session.createDurableSubscriber(topic, "sub-a1", "userId=1", false);
+
+ // Step 8. Create another consuemr with a different Filter
+ TopicSubscriber subscriberB = session.createDurableSubscriber(topic, "sub-a2", "userId=2", false);
+
+ // Step 9. Send two messages
+
+ for (int i = 1; i <= 2; i++)
+ {
+ // Step 9.1 Create a text message
+ TextMessage message1 = session.createTextMessage("This is a text message " + i);
+
+ // Step 9.1 Set a property
+ message1.setIntProperty("userId", i);
+
+ // Step 9.2 Send the message
+ messageProducer.send(message1);
+
+ System.out.println("Sent message: " + message1.getText());
+ }
+
+ // Step 10. Start the JMS Connection. This step will activate the subscribers to receive messages.
+ connection.start();
+
+
+ // Step 11. Consume the message from the durable subscription a
+
+ TextMessage messageReceivedA = (TextMessage)subscriberA.receive();
+
+ System.out.println("Received message: " + messageReceivedA.getText());
+
+
+ // Step 12. Consume the message from the durable subscription b
+
+ TextMessage messageReceivedB = (TextMessage)subscriberB.receive();
+
+ System.out.println("Received message: " + messageReceivedB.getText());
+
+
+ // Step 13. Close the subscribers
+ subscriberA.close();
+ subscriberA.close();
+
+ // Step 14. Delete the durable subscription
+ session.unsubscribe("sub-a1");
+ session.unsubscribe("sub-a2");
+ }
+ finally
+ {
+ if (connection != null)
+ {
+ // Step 15. Be sure to close our JMS resources!
+ connection.close();
+ }
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list