KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > topics > SelectorChat


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package sample.jms.topics.SelectorChat;
48 import org.mr.api.jms.MantaTopicConnectionFactory;
49
50
51 /*======================================================================================
52   For instructions on how to run this sample please refer to the file
53   sample\jms\topics\SelectorChat\Readme.txt under the MantaRay installation directory.
54 ======================================================================================*/

55
56
57 public class SelectorChat
58     implements javax.jms.MessageListener JavaDoc
59 {
60     private static final String JavaDoc PROPERTY_NAME = "Ticket";
61
62     private javax.jms.TopicConnection JavaDoc connect = null;
63     private javax.jms.TopicSession JavaDoc pubSession = null;
64     private javax.jms.TopicSession JavaDoc subSession = null;
65     private javax.jms.TopicPublisher JavaDoc publisher = null;
66
67     /** Create JMS client for publishing and subscribing to messages. */
68     private void chatter(String JavaDoc username, String JavaDoc chatTopic, String JavaDoc selection)
69     {
70
71         // Create a connection.
72
try
73         {
74             javax.jms.TopicConnectionFactory JavaDoc factory;
75             factory = new MantaTopicConnectionFactory();
76             connect = factory.createTopicConnection();
77             pubSession = connect.createTopicSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
78             subSession = connect.createTopicSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
79         }
80         catch (javax.jms.JMSException JavaDoc jmse)
81         {
82             jmse.printStackTrace();
83             System.exit(1);
84         }
85
86         // Create Publisher and Subscriber to 'chat' topics
87

88         try
89         {
90             javax.jms.Topic JavaDoc topic = pubSession.createTopic(chatTopic);
91             // NOTE: The subscriber's message selector will now be set:
92
javax.jms.TopicSubscriber JavaDoc subscriber = subSession.createSubscriber(topic, PROPERTY_NAME + " = \'" + selection +"\'", false);
93             subscriber.setMessageListener(this);
94             publisher = pubSession.createPublisher(topic);
95             // Now that setup is complete, start the Connection
96
connect.start();
97         }
98         catch (javax.jms.JMSException JavaDoc jmse)
99         {
100             jmse.printStackTrace();
101             System.exit(1);
102         }
103
104         try
105         {
106             // Read all standard input and send it as a message.
107

108             java.io.BufferedReader JavaDoc stdin =
109                 new java.io.BufferedReader JavaDoc( new java.io.InputStreamReader JavaDoc( System.in ) );
110             System.out.println ("\nWaiting for messages with property \"" + PROPERTY_NAME + " = \'" + selection + "\'\" ...\n" +
111                                 "\nEnter text messages to clients that subscribe to the " + chatTopic + " topic." +
112                                 "\nPublished messages have a \"" + PROPERTY_NAME + "\" property set to: \"" + selection + "\"." +
113                                 "\n\nPress Enter to publish each message.\n");
114             while ( true )
115             {
116                 String JavaDoc s = stdin.readLine();
117
118                 if ( s == null )
119                     exit();
120                 else if ( s.length() > 0 )
121                 {
122                     javax.jms.TextMessage JavaDoc msg = pubSession.createTextMessage();
123                     msg.setText(username + ": " + s);
124                     // NOTE: here we set a property on messages to be published:
125
msg.setStringProperty(PROPERTY_NAME, selection);
126                     publisher.publish(msg);
127                 }
128             }
129         }
130         catch ( java.io.IOException JavaDoc ioe )
131         {
132             ioe.printStackTrace();
133         }
134         catch ( javax.jms.JMSException JavaDoc jmse )
135         {
136             jmse.printStackTrace();
137         }
138     }
139
140     /**
141      * Handle the message
142      * (as specified in the javax.jms.MessageListener interface).
143      */

144     public void onMessage( javax.jms.Message JavaDoc aMessage)
145     {
146
147         try
148         {
149             // Cast the message as a text message.
150
javax.jms.TextMessage JavaDoc textMessage = (javax.jms.TextMessage JavaDoc) aMessage;
151
152             // This handler reads a single String from the
153
// message and prints it to the standard output.
154
try
155             {
156                 String JavaDoc string = textMessage.getText();
157                 System.out.println( string );
158             }
159             catch (javax.jms.JMSException JavaDoc jmse)
160             {
161                 jmse.printStackTrace();
162             }
163         }
164         catch (java.lang.RuntimeException JavaDoc rte)
165         {
166             rte.printStackTrace();
167         }
168     }
169
170     /** Cleanup resources and then exit. */
171     private void exit()
172     {
173         try
174         {
175             connect.close();
176         }
177         catch (javax.jms.JMSException JavaDoc jmse)
178         {
179             jmse.printStackTrace();
180         }
181
182         System.exit(0);
183     }
184
185     //
186
// NOTE: the remainder of this sample deals with reading arguments
187
// and does not utilize any JMS classes or code.
188
//
189

190     /** Main program entry point. */
191     public static void main(String JavaDoc argv[]) {
192
193         // Is there anything to do?
194
if (argv.length == 0) {
195             printUsage();
196             System.exit(1);
197         }
198
199         // Values to be read from parameters
200
String JavaDoc username = null;
201         String JavaDoc chatTopic = null;
202         String JavaDoc selection = null;
203
204         // Check parameters
205
for (int i = 0; i < argv.length; i++) {
206             String JavaDoc arg = argv[i];
207
208             // Options
209
if (!arg.startsWith("-")) {
210                 System.err.println ("error: unexpected argument - "+arg);
211                 printUsage();
212                 System.exit(1);
213             }
214             else {
215                 if (arg.equals("-u")) {
216                     if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
217                         System.err.println("error: missing user name");
218                         System.exit(1);
219                     }
220                     username = argv[++i];
221                     continue;
222                 }
223
224                 if (arg.equals("-s")) {
225                     if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
226                         System.err.println("error: missing password");
227                         System.exit(1);
228                     }
229                     selection = argv[++i];
230                     continue;
231                 }
232
233                 if (arg.equals("-t")) {
234                     if (i == argv.length - 1 || argv[i+1].startsWith("-")) {
235                         System.err.println("error: missing topic");
236                         System.exit(1);
237                     }
238                     chatTopic = argv[++i];
239                     continue;
240                 }
241
242                 if (arg.equals("-h")) {
243                     printUsage();
244                     System.exit(1);
245                 }
246             }
247         }
248
249         // Check values read in.
250
if (username == null) {
251             System.err.println ("error: user name must be supplied");
252             printUsage();
253             System.exit(1);
254         }
255
256         if (selection == null) {
257             System.err.println ("error: selection must be supplied");
258             printUsage();
259             System.exit(1);
260         }
261
262         if (chatTopic == null) {
263             System.err.println ("error: topic must be supplied");
264             printUsage();
265             System.exit(1);
266         }
267
268         // Start the JMS client for the "chat".
269
SelectorChat chat = new SelectorChat();
270         chat.chatter(username, chatTopic, selection);
271     }
272
273     /** Prints the usage. */
274     private static void printUsage() {
275
276         StringBuffer JavaDoc use = new StringBuffer JavaDoc();
277         use.append("usage: java SelectorChat (options) ...\n\n");
278         use.append("options:\n");
279         use.append(" -u name Specify unique user name. (Required)\n");
280         use.append(" -s selection Message selector value. (Required)\n");
281         use.append(" -t topic Chat topic. (Required)\n");
282         use.append(" -h This help screen\n");
283         System.err.println (use);
284     }
285 }
286
287
Popular Tags