You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ha...@apache.org on 2014/04/29 20:34:15 UTC
[03/14] Use autocrlf consistently for line endings
http://git-wip-us.apache.org/repos/asf/activemq/blob/3f32507f/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/DurableChat/DurableChat.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/DurableChat/DurableChat.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/DurableChat/DurableChat.java
index 42156b1..753353c 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/DurableChat/DurableChat.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/DurableChat/DurableChat.java
@@ -1,283 +1,283 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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.
- */
-
-/*
-DurableChat application
-
-A basic JMS Application that uses:
- - Publish and Subscribe
- - Durable Subsciptions
- - Persistent Messages
-
-This sample publishes and subscribes to a specified topic.
-Text you enter is published to the topic with the user name.
-The message will persist for ten minutes if the subscriber is not available.
-If the subscriber reconnects within that time, the message is delivered.
-
-Usage:
- java DurableChat -b <broker:port> -u <username> -p <password>
- -b broker:port points to your message broker
- Default: tcp://localhost:61616
- -u username must be unique (but is not checked)
- -p password password for user (not checked)
-
-Suggested demonstration:
- - In separate console windows, start instances of the application
- under unique user names.For example:
- java DurableChat -b tcp://localhost:61616 -u ACCOUNTING
- java DurableChat -b tcp://localhost:61616 -u LEGAL
- - Enter text and then press Enter to publish the message.
- - See messages appear under the various user names as you
- enter messages in each console window.
- - Stop a session by pressing CTRL+C in its console window.
- - Keep sending messages in other sessions.
- - Restart the subscriber username session that was stopped.
- - Note that the "missed" messages are still available if the restart is
- within thirty minutes.
-
-*/
-import org.apache.activemq.*;
-
-
-public class DurableChat implements
- javax.jms.MessageListener // to handle message subscriptions
-{
- private static final String APP_TOPIC = "jms.samples.durablechat";
- private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
- private static final String DEFAULT_PASSWORD = "password";
- private static final long MESSAGE_LIFESPAN = 1800000; //30 minutes
-
- private javax.jms.Connection connection = null;
- private javax.jms.Session pubSession = null;
- private javax.jms.Session subSession = null;
-
-
- public void DurableChatter(String broker, String username, String password)
- {
- javax.jms.MessageProducer publisher = null;
- javax.jms.MessageConsumer subscriber = null;
- javax.jms.Topic topic = null;
-
- //Create a connection:
- try{
- javax.jms.ConnectionFactory factory;
- factory = new ActiveMQConnectionFactory(username, password, broker);
- connection = factory.createConnection (username, password);
-
- //Durable Subscriptions are indexed by username, clientID and subscription name
- //It is a good practice to set the clientID:
- connection.setClientID(username);
- pubSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
- subSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
- }
- catch (javax.jms.JMSException jmse){
- System.err.println ("Error: Cannot connect to Broker - " + broker);
- jmse.printStackTrace();
- System.exit(1);
- }
-
- //Create Publisher and Durable Subscriber:
- try{
-
- topic = pubSession.createTopic(APP_TOPIC);
- subscriber = subSession.createDurableSubscriber(topic, username);
- subscriber.setMessageListener(this);
- publisher = pubSession.createProducer(topic);
- connection.start();
- }
- catch (javax.jms.JMSException jmse){
- System.out.println("Error: connection not started.");
- jmse.printStackTrace();
- System.exit(1);
- }
-
- //Wait for user input
- try
- {
- System.out.println("\nDurableChat application:\n"
- + "========================\n"
- + "The user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
- + "The application will publish messages to the " + APP_TOPIC + " topic.\n"
- + "The application also creates a durable subscription to that topic to consume any messages published there.\n\n"
- + "Type some text, and then press Enter to publish it as a TextMesssage from " + username + ".\n");
- java.io.BufferedReader stdin =
- new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
- while (true)
- {
- String s = stdin.readLine();
-
- if(s == null){
- exit();
- }
- else if (s.length()>0)
- {
- try
- {
- javax.jms.TextMessage msg = pubSession.createTextMessage();
- msg.setText(username + ": " + s);
- //Publish the message persistantly:
- publisher.send(
- msg, //message
- javax.jms.DeliveryMode.PERSISTENT, //publish persistently
- javax.jms.Message.DEFAULT_PRIORITY,//priority
- MESSAGE_LIFESPAN); //Time to Live
- }
- catch (javax.jms.JMSException jmse){
- System.err.println("Error publishing message:" + jmse.getMessage());
- }
- }
- }
- }
- catch (java.io.IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
-
- /** Message Handler**/
- public void onMessage( javax.jms.Message aMessage)
- {
- try
- {
- // Cast the message as a text message.
- javax.jms.TextMessage textMessage = (javax.jms.TextMessage) aMessage;
-
- // This handler reads a single String from the
- // message and prints it to the standard output.
- try
- {
- String string = textMessage.getText();
- System.out.println( string );
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- }
- catch (java.lang.RuntimeException rte)
- {
- rte.printStackTrace();
- }
- }
-
-
-
- /** Cleanup resources cleanly and exit. */
- private void exit()
- {
- try
- {
- connection.close();
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
-
- System.exit(0);
- }
-
- //
- // NOTE: the remainder of this sample deals with reading arguments
- // and does not utilize any JMS classes or code.
- //
-
- /** Main program entry point. */
- public static void main(String argv[]) {
- // Is there anything to do?
- if (argv.length == 0) {
- printUsage();
- System.exit(1);
- }
-
- // Values to be read from parameters
- String broker = DEFAULT_BROKER_NAME;
- String username = null;
- String password = DEFAULT_PASSWORD;
-
- // Check parameters
- for (int i = 0; i < argv.length; i++) {
- String arg = argv[i];
-
-
- if (arg.equals("-b")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing broker name:port");
- System.exit(1);
- }
- broker = argv[++i];
- continue;
- }
-
- if (arg.equals("-u")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing user name");
- System.exit(1);
- }
- username = argv[++i];
- continue;
- }
-
- if (arg.equals("-p")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing password");
- System.exit(1);
- }
- password = argv[++i];
- continue;
- }
-
- if (arg.equals("-h")) {
- printUsage();
- System.exit(1);
- }
-
- // Invalid argument
- System.err.println ("error: unexpected argument: "+arg);
- printUsage();
- System.exit(1);
- }
-
- // Check values read in.
- if (username == null) {
- System.err.println ("error: user name must be supplied");
- printUsage();
- }
-
- // Start the JMS client for the "chat".
- DurableChat durableChat = new DurableChat();
- durableChat.DurableChatter (broker, username, password);
-
- }
-
- /** Prints the usage. */
- private static void printUsage() {
-
- StringBuffer use = new StringBuffer();
- use.append("usage: java DurableChat (options) ...\n\n");
- use.append("options:\n");
- use.append(" -b name:port Specify name:port of broker.\n");
- use.append(" Default broker: "+DEFAULT_BROKER_NAME+"\n");
- use.append(" -u username Specify unique user name. (Required)\n");
- use.append(" -p password Specify password for user.\n");
- use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
- use.append(" -h This help screen.\n");
- System.err.println (use);
- }
-
-}
-
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.
+ */
+
+/*
+DurableChat application
+
+A basic JMS Application that uses:
+ - Publish and Subscribe
+ - Durable Subsciptions
+ - Persistent Messages
+
+This sample publishes and subscribes to a specified topic.
+Text you enter is published to the topic with the user name.
+The message will persist for ten minutes if the subscriber is not available.
+If the subscriber reconnects within that time, the message is delivered.
+
+Usage:
+ java DurableChat -b <broker:port> -u <username> -p <password>
+ -b broker:port points to your message broker
+ Default: tcp://localhost:61616
+ -u username must be unique (but is not checked)
+ -p password password for user (not checked)
+
+Suggested demonstration:
+ - In separate console windows, start instances of the application
+ under unique user names.For example:
+ java DurableChat -b tcp://localhost:61616 -u ACCOUNTING
+ java DurableChat -b tcp://localhost:61616 -u LEGAL
+ - Enter text and then press Enter to publish the message.
+ - See messages appear under the various user names as you
+ enter messages in each console window.
+ - Stop a session by pressing CTRL+C in its console window.
+ - Keep sending messages in other sessions.
+ - Restart the subscriber username session that was stopped.
+ - Note that the "missed" messages are still available if the restart is
+ within thirty minutes.
+
+*/
+import org.apache.activemq.*;
+
+
+public class DurableChat implements
+ javax.jms.MessageListener // to handle message subscriptions
+{
+ private static final String APP_TOPIC = "jms.samples.durablechat";
+ private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
+ private static final String DEFAULT_PASSWORD = "password";
+ private static final long MESSAGE_LIFESPAN = 1800000; //30 minutes
+
+ private javax.jms.Connection connection = null;
+ private javax.jms.Session pubSession = null;
+ private javax.jms.Session subSession = null;
+
+
+ public void DurableChatter(String broker, String username, String password)
+ {
+ javax.jms.MessageProducer publisher = null;
+ javax.jms.MessageConsumer subscriber = null;
+ javax.jms.Topic topic = null;
+
+ //Create a connection:
+ try{
+ javax.jms.ConnectionFactory factory;
+ factory = new ActiveMQConnectionFactory(username, password, broker);
+ connection = factory.createConnection (username, password);
+
+ //Durable Subscriptions are indexed by username, clientID and subscription name
+ //It is a good practice to set the clientID:
+ connection.setClientID(username);
+ pubSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
+ subSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
+ }
+ catch (javax.jms.JMSException jmse){
+ System.err.println ("Error: Cannot connect to Broker - " + broker);
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ //Create Publisher and Durable Subscriber:
+ try{
+
+ topic = pubSession.createTopic(APP_TOPIC);
+ subscriber = subSession.createDurableSubscriber(topic, username);
+ subscriber.setMessageListener(this);
+ publisher = pubSession.createProducer(topic);
+ connection.start();
+ }
+ catch (javax.jms.JMSException jmse){
+ System.out.println("Error: connection not started.");
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ //Wait for user input
+ try
+ {
+ System.out.println("\nDurableChat application:\n"
+ + "========================\n"
+ + "The user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
+ + "The application will publish messages to the " + APP_TOPIC + " topic.\n"
+ + "The application also creates a durable subscription to that topic to consume any messages published there.\n\n"
+ + "Type some text, and then press Enter to publish it as a TextMesssage from " + username + ".\n");
+ java.io.BufferedReader stdin =
+ new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
+ while (true)
+ {
+ String s = stdin.readLine();
+
+ if(s == null){
+ exit();
+ }
+ else if (s.length()>0)
+ {
+ try
+ {
+ javax.jms.TextMessage msg = pubSession.createTextMessage();
+ msg.setText(username + ": " + s);
+ //Publish the message persistantly:
+ publisher.send(
+ msg, //message
+ javax.jms.DeliveryMode.PERSISTENT, //publish persistently
+ javax.jms.Message.DEFAULT_PRIORITY,//priority
+ MESSAGE_LIFESPAN); //Time to Live
+ }
+ catch (javax.jms.JMSException jmse){
+ System.err.println("Error publishing message:" + jmse.getMessage());
+ }
+ }
+ }
+ }
+ catch (java.io.IOException ioe)
+ {
+ ioe.printStackTrace();
+ }
+ }
+
+ /** Message Handler**/
+ public void onMessage( javax.jms.Message aMessage)
+ {
+ try
+ {
+ // Cast the message as a text message.
+ javax.jms.TextMessage textMessage = (javax.jms.TextMessage) aMessage;
+
+ // This handler reads a single String from the
+ // message and prints it to the standard output.
+ try
+ {
+ String string = textMessage.getText();
+ System.out.println( string );
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ }
+ catch (java.lang.RuntimeException rte)
+ {
+ rte.printStackTrace();
+ }
+ }
+
+
+
+ /** Cleanup resources cleanly and exit. */
+ private void exit()
+ {
+ try
+ {
+ connection.close();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+
+ System.exit(0);
+ }
+
+ //
+ // NOTE: the remainder of this sample deals with reading arguments
+ // and does not utilize any JMS classes or code.
+ //
+
+ /** Main program entry point. */
+ public static void main(String argv[]) {
+ // Is there anything to do?
+ if (argv.length == 0) {
+ printUsage();
+ System.exit(1);
+ }
+
+ // Values to be read from parameters
+ String broker = DEFAULT_BROKER_NAME;
+ String username = null;
+ String password = DEFAULT_PASSWORD;
+
+ // Check parameters
+ for (int i = 0; i < argv.length; i++) {
+ String arg = argv[i];
+
+
+ if (arg.equals("-b")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing broker name:port");
+ System.exit(1);
+ }
+ broker = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-u")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing user name");
+ System.exit(1);
+ }
+ username = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-p")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing password");
+ System.exit(1);
+ }
+ password = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-h")) {
+ printUsage();
+ System.exit(1);
+ }
+
+ // Invalid argument
+ System.err.println ("error: unexpected argument: "+arg);
+ printUsage();
+ System.exit(1);
+ }
+
+ // Check values read in.
+ if (username == null) {
+ System.err.println ("error: user name must be supplied");
+ printUsage();
+ }
+
+ // Start the JMS client for the "chat".
+ DurableChat durableChat = new DurableChat();
+ durableChat.DurableChatter (broker, username, password);
+
+ }
+
+ /** Prints the usage. */
+ private static void printUsage() {
+
+ StringBuffer use = new StringBuffer();
+ use.append("usage: java DurableChat (options) ...\n\n");
+ use.append("options:\n");
+ use.append(" -b name:port Specify name:port of broker.\n");
+ use.append(" Default broker: "+DEFAULT_BROKER_NAME+"\n");
+ use.append(" -u username Specify unique user name. (Required)\n");
+ use.append(" -p password Specify password for user.\n");
+ use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
+ use.append(" -h This help screen.\n");
+ System.err.println (use);
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/activemq/blob/3f32507f/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/HierarchicalChat/HierarchicalChat.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/HierarchicalChat/HierarchicalChat.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/HierarchicalChat/HierarchicalChat.java
index 997e3b5..ac743b2 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/HierarchicalChat/HierarchicalChat.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/HierarchicalChat/HierarchicalChat.java
@@ -1,303 +1,303 @@
-/*
- * � 2001-2009, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.
- *
- * Licensed 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.
-
-Sample Application
-
-Writing a JMS Application using Publish and Subscribe with
-Hierarchical Topics
-
-This sample publishes and subscribes to specified topic nodes.
-Text you enter is published and then received by all subscribers to
-the that topic on the specified broker.
-
-Usage:
- java HierarchicalChat -b <broker:port> -u <username> -p <password> -t <pubTopicname> -s <subTopicname>
- -b broker:port points to a message broker
- Default: tcp://localhost:61616
- -u username must be unique (but is not checked)
- -p password password for user (not checked)
- -t pubTopicname name of topic to which to publish
- Default: jms.samples.hierarchicalchat
- -s subTopicname name of topic to which to subscribe
- Default: jms.samples.*
-
-Suggested demonstration:
- - In separate console windows with the environment set,
- start instances of the application
- under unique user names.
- For example:
- java HierarchicalChat -u SALES -t sales -s sales.*
- java HierarchicalChat -u USA -t sales.usa -s sales.usa
- - Enter text in the USA console window and then press Enter
- to publish the message.
- - Note that messages published from the SALES console window
- to the sales topic are not seen by the USA user listening
- to messages on the sales.usa topic
- - Message published to the sales.usa are received by the SALES
- user listening to sales.*
- - Stop a session by pressing CTRL+C in its console window.
-
-*/
-
-import org.apache.activemq.*;
-
-
-public class HierarchicalChat
- implements javax.jms.MessageListener
-{
- private static final String DEFAULT_PUBLISHER_TOPIC = "jms.samples.hierarchicalchat";
- private static final String DEFAULT_SUBSCRIBER_TOPIC = "jms.samples.*";
- private static final String DEFAULT_SUBSCRIBER_ROOT = "jms.samples";
- private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
-
- private static final String DEFAULT_PASSWORD = "password";
-
- private javax.jms.Connection connect = null;
- private javax.jms.Session pubSession = null;
- private javax.jms.Session subSession = null;
- private javax.jms.MessageProducer publisher = null;
-
- /** Create JMS client for publishing and subscribing to messages. */
- private void chatter( String broker, String username, String password, String pubTopicname, String subTopicname)
- {
-
- // Create a connection.
- try
- {
- javax.jms.ConnectionFactory factory;
- factory = new ActiveMQConnectionFactory(username, password, broker);
- connect = factory.createConnection (username, password);
- pubSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
- subSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
- }
- catch (javax.jms.JMSException jmse)
- {
- System.err.println("error: Cannot connect to Broker - " + broker);
- jmse.printStackTrace();
- System.exit(1);
- }
-
- // Create Publisher and Subscriber to 'chat' topics
- // Note that the publish and subscribe topics are different.
- try
- {
- javax.jms.Topic subscriberTopic = pubSession.createTopic (subTopicname);
- javax.jms.MessageConsumer subscriber = subSession.createConsumer(subscriberTopic, null, false);
- subscriber.setMessageListener(this);
- javax.jms.Topic publisherTopic = pubSession.createTopic (pubTopicname);
- publisher = pubSession.createProducer(publisherTopic);
- // Now that setup is complete, start the Connection
- connect.start();
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- try
- {
- // Read all standard input and send it as a message.
- java.io.BufferedReader stdin =
- new java.io.BufferedReader( new java.io.InputStreamReader( System.in ) );
- System.out.println("\nHierarchicalChat application:\n"
- + "============================\n"
- + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
- + "The application will publish messages to the " + DEFAULT_PUBLISHER_TOPIC + " topic." + ".\n"
- + "The application also subscribes to topics using the wildcard syntax " + DEFAULT_SUBSCRIBER_TOPIC
- + " so that it can receive all messages to " + DEFAULT_SUBSCRIBER_ROOT + " and its subtopics.\n\n"
- + "Type some text, and then press Enter to publish a TextMesssage from " + username + ".\n");
- while ( true )
- {
- String s = stdin.readLine();
-
- if ( s == null )
- exit();
- else if ( s.length() > 0 )
- {
- javax.jms.TextMessage msg = pubSession.createTextMessage();
- msg.setText( username + ": " + s );
- publisher.send( msg );
- }
- }
- }
- catch ( java.io.IOException ioe )
- {
- ioe.printStackTrace();
- }
- catch ( javax.jms.JMSException jmse )
- {
- jmse.printStackTrace();
- }
- }
-
- /**
- * Handle the message
- * (as specified in the javax.jms.MessageListener interface).
- */
- public void onMessage( javax.jms.Message aMessage)
- {
- try
- {
- // Cast the message as a text message.
- javax.jms.TextMessage textMessage = (javax.jms.TextMessage) aMessage;
-
- // This handler reads a single String from the
- // message and prints it to the standard output.
- try
- {
- String string = textMessage.getText();
- System.out.println( string );
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- }
- catch (java.lang.RuntimeException rte)
- {
- rte.printStackTrace();
- }
- }
-
- /** Cleanup resources and then exit. */
- private void exit()
- {
- try
- {
- connect.close();
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
-
- System.exit(0);
- }
-
- //
- // NOTE: the remainder of this sample deals with reading arguments
- // and does not utilize any JMS classes or code.
- //
-
- /** Main program entry point. */
- public static void main(String argv[]) {
-
- // Is there anything to do?
- if (argv.length == 0) {
- printUsage();
- System.exit(1);
- }
-
- // Values to be read from parameters
- String broker = DEFAULT_BROKER_NAME;
- String username = null;
- String password = DEFAULT_PASSWORD;
- String pubTopicname = DEFAULT_PUBLISHER_TOPIC;
- String subTopicname = DEFAULT_SUBSCRIBER_TOPIC;
-
- // Check parameters
- for (int i = 0; i < argv.length; i++) {
- String arg = argv[i];
-
- // Options
- if (!arg.startsWith("-")) {
- System.err.println ("error: unexpected argument - "+arg);
- printUsage();
- System.exit(1);
- }
- else {
- if (arg.equals("-b")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing broker name:port");
- System.exit(1);
- }
- broker = argv[++i];
- continue;
- }
-
- if (arg.equals("-u")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing user name");
- System.exit(1);
- }
- username = argv[++i];
- continue;
- }
-
- if (arg.equals("-p")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing password");
- System.exit(1);
- }
- password = argv[++i];
- continue;
- }
- if (arg.equals("-t")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing publisher topic name");
- System.exit(1);
- }
- pubTopicname = argv[++i];
- continue;
- }
-
- if (arg.equals("-s")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing subscriber topic name");
- System.exit(1);
- }
- subTopicname = argv[++i];
- continue;
- }
-
- if (arg.equals("-h")) {
- printUsage();
- System.exit(1);
- }
- }
- }
-
- // Check values read in.
- if (username == null) {
- System.err.println ("error: user name must be supplied");
- printUsage();
- }
-
- // Start the JMS client for the "chat".
- HierarchicalChat chat = new HierarchicalChat();
- chat.chatter (broker, username, password, pubTopicname, subTopicname);
-
- }
-
- /** Prints the usage. */
- private static void printUsage() {
-
- StringBuffer use = new StringBuffer();
- use.append("usage: java HierarchicalChat (options) ...\n\n");
- use.append("options:\n");
- use.append(" -b name:port Specify name:port of broker.\n");
- use.append(" Default broker: "+DEFAULT_BROKER_NAME+"\n");
- use.append(" -u name Specify unique user name. (Required)\n");
- use.append(" -p password Specify password for user.\n");
- use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
- use.append(" -t pubTopicname name of topic to which to publish.\n");
- use.append(" Default publisher topic name: "+DEFAULT_PUBLISHER_TOPIC+"\n");
- use.append(" -s subTopicname Specify subscriber topic name.\n");
- use.append(" name of topic to which to subscribe: "+DEFAULT_SUBSCRIBER_TOPIC+"\n");
- use.append(" -h This help screen.\n");
- System.err.println (use);
- }
-
-}
-
+/*
+ * � 2001-2009, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.
+ *
+ * Licensed 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.
+
+Sample Application
+
+Writing a JMS Application using Publish and Subscribe with
+Hierarchical Topics
+
+This sample publishes and subscribes to specified topic nodes.
+Text you enter is published and then received by all subscribers to
+the that topic on the specified broker.
+
+Usage:
+ java HierarchicalChat -b <broker:port> -u <username> -p <password> -t <pubTopicname> -s <subTopicname>
+ -b broker:port points to a message broker
+ Default: tcp://localhost:61616
+ -u username must be unique (but is not checked)
+ -p password password for user (not checked)
+ -t pubTopicname name of topic to which to publish
+ Default: jms.samples.hierarchicalchat
+ -s subTopicname name of topic to which to subscribe
+ Default: jms.samples.*
+
+Suggested demonstration:
+ - In separate console windows with the environment set,
+ start instances of the application
+ under unique user names.
+ For example:
+ java HierarchicalChat -u SALES -t sales -s sales.*
+ java HierarchicalChat -u USA -t sales.usa -s sales.usa
+ - Enter text in the USA console window and then press Enter
+ to publish the message.
+ - Note that messages published from the SALES console window
+ to the sales topic are not seen by the USA user listening
+ to messages on the sales.usa topic
+ - Message published to the sales.usa are received by the SALES
+ user listening to sales.*
+ - Stop a session by pressing CTRL+C in its console window.
+
+*/
+
+import org.apache.activemq.*;
+
+
+public class HierarchicalChat
+ implements javax.jms.MessageListener
+{
+ private static final String DEFAULT_PUBLISHER_TOPIC = "jms.samples.hierarchicalchat";
+ private static final String DEFAULT_SUBSCRIBER_TOPIC = "jms.samples.*";
+ private static final String DEFAULT_SUBSCRIBER_ROOT = "jms.samples";
+ private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
+
+ private static final String DEFAULT_PASSWORD = "password";
+
+ private javax.jms.Connection connect = null;
+ private javax.jms.Session pubSession = null;
+ private javax.jms.Session subSession = null;
+ private javax.jms.MessageProducer publisher = null;
+
+ /** Create JMS client for publishing and subscribing to messages. */
+ private void chatter( String broker, String username, String password, String pubTopicname, String subTopicname)
+ {
+
+ // Create a connection.
+ try
+ {
+ javax.jms.ConnectionFactory factory;
+ factory = new ActiveMQConnectionFactory(username, password, broker);
+ connect = factory.createConnection (username, password);
+ pubSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
+ subSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ System.err.println("error: Cannot connect to Broker - " + broker);
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ // Create Publisher and Subscriber to 'chat' topics
+ // Note that the publish and subscribe topics are different.
+ try
+ {
+ javax.jms.Topic subscriberTopic = pubSession.createTopic (subTopicname);
+ javax.jms.MessageConsumer subscriber = subSession.createConsumer(subscriberTopic, null, false);
+ subscriber.setMessageListener(this);
+ javax.jms.Topic publisherTopic = pubSession.createTopic (pubTopicname);
+ publisher = pubSession.createProducer(publisherTopic);
+ // Now that setup is complete, start the Connection
+ connect.start();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ try
+ {
+ // Read all standard input and send it as a message.
+ java.io.BufferedReader stdin =
+ new java.io.BufferedReader( new java.io.InputStreamReader( System.in ) );
+ System.out.println("\nHierarchicalChat application:\n"
+ + "============================\n"
+ + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
+ + "The application will publish messages to the " + DEFAULT_PUBLISHER_TOPIC + " topic." + ".\n"
+ + "The application also subscribes to topics using the wildcard syntax " + DEFAULT_SUBSCRIBER_TOPIC
+ + " so that it can receive all messages to " + DEFAULT_SUBSCRIBER_ROOT + " and its subtopics.\n\n"
+ + "Type some text, and then press Enter to publish a TextMesssage from " + username + ".\n");
+ while ( true )
+ {
+ String s = stdin.readLine();
+
+ if ( s == null )
+ exit();
+ else if ( s.length() > 0 )
+ {
+ javax.jms.TextMessage msg = pubSession.createTextMessage();
+ msg.setText( username + ": " + s );
+ publisher.send( msg );
+ }
+ }
+ }
+ catch ( java.io.IOException ioe )
+ {
+ ioe.printStackTrace();
+ }
+ catch ( javax.jms.JMSException jmse )
+ {
+ jmse.printStackTrace();
+ }
+ }
+
+ /**
+ * Handle the message
+ * (as specified in the javax.jms.MessageListener interface).
+ */
+ public void onMessage( javax.jms.Message aMessage)
+ {
+ try
+ {
+ // Cast the message as a text message.
+ javax.jms.TextMessage textMessage = (javax.jms.TextMessage) aMessage;
+
+ // This handler reads a single String from the
+ // message and prints it to the standard output.
+ try
+ {
+ String string = textMessage.getText();
+ System.out.println( string );
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ }
+ catch (java.lang.RuntimeException rte)
+ {
+ rte.printStackTrace();
+ }
+ }
+
+ /** Cleanup resources and then exit. */
+ private void exit()
+ {
+ try
+ {
+ connect.close();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+
+ System.exit(0);
+ }
+
+ //
+ // NOTE: the remainder of this sample deals with reading arguments
+ // and does not utilize any JMS classes or code.
+ //
+
+ /** Main program entry point. */
+ public static void main(String argv[]) {
+
+ // Is there anything to do?
+ if (argv.length == 0) {
+ printUsage();
+ System.exit(1);
+ }
+
+ // Values to be read from parameters
+ String broker = DEFAULT_BROKER_NAME;
+ String username = null;
+ String password = DEFAULT_PASSWORD;
+ String pubTopicname = DEFAULT_PUBLISHER_TOPIC;
+ String subTopicname = DEFAULT_SUBSCRIBER_TOPIC;
+
+ // Check parameters
+ for (int i = 0; i < argv.length; i++) {
+ String arg = argv[i];
+
+ // Options
+ if (!arg.startsWith("-")) {
+ System.err.println ("error: unexpected argument - "+arg);
+ printUsage();
+ System.exit(1);
+ }
+ else {
+ if (arg.equals("-b")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing broker name:port");
+ System.exit(1);
+ }
+ broker = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-u")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing user name");
+ System.exit(1);
+ }
+ username = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-p")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing password");
+ System.exit(1);
+ }
+ password = argv[++i];
+ continue;
+ }
+ if (arg.equals("-t")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing publisher topic name");
+ System.exit(1);
+ }
+ pubTopicname = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-s")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing subscriber topic name");
+ System.exit(1);
+ }
+ subTopicname = argv[++i];
+ continue;
+ }
+
+ if (arg.equals("-h")) {
+ printUsage();
+ System.exit(1);
+ }
+ }
+ }
+
+ // Check values read in.
+ if (username == null) {
+ System.err.println ("error: user name must be supplied");
+ printUsage();
+ }
+
+ // Start the JMS client for the "chat".
+ HierarchicalChat chat = new HierarchicalChat();
+ chat.chatter (broker, username, password, pubTopicname, subTopicname);
+
+ }
+
+ /** Prints the usage. */
+ private static void printUsage() {
+
+ StringBuffer use = new StringBuffer();
+ use.append("usage: java HierarchicalChat (options) ...\n\n");
+ use.append("options:\n");
+ use.append(" -b name:port Specify name:port of broker.\n");
+ use.append(" Default broker: "+DEFAULT_BROKER_NAME+"\n");
+ use.append(" -u name Specify unique user name. (Required)\n");
+ use.append(" -p password Specify password for user.\n");
+ use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
+ use.append(" -t pubTopicname name of topic to which to publish.\n");
+ use.append(" Default publisher topic name: "+DEFAULT_PUBLISHER_TOPIC+"\n");
+ use.append(" -s subTopicname Specify subscriber topic name.\n");
+ use.append(" name of topic to which to subscribe: "+DEFAULT_SUBSCRIBER_TOPIC+"\n");
+ use.append(" -h This help screen.\n");
+ System.err.println (use);
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/activemq/blob/3f32507f/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.java
index 19ab78f..20b03e1 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.java
@@ -1,328 +1,328 @@
-/*
- * � 2001-2009, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.
- *
- * Licensed 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.
-
-Sample Application
-
-Writing a Basic JMS Application Using
- Subscribe
-
-When you run this program, it will
-subscribe to any group of messages specified
-in a properties file. [See comments in
-MessageMonitor.properties for information on
-this file.]
-
-Every message topic being monitored will be
-displayed to a Java window.
-
-Usage:
- java MessageMonitor
-
-Suggested demonstration:
- - Start one instance of this application:
- java MessageMonitor
- - Run one or more Chat and/or DurableChat window(s).
- - Enter messages on the various chat windows.
- - Watch the MessageMonitor display the messages.
-*/
-import org.apache.activemq.*;
-
-import javax.swing.JTextArea;
-import javax.swing.JFrame;
-import javax.swing.JScrollPane;
-import javax.swing.JButton;
-
-import javax.swing.text.Highlighter;
-import javax.swing.text.DefaultHighlighter;
-import javax.swing.text.BadLocationException;
-
-import javax.swing.border.CompoundBorder;
-import javax.swing.border.EmptyBorder;
-import javax.swing.border.BevelBorder;
-import javax.swing.border.SoftBevelBorder;
-
-import java.awt.Toolkit;
-import java.awt.Dimension;
-import java.awt.BorderLayout;
-import java.awt.Rectangle;
-
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
-
-import javax.jms.Topic;
-import javax.jms.Session;
-import javax.jms.Connection;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
-
-
-import java.io.FileInputStream;
-
-import java.util.Vector;
-import java.util.Properties;
-import java.util.StringTokenizer;
-
-public class MessageMonitor
-extends JFrame
-{
- private static final String DEFAULT_PROPERTIES_FILE = "MessageMonitor.properties";
-
- String propertiesFile = DEFAULT_PROPERTIES_FILE;
- String brokerHostPort = "localhost";
- String connectID = "MessageMonitor";
- String userID = "Administrator";
-
- String subscriptionTopics = "jms.samples.chat";
- String textFontName = "Dialog";
- String textFontStyle = "PLAIN";
- String textFontSize = "12";
- String title = "MessageMonitor";
-
- JTextArea textArea = new JTextArea();
- JScrollPane scrollPane = new JScrollPane(textArea);
- JButton clearButton = new JButton("Clear");
-
- Connection connection = null;
- Session session = null;
-
- private String user = ActiveMQConnection.DEFAULT_USER;
- private String password = ActiveMQConnection.DEFAULT_PASSWORD;
- private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
-
-
- /** Constructor for MessageMonitor window. */
- public MessageMonitor()
- {
- loadProperties();
-
- setTitle(title);
-
- // Connect to Message Broker
- try
- {
- javax.jms.ConnectionFactory factory;
- factory = new ActiveMQConnectionFactory(user, password, url);
-
- connection = factory.createConnection (userID, password);
- session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
- }
- catch (javax.jms.JMSException jmse)
- {
- System.err.println("Cannot connect to Broker");
- jmse.printStackTrace();
- System.exit(1);
- }
-
- // Subscribe to Topics
- StringTokenizer topics = new StringTokenizer(subscriptionTopics, ",");
- while (topics.hasMoreTokens())
- {
- try
- {
- String topicName = topics.nextToken();
- System.out.println ("Subscribing to: " + topicName);
- new Subscription(session.createTopic(topicName));
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- }
-
- // Set up the viewing area.
- textArea.setEditable(false);
- scrollPane.setBorder(new CompoundBorder(new EmptyBorder(6,6,6,6),
- new SoftBevelBorder(BevelBorder.LOWERED)));
- getContentPane().add(scrollPane,BorderLayout.CENTER);
- getContentPane().add(clearButton,BorderLayout.SOUTH);
- clearButton.addActionListener(new OnClear());
- // Start the connection so that we can now receive messages.
- try
- {
- connection.start();
- }
- catch (javax.jms.JMSException jmse)
- {
- System.err.println("Cannot start connection");
- jmse.printStackTrace();
- System.exit(1);
- }
-
- }
-
- /** Class to handle the subsciption to messages. */
- public class Subscription implements javax.jms.MessageListener
- {
- public Subscription(Topic topic)
- {
- try
- {
- topicSubscriber = session.createConsumer(topic);
- topicSubscriber.setMessageListener(this);
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- }
-
- /**
- * Handle the text message
- * (as specified in the javax.jms.MessageListener interface).
- */
-
- public void onMessage(javax.jms.Message message)
- {
- String msgBody = null;
- String msgClass = null;
-
- if (message instanceof javax.jms.TextMessage)
- {
- msgClass = "javax.jms.TextMessage";
- try
- {
- msgBody = ((javax.jms.TextMessage)message).getText();
- }
- catch (javax.jms.JMSException jmse)
- {
- msgBody = "";
- }
- }
- else
- {
- return;
- }
- try
- {
- textArea.append("\n");
- textArea.append("-----------------------------------------------------------------------------------------------------\n");
- // textArea.append("Class: " + msgClass + "\n");
- textArea.append("The following message, received on topic " + ((Topic)message.getJMSDestination()).getTopicName() + ", was sent by\n");
- //textArea.append("\n");
- textArea.append(msgBody);
-
- // Ensure Appended Text is Visible
- Rectangle area = textArea.modelToView(textArea.getText().length());
- if (area != null) textArea.scrollRectToVisible(area);
-
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- catch (BadLocationException ble)
- {
- ble.printStackTrace();
- }
-
- }
-
- MessageConsumer topicSubscriber = null;
- }
-
- //
- // NOTE: the remainder of this sample deals with reading arguments
- // and does not utilize any JMS classes or code.
- //
-
- /** Main program entry point. */
- public static void main(String[] args)
- {
- // There should be no arguments to this program.
- if (args.length > 0) {
- printUsage();
- System.exit(1);
- }
-
- MessageMonitor messageMonitor = new MessageMonitor();
-
- messageMonitor.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
-
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- screenSize.height = screenSize.height / 2 ;
- screenSize.width = screenSize.width / 2 ;
- messageMonitor.setSize(screenSize);
- messageMonitor.setVisible(true);
-
- }
-
- /** Prints the usage. */
- private static void printUsage()
- {
- StringBuffer use = new StringBuffer();
- use.append("\nusage: MessageMonitor\n\n");
- use.append("Properties for this sample can be set in a properties file.\n");
- String dfltFile = System.getProperty("propertiesFile", DEFAULT_PROPERTIES_FILE);
- use.append("[Default file: " + dfltFile +"]\n\n");
-
- System.out.print(use);
- }
-
- /** Load the window and JMS properties from a file. */
- private void loadProperties()
- {
- try
- {
- Properties properties = new Properties();
-
- propertiesFile = System.getProperty("propertiesFile", propertiesFile);
-
- properties.load(new FileInputStream(propertiesFile));
-
- // Connection Properties
- brokerHostPort = properties.getProperty("brokerHostPort",brokerHostPort).trim();
- connectID = properties.getProperty("connectID",connectID).trim();
- userID = properties.getProperty("userID",userID).trim();
- password = properties.getProperty("password",password).trim();
-
- // Subscription Properties
- subscriptionTopics = properties.getProperty("subscriptionTopics", subscriptionTopics).trim();
-
- // Text Properties
- textFontName = properties.getProperty("textFontName", textFontName).trim();
- textFontStyle = properties.getProperty("textFontSize", textFontStyle).trim();
- textFontSize = properties.getProperty("textFontSize", textFontSize).trim();
-
- // Window Properties
- title = properties.getProperty("title", title).trim();
-
- }
- catch (java.io.FileNotFoundException fnfe)
- {
- System.out.println (propertiesFile + " not found: using defaults"); // Use Defaults
- }
- catch (java.io.IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
-
- /** Class to handle the "Clear" button action. */
- public class OnClear implements ActionListener
- {
- public void actionPerformed(ActionEvent evt)
- {
- textArea.setText("");
- }
- }
-
-}
+/*
+ * � 2001-2009, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.
+ *
+ * Licensed 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.
+
+Sample Application
+
+Writing a Basic JMS Application Using
+ Subscribe
+
+When you run this program, it will
+subscribe to any group of messages specified
+in a properties file. [See comments in
+MessageMonitor.properties for information on
+this file.]
+
+Every message topic being monitored will be
+displayed to a Java window.
+
+Usage:
+ java MessageMonitor
+
+Suggested demonstration:
+ - Start one instance of this application:
+ java MessageMonitor
+ - Run one or more Chat and/or DurableChat window(s).
+ - Enter messages on the various chat windows.
+ - Watch the MessageMonitor display the messages.
+*/
+import org.apache.activemq.*;
+
+import javax.swing.JTextArea;
+import javax.swing.JFrame;
+import javax.swing.JScrollPane;
+import javax.swing.JButton;
+
+import javax.swing.text.Highlighter;
+import javax.swing.text.DefaultHighlighter;
+import javax.swing.text.BadLocationException;
+
+import javax.swing.border.CompoundBorder;
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.BevelBorder;
+import javax.swing.border.SoftBevelBorder;
+
+import java.awt.Toolkit;
+import java.awt.Dimension;
+import java.awt.BorderLayout;
+import java.awt.Rectangle;
+
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+import javax.jms.Topic;
+import javax.jms.Session;
+import javax.jms.Connection;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+
+
+import java.io.FileInputStream;
+
+import java.util.Vector;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+public class MessageMonitor
+extends JFrame
+{
+ private static final String DEFAULT_PROPERTIES_FILE = "MessageMonitor.properties";
+
+ String propertiesFile = DEFAULT_PROPERTIES_FILE;
+ String brokerHostPort = "localhost";
+ String connectID = "MessageMonitor";
+ String userID = "Administrator";
+
+ String subscriptionTopics = "jms.samples.chat";
+ String textFontName = "Dialog";
+ String textFontStyle = "PLAIN";
+ String textFontSize = "12";
+ String title = "MessageMonitor";
+
+ JTextArea textArea = new JTextArea();
+ JScrollPane scrollPane = new JScrollPane(textArea);
+ JButton clearButton = new JButton("Clear");
+
+ Connection connection = null;
+ Session session = null;
+
+ private String user = ActiveMQConnection.DEFAULT_USER;
+ private String password = ActiveMQConnection.DEFAULT_PASSWORD;
+ private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
+
+
+ /** Constructor for MessageMonitor window. */
+ public MessageMonitor()
+ {
+ loadProperties();
+
+ setTitle(title);
+
+ // Connect to Message Broker
+ try
+ {
+ javax.jms.ConnectionFactory factory;
+ factory = new ActiveMQConnectionFactory(user, password, url);
+
+ connection = factory.createConnection (userID, password);
+ session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ System.err.println("Cannot connect to Broker");
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ // Subscribe to Topics
+ StringTokenizer topics = new StringTokenizer(subscriptionTopics, ",");
+ while (topics.hasMoreTokens())
+ {
+ try
+ {
+ String topicName = topics.nextToken();
+ System.out.println ("Subscribing to: " + topicName);
+ new Subscription(session.createTopic(topicName));
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ }
+
+ // Set up the viewing area.
+ textArea.setEditable(false);
+ scrollPane.setBorder(new CompoundBorder(new EmptyBorder(6,6,6,6),
+ new SoftBevelBorder(BevelBorder.LOWERED)));
+ getContentPane().add(scrollPane,BorderLayout.CENTER);
+ getContentPane().add(clearButton,BorderLayout.SOUTH);
+ clearButton.addActionListener(new OnClear());
+ // Start the connection so that we can now receive messages.
+ try
+ {
+ connection.start();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ System.err.println("Cannot start connection");
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ }
+
+ /** Class to handle the subsciption to messages. */
+ public class Subscription implements javax.jms.MessageListener
+ {
+ public Subscription(Topic topic)
+ {
+ try
+ {
+ topicSubscriber = session.createConsumer(topic);
+ topicSubscriber.setMessageListener(this);
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ }
+
+ /**
+ * Handle the text message
+ * (as specified in the javax.jms.MessageListener interface).
+ */
+
+ public void onMessage(javax.jms.Message message)
+ {
+ String msgBody = null;
+ String msgClass = null;
+
+ if (message instanceof javax.jms.TextMessage)
+ {
+ msgClass = "javax.jms.TextMessage";
+ try
+ {
+ msgBody = ((javax.jms.TextMessage)message).getText();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ msgBody = "";
+ }
+ }
+ else
+ {
+ return;
+ }
+ try
+ {
+ textArea.append("\n");
+ textArea.append("-----------------------------------------------------------------------------------------------------\n");
+ // textArea.append("Class: " + msgClass + "\n");
+ textArea.append("The following message, received on topic " + ((Topic)message.getJMSDestination()).getTopicName() + ", was sent by\n");
+ //textArea.append("\n");
+ textArea.append(msgBody);
+
+ // Ensure Appended Text is Visible
+ Rectangle area = textArea.modelToView(textArea.getText().length());
+ if (area != null) textArea.scrollRectToVisible(area);
+
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ catch (BadLocationException ble)
+ {
+ ble.printStackTrace();
+ }
+
+ }
+
+ MessageConsumer topicSubscriber = null;
+ }
+
+ //
+ // NOTE: the remainder of this sample deals with reading arguments
+ // and does not utilize any JMS classes or code.
+ //
+
+ /** Main program entry point. */
+ public static void main(String[] args)
+ {
+ // There should be no arguments to this program.
+ if (args.length > 0) {
+ printUsage();
+ System.exit(1);
+ }
+
+ MessageMonitor messageMonitor = new MessageMonitor();
+
+ messageMonitor.addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent e)
+ {
+ System.exit(0);
+ }
+ });
+
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+ screenSize.height = screenSize.height / 2 ;
+ screenSize.width = screenSize.width / 2 ;
+ messageMonitor.setSize(screenSize);
+ messageMonitor.setVisible(true);
+
+ }
+
+ /** Prints the usage. */
+ private static void printUsage()
+ {
+ StringBuffer use = new StringBuffer();
+ use.append("\nusage: MessageMonitor\n\n");
+ use.append("Properties for this sample can be set in a properties file.\n");
+ String dfltFile = System.getProperty("propertiesFile", DEFAULT_PROPERTIES_FILE);
+ use.append("[Default file: " + dfltFile +"]\n\n");
+
+ System.out.print(use);
+ }
+
+ /** Load the window and JMS properties from a file. */
+ private void loadProperties()
+ {
+ try
+ {
+ Properties properties = new Properties();
+
+ propertiesFile = System.getProperty("propertiesFile", propertiesFile);
+
+ properties.load(new FileInputStream(propertiesFile));
+
+ // Connection Properties
+ brokerHostPort = properties.getProperty("brokerHostPort",brokerHostPort).trim();
+ connectID = properties.getProperty("connectID",connectID).trim();
+ userID = properties.getProperty("userID",userID).trim();
+ password = properties.getProperty("password",password).trim();
+
+ // Subscription Properties
+ subscriptionTopics = properties.getProperty("subscriptionTopics", subscriptionTopics).trim();
+
+ // Text Properties
+ textFontName = properties.getProperty("textFontName", textFontName).trim();
+ textFontStyle = properties.getProperty("textFontSize", textFontStyle).trim();
+ textFontSize = properties.getProperty("textFontSize", textFontSize).trim();
+
+ // Window Properties
+ title = properties.getProperty("title", title).trim();
+
+ }
+ catch (java.io.FileNotFoundException fnfe)
+ {
+ System.out.println (propertiesFile + " not found: using defaults"); // Use Defaults
+ }
+ catch (java.io.IOException ioe)
+ {
+ ioe.printStackTrace();
+ }
+ }
+
+ /** Class to handle the "Clear" button action. */
+ public class OnClear implements ActionListener
+ {
+ public void actionPerformed(ActionEvent evt)
+ {
+ textArea.setText("");
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/activemq/blob/3f32507f/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.properties
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.properties b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.properties
index b2ecb47..d74b92a 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.properties
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/MessageMonitor/MessageMonitor.properties
@@ -1,72 +1,72 @@
-## ---------------------------------------------------------------------------
-## Licensed to the Apache Software Foundation (ASF) under one or more
-## contributor license agreements. See the NOTICE file distributed with
-## this work for additional information regarding copyright ownership.
-## The ASF 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.
-## ---------------------------------------------------------------------------
-
-#
-# This is the properties file for the MessageMonitor Class.
-#
-# A MessageMonitor object will load this file when constructed.
-# By default, a MessageMonitor object searches for and load a
-# file named "MessageMonitor.properties" in the JVM's "current" directory.
-#
-# This behavior may be overriden by assigning an alternate property file
-# name and location to the System property "propertiesFile".
-#
-# Property: brokerHostPort
-# Default Value: localhost
-# Description: Specifies the host and port of the Messaging Broker to connect to.
-brokerHostPort localhost
-#
-# Property: connectID
-# Default Value: MessageMonitor
-# Description: Specifies the program identifier used to connect to the Messaging Broker.
-connectID MessageMonitor
-#
-# Property: userID
-# Default Value: MessageMonitor
-# Description: Specifies the user identifier used to connect to the Messaging Broker.
-userID Administrator
-#
-# Property: password
-# Default Value: MessageMonitor
-# Description: Specifies the password used to connect to the Messaging Broker.
-password Administrator
-#
-# Property: subscriptionTopics
-# Default Value: jms.samples.#
-# Description: A comma-separated list of topics that MessageMonitor subscribes to.
-# An empty list will subscribe to jms.samples.#.
-subscriptionTopics jms.samples.*
-##
-# Property: textFontName
-# Default Value: Dialog
-# Description: The name of the font used to display recieved messages.
-textFontName Dialog
-#
-# Property: textFontStyle
-# Default Value: PLAIN
-# Description: The style of the font used to display recieved messages.
-textFontStyle PLAIN
-#
-# Property: textFontSize
-# Default Value: 12
-# Description: The size of the font used to display recieved messages.
-textFontSize 12
-#
-# Property: title
-# Default Value: MessageMonitor
-# Description: The title of the MessageMonitor Window.
-title MessageMonitor
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements. See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF 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.
+## ---------------------------------------------------------------------------
+
+#
+# This is the properties file for the MessageMonitor Class.
+#
+# A MessageMonitor object will load this file when constructed.
+# By default, a MessageMonitor object searches for and load a
+# file named "MessageMonitor.properties" in the JVM's "current" directory.
+#
+# This behavior may be overriden by assigning an alternate property file
+# name and location to the System property "propertiesFile".
+#
+# Property: brokerHostPort
+# Default Value: localhost
+# Description: Specifies the host and port of the Messaging Broker to connect to.
+brokerHostPort localhost
+#
+# Property: connectID
+# Default Value: MessageMonitor
+# Description: Specifies the program identifier used to connect to the Messaging Broker.
+connectID MessageMonitor
+#
+# Property: userID
+# Default Value: MessageMonitor
+# Description: Specifies the user identifier used to connect to the Messaging Broker.
+userID Administrator
+#
+# Property: password
+# Default Value: MessageMonitor
+# Description: Specifies the password used to connect to the Messaging Broker.
+password Administrator
+#
+# Property: subscriptionTopics
+# Default Value: jms.samples.#
+# Description: A comma-separated list of topics that MessageMonitor subscribes to.
+# An empty list will subscribe to jms.samples.#.
+subscriptionTopics jms.samples.*
+##
+# Property: textFontName
+# Default Value: Dialog
+# Description: The name of the font used to display recieved messages.
+textFontName Dialog
+#
+# Property: textFontStyle
+# Default Value: PLAIN
+# Description: The style of the font used to display recieved messages.
+textFontStyle PLAIN
+#
+# Property: textFontSize
+# Default Value: 12
+# Description: The size of the font used to display recieved messages.
+textFontSize 12
+#
+# Property: title
+# Default Value: MessageMonitor
+# Description: The title of the MessageMonitor Window.
+title MessageMonitor