1 46 package sample.jms.queues.guitalk; 47 48 import java.awt.BorderLayout ; 49 import java.awt.Color ; 50 import java.awt.Dimension ; 51 import java.awt.event.ActionEvent ; 52 import java.awt.event.ActionListener ; 53 import java.awt.event.WindowAdapter ; 54 import java.awt.event.WindowEvent ; 55 import java.io.IOException ; 56 57 import javax.jms.QueueConnectionFactory ; 58 import javax.jms.QueueSession ; 59 import javax.jms.Session ; 60 import javax.swing.BoxLayout ; 61 import javax.swing.JFrame ; 62 import javax.swing.JLabel ; 63 import javax.swing.JPanel ; 64 import javax.swing.JScrollPane ; 65 import javax.swing.JTextField ; 66 import javax.swing.JTextPane ; 67 import javax.swing.SwingUtilities ; 68 import javax.swing.text.BadLocationException ; 69 import javax.swing.text.Style ; 70 import javax.swing.text.StyleConstants ; 71 import javax.swing.text.StyleContext ; 72 import javax.swing.text.StyledDocument ; 73 74 import org.mr.api.jms.MantaQueueConnectionFactory; 75 76 77 81 82 public class Talk 83 implements javax.jms.MessageListener 84 { 85 86 private static final int MESSAGE_TTL = 6000000; 87 88 private javax.jms.QueueConnection con = null; 89 private javax.jms.QueueSession sendSession = null; 90 private javax.jms.QueueSession receiveSession = null; 91 private javax.jms.QueueSender sender = null; 92 93 private JFrame frame = null; 94 private JTextField textField = null; 95 private JTextPane textArea = null; 96 private StyledDocument doc = null; 97 private String username = null; 98 private String qSender = null; 99 private String qReceiver = null; 100 101 public Talk(String userName, String rQueue, String sQueue) { 102 this.username = userName; 103 qSender = rQueue; 104 qReceiver = sQueue; 105 createUI(); 106 } 107 108 109 private void talker() 111 { 112 try 114 { 115 QueueConnectionFactory conFactory = (QueueConnectionFactory ) new MantaQueueConnectionFactory(); 116 117 con = conFactory.createQueueConnection(); 119 120 sendSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 122 receiveSession =(QueueSession ) con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 123 124 } 125 catch (javax.jms.JMSException jmse) 126 { 127 jmse.printStackTrace(); 128 waitForAnyKey(); 129 130 try { 131 System.in.read(); 132 } catch (IOException e) { 133 e.printStackTrace(); 134 } 135 136 System.exit(1); 137 } 138 139 try 141 { 142 javax.jms.Queue sendQueue = sendSession.createQueue (qSender); 143 sender = sendSession.createSender(sendQueue); 144 javax.jms.Queue receiveQueue = receiveSession.createQueue (qReceiver); 145 javax.jms.QueueReceiver qReceiver = receiveSession.createReceiver(receiveQueue); 146 qReceiver.setMessageListener(this); 147 System.out.println ("\nStart receiving messages on queue \"" + qReceiver + "\".\n"); 150 con.start(); 151 } 152 catch (javax.jms.JMSException jmse) 153 { 154 jmse.printStackTrace(); 155 exit(); 156 } 157 } 158 159 private void createUI() { 161 frame = new JFrame ("Talk Client"); 162 JPanel panel = new JPanel (); 163 frame.setContentPane(panel); 164 panel.setLayout(new BorderLayout (5, 5)); 165 166 textArea = new JTextPane (); 168 doc = textArea.getStyledDocument(); 169 Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 170 Style localStyle = doc.addStyle("local", defaultStyle); 171 StyleConstants.setBold(localStyle, true); 172 StyleConstants.setForeground(localStyle, Color.blue); 173 Style remoteStyle = doc.addStyle("remote", defaultStyle); 174 StyleConstants.setBold(remoteStyle, true); 175 StyleConstants.setForeground(remoteStyle, Color.green); 176 JScrollPane areaScroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 177 areaScroll.setPreferredSize(new Dimension (250, 145)); 178 panel.add(areaScroll, BorderLayout.CENTER); 179 180 JPanel inputPanel = new JPanel (); 182 inputPanel.setLayout(new BoxLayout (inputPanel, BoxLayout.X_AXIS)); 183 inputPanel.add(new JLabel ("Text: ")); 184 textField = new JTextField (); 185 textField.addActionListener(new ActionListener () { 186 public void actionPerformed(ActionEvent e) { 187 String text = textField.getText(); 188 if (text == null) return; 189 text = text.trim(); 190 if (text.length() == 0) return; 191 String msgText = username + "> " + text; 192 try { 193 if (qSender != null) { 194 javax.jms.TextMessage msg = sendSession.createTextMessage(); 195 msg.setText(msgText); 196 sender.send( msg, 199 javax.jms.DeliveryMode.NON_PERSISTENT, 200 javax.jms.Message.DEFAULT_PRIORITY, 201 MESSAGE_TTL); 202 } 203 } 204 catch ( javax.jms.JMSException jmse ) 205 { 206 jmse.printStackTrace(); 207 } 208 209 try { 210 doc.insertString(doc.getLength(), msgText+"\n", doc.getStyle("local")); 211 } 212 catch (BadLocationException ble) {ble.printStackTrace();} 213 textField.setText(""); 214 textArea.setCaretPosition(textArea.getDocument().getLength()); 215 } 216 }); 217 inputPanel.add(textField); 218 panel.add(inputPanel, BorderLayout.SOUTH); 219 220 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 222 frame.addWindowListener(new WindowAdapter () { 223 public void windowClosing(WindowEvent e) { 224 exit(); 225 } 226 }); 227 228 frame.pack(); 230 231 } 232 233 private void showGUI() { 234 SwingUtilities.invokeLater(new Runnable () { 235 public void run() { 236 frame.setVisible(true); 237 textField.requestFocus(); 238 } 239 }); 240 } 241 242 246 public void onMessage( javax.jms.Message aMessage) 247 { 248 249 try 250 { 251 javax.jms.TextMessage textMessage = (javax.jms.TextMessage ) aMessage; 253 254 try 257 { 258 String string = textMessage.getText(); 259 doc.insertString(doc.getLength(), string+"\n", doc.getStyle("remote")); 260 textArea.setCaretPosition(doc.getLength()); 261 } 262 catch (javax.jms.JMSException jmse) 263 { 264 jmse.printStackTrace(); 265 } 266 } 267 catch (Throwable t) 268 { 269 t.printStackTrace(); 270 } 271 } 272 273 274 private void exit() 275 { 276 try 277 { 278 con.close(); 279 } 280 catch (javax.jms.JMSException jmse) 281 { 282 jmse.printStackTrace(); 283 } 284 frame.dispose(); 285 System.exit(0); 286 } 287 288 293 294 public static void main(String argv[]) { 295 296 if (argv.length == 0) { 298 printHelp(); 299 waitForAnyKey(); 300 System.exit(1); 301 } 302 303 305 String user = null; 306 String qSend = null; 307 String qReceive = null; 308 309 for (int i = 0; i < argv.length; i++) { 311 String arg = argv[i]; 312 313 if (!arg.startsWith("-")) { 315 System.err.println ("error: unexpected argument - "+arg); 316 printHelp(); 317 318 waitForAnyKey(); 319 System.exit(1); 320 } 321 else { 322 323 if (arg.equals("-u")) { 324 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 325 System.err.println("error: missing user name"); 326 System.exit(1); 327 } 328 user = argv[++i]; 329 continue; 330 } 331 332 if (arg.equals("-qr")) { 333 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 334 System.err.println("error: missing receive queue parameter"); 335 System.exit(1); 336 } 337 qReceive = argv[++i]; 338 continue; 339 } 340 341 if (arg.equals("-qs")) { 342 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 343 System.err.println("error: missing send queue parameter"); 344 System.exit(1); 345 } 346 qSend = argv[++i]; 347 continue; 348 } 349 350 351 if (arg.equals("-h")) { 352 printHelp(); 353 System.exit(1); 354 } 355 } 356 } 357 358 if (user == null) { 360 System.err.println ("error: user name must be supplied"); 361 printHelp(); 362 System.exit(1); 363 } 364 365 if (qReceive == null || qSend == null) { 366 System.err.println ("error: receive queue and send queue must be supplied"); 367 printHelp(); 368 System.exit(1); 369 } 370 371 Talk talk = new Talk(user, qReceive, qSend); 373 talk.talker(); 374 talk.showGUI(); 375 } 376 377 private static void waitForAnyKey(){ 378 System.out.print("Press any key ..."); 379 try { 380 System.in.read(); 381 } catch (IOException e) { 382 e.printStackTrace(); 383 } 384 } 385 386 387 private static void printHelp() { 388 389 StringBuffer use = new StringBuffer (); 390 use.append("help: java Talk (options) ...\n\n"); 391 use.append("options:\n"); 392 use.append(" -qr queue Specify queue for receiving messages.\n"); 393 use.append(" -qs queue Specify queue for sending messages.\n"); 394 use.append(" -u user Specify a user name.\n"); 395 use.append(" -h This help screen.\n"); 396 System.err.println (use); 397 } 398 399 } | Popular Tags |