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