1 46 47 package sample.jms.topics.SelectorChat; 48 import org.mr.api.jms.MantaTopicConnectionFactory; 49 50 51 55 56 57 public class SelectorChat 58 implements javax.jms.MessageListener 59 { 60 private static final String PROPERTY_NAME = "Ticket"; 61 62 private javax.jms.TopicConnection connect = null; 63 private javax.jms.TopicSession pubSession = null; 64 private javax.jms.TopicSession subSession = null; 65 private javax.jms.TopicPublisher publisher = null; 66 67 68 private void chatter(String username, String chatTopic, String selection) 69 { 70 71 try 73 { 74 javax.jms.TopicConnectionFactory 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 jmse) 81 { 82 jmse.printStackTrace(); 83 System.exit(1); 84 } 85 86 88 try 89 { 90 javax.jms.Topic topic = pubSession.createTopic(chatTopic); 91 javax.jms.TopicSubscriber subscriber = subSession.createSubscriber(topic, PROPERTY_NAME + " = \'" + selection +"\'", false); 93 subscriber.setMessageListener(this); 94 publisher = pubSession.createPublisher(topic); 95 connect.start(); 97 } 98 catch (javax.jms.JMSException jmse) 99 { 100 jmse.printStackTrace(); 101 System.exit(1); 102 } 103 104 try 105 { 106 108 java.io.BufferedReader stdin = 109 new java.io.BufferedReader ( new java.io.InputStreamReader ( 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 s = stdin.readLine(); 117 118 if ( s == null ) 119 exit(); 120 else if ( s.length() > 0 ) 121 { 122 javax.jms.TextMessage msg = pubSession.createTextMessage(); 123 msg.setText(username + ": " + s); 124 msg.setStringProperty(PROPERTY_NAME, selection); 126 publisher.publish(msg); 127 } 128 } 129 } 130 catch ( java.io.IOException ioe ) 131 { 132 ioe.printStackTrace(); 133 } 134 catch ( javax.jms.JMSException jmse ) 135 { 136 jmse.printStackTrace(); 137 } 138 } 139 140 144 public void onMessage( javax.jms.Message aMessage) 145 { 146 147 try 148 { 149 javax.jms.TextMessage textMessage = (javax.jms.TextMessage ) aMessage; 151 152 try 155 { 156 String string = textMessage.getText(); 157 System.out.println( string ); 158 } 159 catch (javax.jms.JMSException jmse) 160 { 161 jmse.printStackTrace(); 162 } 163 } 164 catch (java.lang.RuntimeException rte) 165 { 166 rte.printStackTrace(); 167 } 168 } 169 170 171 private void exit() 172 { 173 try 174 { 175 connect.close(); 176 } 177 catch (javax.jms.JMSException jmse) 178 { 179 jmse.printStackTrace(); 180 } 181 182 System.exit(0); 183 } 184 185 190 191 public static void main(String argv[]) { 192 193 if (argv.length == 0) { 195 printUsage(); 196 System.exit(1); 197 } 198 199 String username = null; 201 String chatTopic = null; 202 String selection = null; 203 204 for (int i = 0; i < argv.length; i++) { 206 String arg = argv[i]; 207 208 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 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 SelectorChat chat = new SelectorChat(); 270 chat.chatter(username, chatTopic, selection); 271 } 272 273 274 private static void printUsage() { 275 276 StringBuffer use = new StringBuffer (); 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 |