1 46 package sample.jms.queues.Talk; 47 48 import java.io.IOException ; 49 50 import javax.jms.QueueConnectionFactory ; 51 import javax.jms.QueueSession ; 52 import javax.jms.Session ; 53 54 import org.mr.api.jms.MantaQueueConnectionFactory; 55 56 60 61 public class Talk1 62 implements javax.jms.MessageListener 63 { 64 65 private static final int MESSAGE_TTL = 6000000; 66 67 private javax.jms.QueueConnection con = null; 68 private javax.jms.QueueSession sendSession = null; 69 private javax.jms.QueueSession receiveSession = null; 70 private javax.jms.QueueSender sender = null; 71 72 73 private void talker(String userName, String rQueue, String sQueue) 74 { 75 try 77 { 78 QueueConnectionFactory conFactory = (QueueConnectionFactory ) new MantaQueueConnectionFactory(); 79 80 con = conFactory.createQueueConnection(); 82 83 sendSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 85 receiveSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 86 87 } 88 catch (javax.jms.JMSException jmse) 89 { 90 jmse.printStackTrace(); 91 waitForAnyKey(); 92 93 try { 94 System.in.read(); 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 99 System.exit(1); 100 } 101 102 try 104 { 105 javax.jms.Queue sendQueue = sendSession.createQueue (sQueue); 106 sender = sendSession.createSender(sendQueue); 107 javax.jms.Queue receiveQueue = receiveSession.createQueue (rQueue); 108 javax.jms.QueueReceiver qReceiver = receiveSession.createReceiver(receiveQueue); 109 qReceiver.setMessageListener(this); 110 System.out.println ("\nStart receiving messages on queue \"" + rQueue + "\".\n"); 113 con.start(); 114 } 115 catch (javax.jms.JMSException jmse) 116 { 117 jmse.printStackTrace(); 118 exit(); 119 } 120 try 121 { 122 java.io.BufferedReader stdin = 124 new java.io.BufferedReader ( new java.io.InputStreamReader ( System.in ) ); 125 if (sQueue != null){ 126 System.out.println("Enter text to send to queue \"" + sQueue + "\"."); 127 System.out.println("Press Enter to send each message."); 128 System.out.println("Empty messages will not be sent."); 129 System.out.println("Typing 'exit' will stop the program."); 130 } 131 132 while ( true ) 133 { 134 System.out.print(userName+">"); 135 String s = "foo"; 136 137 if(s == null) 138 continue; 139 s =s.trim(); 141 142 if(s.length() ==0) 143 continue; 144 145 if ( s.equalsIgnoreCase("exit")) 146 exit(); 147 148 else if ( s.length() > 0 && sQueue != null) 149 { 150 javax.jms.TextMessage msg = sendSession.createTextMessage(); 151 msg.setText( userName + ": " + s ); 152 sender.send( msg, 155 javax.jms.DeliveryMode.NON_PERSISTENT, 156 javax.jms.Message.DEFAULT_PRIORITY, 157 MESSAGE_TTL); 158 try { 159 Thread.sleep(5000); 160 } catch (Exception e) {} 161 } 162 } 163 } 164 catch ( javax.jms.JMSException jmse ) 165 { 166 jmse.printStackTrace(); 167 } 168 exit(); 170 } 171 172 176 public void onMessage( javax.jms.Message aMessage) 177 { 178 179 try 180 { 181 javax.jms.TextMessage textMessage = (javax.jms.TextMessage ) aMessage; 183 184 try 187 { 188 String string = textMessage.getText(); 189 System.out.println( string ); 190 } 191 catch (javax.jms.JMSException jmse) 192 { 193 jmse.printStackTrace(); 194 } 195 } 196 catch (Throwable t) 197 { 198 t.printStackTrace(); 199 } 200 } 201 202 203 private void exit() 204 { 205 try 206 { 207 con.close(); 208 } 209 catch (javax.jms.JMSException jmse) 210 { 211 jmse.printStackTrace(); 212 } 213 214 System.exit(0); 215 } 216 217 222 223 public static void main(String argv[]) { 224 225 if (argv.length == 0) { 227 printHelp(); 228 waitForAnyKey(); 229 System.exit(1); 230 } 231 232 234 String username = null; 235 236 String qSender = null; 237 String qReceiver = null; 238 239 for (int i = 0; i < argv.length; i++) { 241 String arg = argv[i]; 242 243 if (!arg.startsWith("-")) { 245 System.err.println ("error: unexpected argument - "+arg); 246 printHelp(); 247 248 waitForAnyKey(); 249 System.exit(1); 250 } 251 else { 252 253 if (arg.equals("-u")) { 254 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 255 System.err.println("error: missing user name"); 256 System.exit(1); 257 } 258 username = argv[++i]; 259 continue; 260 } 261 262 if (arg.equals("-qr")) { 263 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 264 System.err.println("error: missing receive queue parameter"); 265 System.exit(1); 266 } 267 qReceiver = argv[++i]; 268 continue; 269 } 270 271 if (arg.equals("-qs")) { 272 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 273 System.err.println("error: missing send queue parameter"); 274 System.exit(1); 275 } 276 qSender = argv[++i]; 277 continue; 278 } 279 280 281 if (arg.equals("-h")) { 282 printHelp(); 283 System.exit(1); 284 } 285 } 286 } 287 288 if (username == null) { 290 System.err.println ("error: user name must be supplied"); 291 printHelp(); 292 System.exit(1); 293 } 294 295 if (qReceiver == null || qSender == null) { 296 System.err.println ("error: receive queue and send queue must be supplied"); 297 printHelp(); 298 System.exit(1); 299 } 300 301 Talk1 talk = new Talk1(); 303 talk.talker(username ,qReceiver, qSender); 304 305 } 306 307 private static void waitForAnyKey(){ 308 System.out.print("Press any key ..."); 309 try { 310 System.in.read(); 311 } catch (IOException e) { 312 e.printStackTrace(); 313 } 314 } 315 316 317 private static void printHelp() { 318 319 StringBuffer use = new StringBuffer (); 320 use.append("help: java Talk (options) ...\n\n"); 321 use.append("options:\n"); 322 use.append(" -qr queue Specify queue for receiving messages.\n"); 323 use.append(" -qs queue Specify queue for sending messages.\n"); 324 use.append(" -u user Specify a user name.\n"); 325 use.append(" -h This help screen.\n"); 326 System.err.println (use); 327 } 328 329 } | Popular Tags |