1 package sample.jms.topics.Chat; 2 3 48 49 50 54 55 56 import java.io.InputStreamReader ; 57 58 import javax.jms.Session ; 59 import javax.jms.TopicConnectionFactory ; 60 61 import org.mr.api.jms.MantaTopicConnectionFactory; 62 63 64 public class Chat 65 implements javax.jms.MessageListener 66 { 67 private static final int MESSAGE_TTL = 6000000; 68 69 static int counter = 0; 70 private javax.jms.TopicConnection connect = null; 71 private javax.jms.TopicSession pubSession = null; 72 private javax.jms.TopicSession subSession = null; 73 private javax.jms.TopicPublisher publisher = null; 74 75 76 private void chatter(String username, String chatTopic) { 77 try { 79 TopicConnectionFactory factory; 80 factory = new MantaTopicConnectionFactory(); 81 connect = factory.createTopicConnection(); 82 pubSession = 83 connect.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 84 subSession = 85 connect.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 86 } catch (javax.jms.JMSException jmse) { 87 System.err.println("error while creating connection - " + 88 jmse.toString()); 89 jmse.printStackTrace(); 90 System.exit(1); 91 } 92 93 try { 95 javax.jms.Topic topic = pubSession.createTopic(chatTopic); 96 javax.jms.TopicSubscriber subscriber = subSession.createSubscriber(topic); 97 subscriber.setMessageListener(this); 98 publisher = pubSession.createPublisher(topic); 99 connect.start(); 101 } catch (javax.jms.JMSException jmse) { 102 jmse.printStackTrace(); 103 } 104 105 try { 106 java.io.BufferedReader stdin = 108 new java.io.BufferedReader (new InputStreamReader (System.in)); 109 System.out.println("\nEnter text messages to clients that " + 110 "subscribe to the " + chatTopic + " topic." + 111 "\nPress Enter to publish each message.\n"); 112 while (true) { 113 String s = stdin.readLine(); 114 115 if (s == null) 116 exit(); 117 else if (s.length() > 0) { 118 javax.jms.TextMessage msg = pubSession.createTextMessage(); 119 msg.setText(username + ": " + s); 120 publisher.publish(msg, 121 javax.jms.DeliveryMode.NON_PERSISTENT, 122 javax.jms.Message.DEFAULT_PRIORITY, 123 MESSAGE_TTL); 124 } 125 } 126 } 127 catch (java.io.IOException ioe) { 128 ioe.printStackTrace(); 129 } catch (javax.jms.JMSException jmse) { 130 jmse.printStackTrace(); 131 } 132 } 133 134 138 public void onMessage(javax.jms.Message aMessage) 139 { 140 try { 141 javax.jms.TextMessage textMessage = 143 (javax.jms.TextMessage ) aMessage; 144 145 try { 148 String string = textMessage.getText(); 149 System.out.println(string); 150 } catch (javax.jms.JMSException jmse) { 151 jmse.printStackTrace(); 152 } 153 } catch (java.lang.RuntimeException rte) { 154 rte.printStackTrace(); 155 } 156 } 157 158 159 private void exit() 160 { 161 try { 162 connect.close(); 163 } catch (javax.jms.JMSException jmse) { 164 jmse.printStackTrace(); 165 } 166 167 System.exit(0); 168 } 169 170 175 176 public static void main(String argv[]) { 177 178 if (argv.length == 0) { 180 printUsage(); 181 System.exit(1); 182 } 183 184 String username = null; 186 String chatTopic = null; 187 188 for (int i = 0; i < argv.length; i++) { 190 String arg = argv[i]; 191 192 if (arg.equals("-u")) { 193 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 194 System.err.println("error: missing user name"); 195 System.exit(1); 196 } 197 username = argv[++i]; 198 continue; 199 } 200 201 if (arg.equals("-t")) { 202 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 203 System.err.println("error: missing topic name"); 204 System.exit(1); 205 } 206 chatTopic = argv[++i]; 207 continue; 208 } 209 210 if (arg.equals("-h")) { 211 printUsage(); 212 System.exit(1); 213 } 214 215 System.err.println ("error: unexpected argument: "+arg); 217 printUsage(); 218 System.exit(1); 219 } 220 221 if (username == null) { 223 System.err.println ("error: user name must be supplied"); 224 printUsage(); 225 System.exit(1); 226 } 227 228 if (chatTopic == null) { 229 System.err.println ("error: topic name must be supplied"); 230 printUsage(); 231 System.exit(1); 232 } 233 234 Chat chat = new Chat(); 236 chat.chatter(username, chatTopic); 237 238 } 239 240 241 private static void printUsage() { 242 243 StringBuffer use = new StringBuffer (); 244 use.append("usage: java Chat (options) ...\n\n"); 245 use.append("options:\n"); 246 use.append(" -u name Specify unique user name. (Required)\n"); 247 use.append(" -t topic Specify a topic for chat.\n"); 248 use.append(" -h This help screen.\n"); 249 System.err.println(use); 250 } 251 252 } 253 254 | Popular Tags |