1 4 package it.bigatti.c6; 5 6 import java.io.BufferedReader ; 7 import java.io.IOException ; 8 import java.io.InputStreamReader ; 9 10 import javax.jms.DeliveryMode ; 11 import javax.jms.JMSException ; 12 import javax.jms.Message ; 13 import javax.jms.MessageListener ; 14 import javax.jms.QueueConnection ; 15 import javax.jms.QueueConnectionFactory ; 16 import javax.jms.QueueSender ; 17 import javax.jms.QueueSession ; 18 import javax.jms.Session ; 19 import javax.jms.TextMessage ; 20 21 import org.mr.api.jms.MantaQueueConnectionFactory; 22 23 32 public class BadgeReader { 33 private static final int MESSAGE_TTL = 100000; 34 35 private QueueConnection con = null; 36 private QueueSession sendSession = null; 37 private QueueSession receiveSession = null; 38 private QueueSender sender = null; 39 40 43 public BadgeReader() throws JMSException , IOException { 44 QueueConnectionFactory conFactory = 45 (QueueConnectionFactory ) new MantaQueueConnectionFactory(); 46 47 con = conFactory.createQueueConnection(); 49 50 sendSession = (QueueSession ) 52 con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 53 receiveSession = (QueueSession ) 54 con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 55 56 javax.jms.Queue sendQueue = sendSession.createQueue( 58 C6Constants.SEND_QUEUE_NAME); 59 sender = sendSession.createSender(sendQueue); 60 61 javax.jms.Queue receiveQueue = receiveSession.createQueue( 63 C6Constants.RECEIVE_QUEUE_NAME); 64 javax.jms.QueueReceiver qReceiver = 65 receiveSession.createReceiver(receiveQueue); 66 67 qReceiver.setMessageListener( new MessageListener () { 69 public void onMessage(Message msg) { 70 TextMessage tmsg = (TextMessage )msg; 71 String line; 72 try { 73 line = tmsg.getText(); 74 System.out.println( "[receive] " + line ); 75 } catch (JMSException e) { 76 e.printStackTrace(); 77 } 78 } 79 }); 80 81 leggiEScrivi(); 82 } 83 84 85 void leggiEScrivi() throws JMSException , IOException { 86 BufferedReader stdin = 87 new BufferedReader ( 88 new InputStreamReader ( System.in ) ); 89 90 con.start(); 91 System.out.println("Pronti"); 92 93 while( true ) { 94 System.out.print("[input]> "); 95 String line = stdin.readLine(); 96 verificaBadge( line ); 97 } 98 } 99 100 101 void verificaBadge( final String codiceBadge ) throws JMSException { 102 Thread th = new Thread ( new Runnable () { 103 public void run() { 104 TextMessage msg; 105 try { 106 System.out.println("verificaBadge() " + codiceBadge ); 107 msg = sendSession.createTextMessage(); 108 msg.setText( "BADGE: " + codiceBadge ); 109 110 sender.send( msg, 111 DeliveryMode.NON_PERSISTENT, 112 Message.DEFAULT_PRIORITY, 113 MESSAGE_TTL); 114 115 System.out.println("[send] " + msg.getText()); 116 } catch (JMSException e) { 117 e.printStackTrace(); 118 } 119 } 120 } ); 121 th.start(); 122 } 123 124 public static void main(String [] args) { 125 try { 126 new BadgeReader(); 127 } catch (JMSException e) { 128 e.printStackTrace(); 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 } 133 } 134 | Popular Tags |