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:14 UTC
[02/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/RequestReply/TopicReplier.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicReplier.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicReplier.java
index a786afe..ac474b4 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicReplier.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicReplier.java
@@ -1,318 +1,318 @@
-/*
- * � 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:
- - Synchronous Request/Reply
- - Publish/Subscribe
- - javax.jms.TopicRequestor class
- - JMSReplyTo Header
-
-When this program runs, it waits for
-messages to the topic "jms.samples.request".
-When that message occurs, a response based on the request
-is sent back to the "Requestor" specified in the JMSReplyTo header.
-
-This sample replies with a simple text manipulation of the request;
-the text is either folded to all UPPERCASE or all lowercase.
-
-Usage:
- java TopicReplier -b <broker:port> -u <username> -p <password> -m <mode>
- -b broker:port points to your message broker
- Default: tcp://localhost:61616
- -u username must be unique (but is not checked)
- Default: SampleReplier
- -p password password for user (not checked)
- Default: password
- -m mode replier mode (uppercase, or lowercase)
- Default: uppercase
-
-Suggested usage:
- - In a console window with the environment set, start a replier:
- java TopicReplier -u SampleReplier
- - In another console window, start a Requestor:
- java TopicRequestor -u SampleRequestor
- - Enter text in the Requestor window then press Enter.
-
- The Replier responds with the message in all uppercase characters.
- - Start other TopicRequestors with different user names to see that
- replies are not broadcast to all users. For example:
- java TopicRequestor -u SampleRequestorToo
-
- - Start other TopicRepliers.
- - See that all repliers are receiving all the messages,(as they should).
- - See the Requestor only receives one response.
- java TopicReplier -u toLower -m lowercase
-
- */
-
-import org.apache.activemq.*;
-
-
-public class TopicReplier
- implements javax.jms.MessageListener
-{
- private static final String APP_TOPIC = "jms.samples.request";
- private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
- private static final String DEFAULT_USER_NAME = "SampleReplier";
- private static final String DEFAULT_PASSWORD = "password";
- private static final String DEFAULT_MODE = "uppercase";
- private static final int UPPERCASE = 0;
- private static final int LOWERCASE = 1;
-
- private javax.jms.Connection connect = null;
- private javax.jms.Session session = null;
- private javax.jms.MessageProducer replier = null;
-
- private int imode = UPPERCASE;
-
- /** Create JMS client for publishing and subscribing to messages. */
- private void start ( String broker, String username, String password, String mode)
- {
- // Set the operation mode
- imode = (mode.equals("uppercase")) ? UPPERCASE : LOWERCASE;
-
- // Create a connection.
- try
- {
- javax.jms.ConnectionFactory factory;
- factory = new ActiveMQConnectionFactory(username, password, broker);
- connect = factory.createConnection (username, password);
- session = connect.createSession(true, 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 Subscriber to application topics as well as a Publisher
- // to use for JMS replies.
- try
- {
- javax.jms.Topic topic = session.createTopic (APP_TOPIC);
- javax.jms.MessageConsumer subscriber = session.createConsumer(topic);
- subscriber.setMessageListener(this);
- replier = session.createProducer(null); // Topic will be set for each reply
- // Now that all 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 ) );
- while ( true )
- {
- System.out.println ("\nReplier application:\n"
- + "============================\n"
- + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
- + "The application gets requests with JMSReplyTo set on the " + APP_TOPIC + " topic."
- + "The message is transformed to all uppercase or all lowercase, and then returned to the requestor."
- + "The Requestor application displays the result.\n\n"
- + "Enter EXIT or press Ctrl+C to close the Replier.\n");
- String s = stdin.readLine();
- if ( s == null || s.equalsIgnoreCase("EXIT"))
- {
- System.out.println ("\nStopping Replier. Please wait..\n>");
- exit();
- }
- }
- }
- catch ( java.io.IOException ioe )
- {
- ioe.printStackTrace();
- }
- }
-
- /**
- * Handle the message.
- * (as specified in the javax.jms.MessageListener interface).
- *
- * IMPORTANT NOTE: We must follow the design paradigm for JMS
- * synchronous requests. That is, we must:
- * - get the message
- * - look for the header specifying JMSReplyTo
- * - send a reply to the topic specified there.
- * Failing to follow these steps might leave the originator
- * of the request waiting forever.
- *
- * OPTIONAL BEHAVIOR: The following actions taken by the
- * message handler represent good programming style, but are
- * not required by the design paradigm for JMS requests.
- * - set the JMSCorrelationID (tying the response back to
- * the original request.
- * - use transacted session "commit" so receipt of request
- * won't happen without the reply being sent.
- *
- */
- 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( "[Request] " + string );
-
- // Check for a ReplyTo topic
- javax.jms.Topic replyTopic = (javax.jms.Topic) aMessage.getJMSReplyTo();
- if (replyTopic != null)
- {
- // Send the modified message back.
- javax.jms.TextMessage reply = session.createTextMessage();
- if (imode == UPPERCASE)
- reply.setText("Transformed " + string + " to all uppercase: " + string.toUpperCase());
- else
- reply.setText("Transformed " + string + " to all lowercase " + string.toLowerCase());
- reply.setJMSCorrelationID(aMessage.getJMSMessageID());
- replier.send(replyTopic, reply);
- session.commit();
- }
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- }
- }
- catch (java.lang.RuntimeException rte)
- {
- rte.printStackTrace();
- }
- }
-
- /** Cleanup resources cleanly and 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[]) {
-
- // Values to be read from parameters
- String broker = DEFAULT_BROKER_NAME;
- String username = DEFAULT_USER_NAME;
- String password = DEFAULT_PASSWORD;
- String mode = DEFAULT_MODE;
-
- // 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("-m")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing mode");
- System.exit(1);
- }
- mode = argv[++i];
- if (!(mode.equals("uppercase") || mode.equals("lowercase"))) {
- System.err.println("error: mode must be 'uppercase' or 'lowercase'");
- System.exit(1);
- }
- continue;
- }
-
- if (arg.equals("-h")) {
- printUsage();
- System.exit(1);
- }
-
- // Invalid argument
- System.err.println ("error: unexpected argument: "+arg);
- printUsage();
- System.exit(1);
- }
-
-
- // Start the JMS client for the "chat".
- TopicReplier replier = new TopicReplier();
- replier.start (broker, username, password, mode);
- }
-
- /** Prints the usage. */
- private static void printUsage() {
-
- StringBuffer use = new StringBuffer();
- use.append("usage: java Replier (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.\n");
- use.append(" Default broker: "+DEFAULT_USER_NAME+"\n");
- use.append(" -p password Specify password for user.\n");
- use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
- use.append(" -m mode Replier operating mode - uppercase or lowercase.\n");
- use.append(" Default mode: "+DEFAULT_MODE+"\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 Basic JMS Application using:
+ - Synchronous Request/Reply
+ - Publish/Subscribe
+ - javax.jms.TopicRequestor class
+ - JMSReplyTo Header
+
+When this program runs, it waits for
+messages to the topic "jms.samples.request".
+When that message occurs, a response based on the request
+is sent back to the "Requestor" specified in the JMSReplyTo header.
+
+This sample replies with a simple text manipulation of the request;
+the text is either folded to all UPPERCASE or all lowercase.
+
+Usage:
+ java TopicReplier -b <broker:port> -u <username> -p <password> -m <mode>
+ -b broker:port points to your message broker
+ Default: tcp://localhost:61616
+ -u username must be unique (but is not checked)
+ Default: SampleReplier
+ -p password password for user (not checked)
+ Default: password
+ -m mode replier mode (uppercase, or lowercase)
+ Default: uppercase
+
+Suggested usage:
+ - In a console window with the environment set, start a replier:
+ java TopicReplier -u SampleReplier
+ - In another console window, start a Requestor:
+ java TopicRequestor -u SampleRequestor
+ - Enter text in the Requestor window then press Enter.
+
+ The Replier responds with the message in all uppercase characters.
+ - Start other TopicRequestors with different user names to see that
+ replies are not broadcast to all users. For example:
+ java TopicRequestor -u SampleRequestorToo
+
+ - Start other TopicRepliers.
+ - See that all repliers are receiving all the messages,(as they should).
+ - See the Requestor only receives one response.
+ java TopicReplier -u toLower -m lowercase
+
+ */
+
+import org.apache.activemq.*;
+
+
+public class TopicReplier
+ implements javax.jms.MessageListener
+{
+ private static final String APP_TOPIC = "jms.samples.request";
+ private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
+ private static final String DEFAULT_USER_NAME = "SampleReplier";
+ private static final String DEFAULT_PASSWORD = "password";
+ private static final String DEFAULT_MODE = "uppercase";
+ private static final int UPPERCASE = 0;
+ private static final int LOWERCASE = 1;
+
+ private javax.jms.Connection connect = null;
+ private javax.jms.Session session = null;
+ private javax.jms.MessageProducer replier = null;
+
+ private int imode = UPPERCASE;
+
+ /** Create JMS client for publishing and subscribing to messages. */
+ private void start ( String broker, String username, String password, String mode)
+ {
+ // Set the operation mode
+ imode = (mode.equals("uppercase")) ? UPPERCASE : LOWERCASE;
+
+ // Create a connection.
+ try
+ {
+ javax.jms.ConnectionFactory factory;
+ factory = new ActiveMQConnectionFactory(username, password, broker);
+ connect = factory.createConnection (username, password);
+ session = connect.createSession(true, 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 Subscriber to application topics as well as a Publisher
+ // to use for JMS replies.
+ try
+ {
+ javax.jms.Topic topic = session.createTopic (APP_TOPIC);
+ javax.jms.MessageConsumer subscriber = session.createConsumer(topic);
+ subscriber.setMessageListener(this);
+ replier = session.createProducer(null); // Topic will be set for each reply
+ // Now that all 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 ) );
+ while ( true )
+ {
+ System.out.println ("\nReplier application:\n"
+ + "============================\n"
+ + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
+ + "The application gets requests with JMSReplyTo set on the " + APP_TOPIC + " topic."
+ + "The message is transformed to all uppercase or all lowercase, and then returned to the requestor."
+ + "The Requestor application displays the result.\n\n"
+ + "Enter EXIT or press Ctrl+C to close the Replier.\n");
+ String s = stdin.readLine();
+ if ( s == null || s.equalsIgnoreCase("EXIT"))
+ {
+ System.out.println ("\nStopping Replier. Please wait..\n>");
+ exit();
+ }
+ }
+ }
+ catch ( java.io.IOException ioe )
+ {
+ ioe.printStackTrace();
+ }
+ }
+
+ /**
+ * Handle the message.
+ * (as specified in the javax.jms.MessageListener interface).
+ *
+ * IMPORTANT NOTE: We must follow the design paradigm for JMS
+ * synchronous requests. That is, we must:
+ * - get the message
+ * - look for the header specifying JMSReplyTo
+ * - send a reply to the topic specified there.
+ * Failing to follow these steps might leave the originator
+ * of the request waiting forever.
+ *
+ * OPTIONAL BEHAVIOR: The following actions taken by the
+ * message handler represent good programming style, but are
+ * not required by the design paradigm for JMS requests.
+ * - set the JMSCorrelationID (tying the response back to
+ * the original request.
+ * - use transacted session "commit" so receipt of request
+ * won't happen without the reply being sent.
+ *
+ */
+ 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( "[Request] " + string );
+
+ // Check for a ReplyTo topic
+ javax.jms.Topic replyTopic = (javax.jms.Topic) aMessage.getJMSReplyTo();
+ if (replyTopic != null)
+ {
+ // Send the modified message back.
+ javax.jms.TextMessage reply = session.createTextMessage();
+ if (imode == UPPERCASE)
+ reply.setText("Transformed " + string + " to all uppercase: " + string.toUpperCase());
+ else
+ reply.setText("Transformed " + string + " to all lowercase " + string.toLowerCase());
+ reply.setJMSCorrelationID(aMessage.getJMSMessageID());
+ replier.send(replyTopic, reply);
+ session.commit();
+ }
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ }
+ }
+ catch (java.lang.RuntimeException rte)
+ {
+ rte.printStackTrace();
+ }
+ }
+
+ /** Cleanup resources cleanly and 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[]) {
+
+ // Values to be read from parameters
+ String broker = DEFAULT_BROKER_NAME;
+ String username = DEFAULT_USER_NAME;
+ String password = DEFAULT_PASSWORD;
+ String mode = DEFAULT_MODE;
+
+ // 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("-m")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing mode");
+ System.exit(1);
+ }
+ mode = argv[++i];
+ if (!(mode.equals("uppercase") || mode.equals("lowercase"))) {
+ System.err.println("error: mode must be 'uppercase' or 'lowercase'");
+ System.exit(1);
+ }
+ continue;
+ }
+
+ if (arg.equals("-h")) {
+ printUsage();
+ System.exit(1);
+ }
+
+ // Invalid argument
+ System.err.println ("error: unexpected argument: "+arg);
+ printUsage();
+ System.exit(1);
+ }
+
+
+ // Start the JMS client for the "chat".
+ TopicReplier replier = new TopicReplier();
+ replier.start (broker, username, password, mode);
+ }
+
+ /** Prints the usage. */
+ private static void printUsage() {
+
+ StringBuffer use = new StringBuffer();
+ use.append("usage: java Replier (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.\n");
+ use.append(" Default broker: "+DEFAULT_USER_NAME+"\n");
+ use.append(" -p password Specify password for user.\n");
+ use.append(" Default password: "+DEFAULT_PASSWORD+"\n");
+ use.append(" -m mode Replier operating mode - uppercase or lowercase.\n");
+ use.append(" Default mode: "+DEFAULT_MODE+"\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/RequestReply/TopicRequestor.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicRequestor.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicRequestor.java
index 4c48fab..1c59c65 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicRequestor.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/RequestReply/TopicRequestor.java
@@ -1,241 +1,241 @@
-/*
- * � 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:
- - Synchronous Request/Reply
- - Publish/Subscribe
- - javax.jms.TopicRequestor class
- - JMSReplyTo Header
-
-When this program runs, it reads input from System.in
-and then sends the text as a message to the topic
-"progress.samples.request".
-
-A "Replier" class should be waiting for the request.
-It will reply with a message.
-
-NOTE: You must run the TopicReplier first.
-(Otherwise the syncronous request will block forever.)
-
-Usage:
- java TopicRequestor -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)
- Default: SampleRequestor
- -p password password for user (not checked)
- Default: password
-
-Suggested demonstration:
- - In a console window with the environment set,
- start a copy of the Replier. For example:
- java TopicReplier -u SampleReplier
- - In another console window, start a Requestor.
- For example:
- java TopicRequestor -u SampleRequestor
- - Enter text in the Requestor window then press Enter.
-
- The Replier responds with the message in all uppercase characters.
- - Start other Requestors with different user names to see that
- replies are not broadcast to all users. For example:
- java TopicRequestor -u SampleRequestorToo
-
- - Start other Repliers.
- - See that all repliers are receiving all the messages,(as they should).
- - See the Requestor only receives one response.
- java TopicReplier -u toLower -m lowercase
-*/
-import org.apache.activemq.*;
-
-
-public class TopicRequestor
-{
- private static final String APP_TOPIC = "jms.samples.request";
- private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
- private static final String DEFAULT_USER_NAME = "SampleRequestor";
- private static final String DEFAULT_PASSWORD = "password";
-
- private javax.jms.TopicConnection connect = null;
- private javax.jms.TopicSession session = null;
-
- /** Create JMS client for publishing and subscribing to messages. */
- private void start ( String broker, String username, String password)
- {
- // Create a connection.
- try
- {
- javax.jms.TopicConnectionFactory factory;
- factory = new ActiveMQConnectionFactory(username, password, broker);
- connect = factory.createTopicConnection (username, password);
- session = connect.createTopicSession(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 Topic for all requests. TopicRequestor will be created
- // as needed.
- javax.jms.Topic topic = null;
- try
- {
- topic = session.createTopic (APP_TOPIC);
- // Now that all 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 ("\nRequestor application:\n"
- + "============================\n"
- + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
- + "The application uses a TopicRequestor to on the " + APP_TOPIC + " topic."
- + "The Replier application gets the message, and transforms it."
- + "The Requestor application displays the result.\n\n"
- + "Type some mixed case text, and then press Enter to make a request.\n");
- while ( true )
- {
- String s = stdin.readLine();
-
- if ( s == null )
- exit();
- else if ( s.length() > 0 )
- {
- javax.jms.TextMessage msg = session.createTextMessage();
- msg.setText( username + ": " + s );
- // Instead of publishing, we will use a TopicRequestor.
- javax.jms.TopicRequestor requestor = new javax.jms.TopicRequestor(session, topic);
- javax.jms.Message response = requestor.request(msg);
- // The message should be a TextMessage. Just report it.
- javax.jms.TextMessage textMessage = (javax.jms.TextMessage) response;
- System.out.println( "[Reply] " + textMessage.getText() );
- }
- }
- }
- catch ( java.io.IOException ioe )
- {
- ioe.printStackTrace();
- }
- catch ( javax.jms.JMSException jmse )
- {
- jmse.printStackTrace();
- }
- }
-
- /** Cleanup resources cleanly and 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[]) {
-
- // Values to be read from parameters
- String broker = DEFAULT_BROKER_NAME;
- String username = DEFAULT_USER_NAME;
- 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);
- }
-
- // Start the JMS client for the "chat".
- TopicRequestor requestor = new TopicRequestor();
- requestor.start (broker, username, password);
-
- }
-
- /** Prints the usage. */
- private static void printUsage() {
-
- StringBuffer use = new StringBuffer();
- use.append("usage: java Requestor (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.\n");
- use.append(" Default broker: "+DEFAULT_USER_NAME+"\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);
- }
-
-}
-
+/*
+ * � 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:
+ - Synchronous Request/Reply
+ - Publish/Subscribe
+ - javax.jms.TopicRequestor class
+ - JMSReplyTo Header
+
+When this program runs, it reads input from System.in
+and then sends the text as a message to the topic
+"progress.samples.request".
+
+A "Replier" class should be waiting for the request.
+It will reply with a message.
+
+NOTE: You must run the TopicReplier first.
+(Otherwise the syncronous request will block forever.)
+
+Usage:
+ java TopicRequestor -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)
+ Default: SampleRequestor
+ -p password password for user (not checked)
+ Default: password
+
+Suggested demonstration:
+ - In a console window with the environment set,
+ start a copy of the Replier. For example:
+ java TopicReplier -u SampleReplier
+ - In another console window, start a Requestor.
+ For example:
+ java TopicRequestor -u SampleRequestor
+ - Enter text in the Requestor window then press Enter.
+
+ The Replier responds with the message in all uppercase characters.
+ - Start other Requestors with different user names to see that
+ replies are not broadcast to all users. For example:
+ java TopicRequestor -u SampleRequestorToo
+
+ - Start other Repliers.
+ - See that all repliers are receiving all the messages,(as they should).
+ - See the Requestor only receives one response.
+ java TopicReplier -u toLower -m lowercase
+*/
+import org.apache.activemq.*;
+
+
+public class TopicRequestor
+{
+ private static final String APP_TOPIC = "jms.samples.request";
+ private static final String DEFAULT_BROKER_NAME = "tcp://localhost:61616";
+ private static final String DEFAULT_USER_NAME = "SampleRequestor";
+ private static final String DEFAULT_PASSWORD = "password";
+
+ private javax.jms.TopicConnection connect = null;
+ private javax.jms.TopicSession session = null;
+
+ /** Create JMS client for publishing and subscribing to messages. */
+ private void start ( String broker, String username, String password)
+ {
+ // Create a connection.
+ try
+ {
+ javax.jms.TopicConnectionFactory factory;
+ factory = new ActiveMQConnectionFactory(username, password, broker);
+ connect = factory.createTopicConnection (username, password);
+ session = connect.createTopicSession(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 Topic for all requests. TopicRequestor will be created
+ // as needed.
+ javax.jms.Topic topic = null;
+ try
+ {
+ topic = session.createTopic (APP_TOPIC);
+ // Now that all 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 ("\nRequestor application:\n"
+ + "============================\n"
+ + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
+ + "The application uses a TopicRequestor to on the " + APP_TOPIC + " topic."
+ + "The Replier application gets the message, and transforms it."
+ + "The Requestor application displays the result.\n\n"
+ + "Type some mixed case text, and then press Enter to make a request.\n");
+ while ( true )
+ {
+ String s = stdin.readLine();
+
+ if ( s == null )
+ exit();
+ else if ( s.length() > 0 )
+ {
+ javax.jms.TextMessage msg = session.createTextMessage();
+ msg.setText( username + ": " + s );
+ // Instead of publishing, we will use a TopicRequestor.
+ javax.jms.TopicRequestor requestor = new javax.jms.TopicRequestor(session, topic);
+ javax.jms.Message response = requestor.request(msg);
+ // The message should be a TextMessage. Just report it.
+ javax.jms.TextMessage textMessage = (javax.jms.TextMessage) response;
+ System.out.println( "[Reply] " + textMessage.getText() );
+ }
+ }
+ }
+ catch ( java.io.IOException ioe )
+ {
+ ioe.printStackTrace();
+ }
+ catch ( javax.jms.JMSException jmse )
+ {
+ jmse.printStackTrace();
+ }
+ }
+
+ /** Cleanup resources cleanly and 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[]) {
+
+ // Values to be read from parameters
+ String broker = DEFAULT_BROKER_NAME;
+ String username = DEFAULT_USER_NAME;
+ 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);
+ }
+
+ // Start the JMS client for the "chat".
+ TopicRequestor requestor = new TopicRequestor();
+ requestor.start (broker, username, password);
+
+ }
+
+ /** Prints the usage. */
+ private static void printUsage() {
+
+ StringBuffer use = new StringBuffer();
+ use.append("usage: java Requestor (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.\n");
+ use.append(" Default broker: "+DEFAULT_USER_NAME+"\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/SelectorChat/SelectorChat.java
----------------------------------------------------------------------
diff --git a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/SelectorChat/SelectorChat.java b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/SelectorChat/SelectorChat.java
index 64c9a77..ca5cfbd 100644
--- a/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/SelectorChat/SelectorChat.java
+++ b/assembly/src/release/examples/openwire/exploring-jms/TopicPubSubSamples/SelectorChat/SelectorChat.java
@@ -1,296 +1,296 @@
-/*
- * � 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 Publish and Subscribe using
-Message Selectors
-
-This sample publishes messages and sets a property in the message header when
-publishing messages. The subscriber set a message selector to select these
-messages. In this sample we name the property "Department"
-
-Usage:
- java SelectorChat -b <broker:port> -u <username> -p <password> -v <property> -s <selector>
- -b broker:port points to your message broker
- Default: tcp://localhost:61616
- -u username must be unique (but is not checked)
- -p password (optional) password for user (not checked)
- Default: password
- -s selection required, selection value to set to message property
-
-
-Suggested demonstration:
- - In separate console windows with the environment set,
- start instances of the application
- under unique user names.
- For example:
- java SelectorChat -u BOB -s MARKETING
- java SelectorChat -u JANE -s MARKETING
- - 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.
- - Try starting a new instance with a different message selector
- - Stop a session by pressing CTRL+C in its console window.
-
-*/
-
-import org.apache.activemq.*;
-
-
-public class SelectorChat
- implements javax.jms.MessageListener
-{
- private static final String APP_TOPIC = "jms.samples.chat";
- private static final String PROPERTY_NAME = "Department";
- 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 selection)
- {
-
- // 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
-
- try
- {
- javax.jms.Topic topic = pubSession.createTopic (APP_TOPIC);
- // NOTE: The subscriber's message selector will now be set:
- javax.jms.MessageConsumer subscriber = subSession.createConsumer(topic, PROPERTY_NAME + " = \'" + selection +"\'", false);
- subscriber.setMessageListener(this);
- publisher = pubSession.createProducer(topic);
- // Now that setup is complete, start the Connection
- connect.start();
- }
- catch (javax.jms.JMSException jmse)
- {
- jmse.printStackTrace();
- System.exit(1);
- }
-
- 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 ("\nSelectorChat application:\n"
- + "===========================\n"
- + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
- + "The application will publish messages with " + PROPERTY_NAME + " set to " + selection + " to the " + APP_TOPIC + " topic .\n"
- + "The application also subscribes to that topic, selecting only messages where " + PROPERTY_NAME + " is " + selection + ".\n"
- + "Type some text, and then press Enter to publish it as 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 );
- // NOTE: here we set a property on messages to be published:
- msg.setStringProperty(PROPERTY_NAME, selection);
- 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 selection = null;
-
- // 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("-s")) {
- if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
- System.err.println("error: missing selection");
- System.exit(1);
- }
- selection = 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();
- }
-
- if (selection == null) {
- System.err.println ("error: selection must be supplied");
- printUsage();
- System.exit(1);
- }
-
- // Start the JMS client for the "chat".
- SelectorChat chat = new SelectorChat();
- chat.chatter (broker, username, password, selection);
-
- }
-
- /** Prints the usage. */
- private static void printUsage() {
-
- StringBuffer use = new StringBuffer();
- use.append("usage: java SelectorChat (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(" -s selection Message selector value. (Required)\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 Basic JMS Application using Publish and Subscribe using
+Message Selectors
+
+This sample publishes messages and sets a property in the message header when
+publishing messages. The subscriber set a message selector to select these
+messages. In this sample we name the property "Department"
+
+Usage:
+ java SelectorChat -b <broker:port> -u <username> -p <password> -v <property> -s <selector>
+ -b broker:port points to your message broker
+ Default: tcp://localhost:61616
+ -u username must be unique (but is not checked)
+ -p password (optional) password for user (not checked)
+ Default: password
+ -s selection required, selection value to set to message property
+
+
+Suggested demonstration:
+ - In separate console windows with the environment set,
+ start instances of the application
+ under unique user names.
+ For example:
+ java SelectorChat -u BOB -s MARKETING
+ java SelectorChat -u JANE -s MARKETING
+ - 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.
+ - Try starting a new instance with a different message selector
+ - Stop a session by pressing CTRL+C in its console window.
+
+*/
+
+import org.apache.activemq.*;
+
+
+public class SelectorChat
+ implements javax.jms.MessageListener
+{
+ private static final String APP_TOPIC = "jms.samples.chat";
+ private static final String PROPERTY_NAME = "Department";
+ 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 selection)
+ {
+
+ // 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
+
+ try
+ {
+ javax.jms.Topic topic = pubSession.createTopic (APP_TOPIC);
+ // NOTE: The subscriber's message selector will now be set:
+ javax.jms.MessageConsumer subscriber = subSession.createConsumer(topic, PROPERTY_NAME + " = \'" + selection +"\'", false);
+ subscriber.setMessageListener(this);
+ publisher = pubSession.createProducer(topic);
+ // Now that setup is complete, start the Connection
+ connect.start();
+ }
+ catch (javax.jms.JMSException jmse)
+ {
+ jmse.printStackTrace();
+ System.exit(1);
+ }
+
+ 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 ("\nSelectorChat application:\n"
+ + "===========================\n"
+ + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
+ + "The application will publish messages with " + PROPERTY_NAME + " set to " + selection + " to the " + APP_TOPIC + " topic .\n"
+ + "The application also subscribes to that topic, selecting only messages where " + PROPERTY_NAME + " is " + selection + ".\n"
+ + "Type some text, and then press Enter to publish it as 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 );
+ // NOTE: here we set a property on messages to be published:
+ msg.setStringProperty(PROPERTY_NAME, selection);
+ 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 selection = null;
+
+ // 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("-s")) {
+ if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
+ System.err.println("error: missing selection");
+ System.exit(1);
+ }
+ selection = 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();
+ }
+
+ if (selection == null) {
+ System.err.println ("error: selection must be supplied");
+ printUsage();
+ System.exit(1);
+ }
+
+ // Start the JMS client for the "chat".
+ SelectorChat chat = new SelectorChat();
+ chat.chatter (broker, username, password, selection);
+
+ }
+
+ /** Prints the usage. */
+ private static void printUsage() {
+
+ StringBuffer use = new StringBuffer();
+ use.append("usage: java SelectorChat (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(" -s selection Message selector value. (Required)\n");
+ use.append(" -h This help screen\n");
+ System.err.println (use);
+ }
+
+}
+