1 50 package sample.jms.queues.synchRequest; 51 52 import java.util.Random ; 53 54 import javax.jms.Connection ; 55 import javax.jms.JMSException ; 56 import javax.jms.Message ; 57 import javax.jms.MessageConsumer ; 58 import javax.jms.MessageProducer ; 59 import javax.jms.Queue ; 60 import javax.jms.Session ; 61 import javax.jms.TextMessage ; 62 import javax.jms.Topic ; 63 64 65 import org.mr.api.jms.MantaConnectionFactory; 66 import org.mr.core.util.ExceptionMonitor; 67 import org.mr.core.util.SystemTime; 68 69 70 74 75 76 86 public class Responder implements javax.jms.MessageListener { 87 final String QUEUE = "queue"; 88 final String TOPIC = "topic"; 89 String destType, destName; 90 MantaConnectionFactory factory; 91 Queue queue; 92 Topic topic; 93 Connection prodConn, consConn; 94 Session prodSess, consSess; 95 MessageProducer producer; 96 MessageConsumer consumer; 97 private Random random; 98 99 104 public Responder(String destinationName, String destinationType){ 105 factory = new MantaConnectionFactory(); 106 try { 107 if(destinationType.equalsIgnoreCase("queue")){ 108 destType=QUEUE; 109 }else{ 110 if(destinationType.equalsIgnoreCase("topic")){ 111 destType=TOPIC; 112 }else{ 113 try { 114 ExceptionMonitor.getInstance().shout(24, "There is no destination type "+destType); 115 } catch (JMSException e) { 116 e.printStackTrace(); 117 } 118 } } this.destName = destinationName; 121 prodConn = factory.createConnection(); 122 prodSess = prodConn.createSession(false, Session.AUTO_ACKNOWLEDGE); 123 consConn = factory.createConnection(); 124 consSess = consConn.createSession(false, Session.AUTO_ACKNOWLEDGE); 125 if (destType.equals(QUEUE)){ 126 queue = prodSess.createQueue(destName); 127 producer = prodSess.createProducer(null); 128 consumer = consSess.createConsumer(queue); 129 }else{ 130 topic = prodSess.createTopic(destName); 131 producer = prodSess.createProducer(null); 132 consumer = consSess.createConsumer(topic); 133 } 134 consumer.setMessageListener(this); 135 prodConn.start(); 136 consConn.start(); 137 138 } catch (JMSException e) { 139 e.printStackTrace(); 140 } 141 random = new Random (SystemTime.currentTimeMillis()); 142 } 144 147 public void onMessage(Message message) 148 { 149 try { 150 String text; 151 int i= random.nextInt(4); 152 switch (i) { 153 case 0 : text="I agree."; 154 break; 155 case 1 : text="Yes, this is true."; 156 break; 157 case 2 : text="Have a nice day."; 158 break; 159 case 3 : text="Interesting, "+((TextMessage )message).getText(); 160 break; 161 default : text ="Yep."; 162 } 163 TextMessage newMessage; 164 newMessage = prodSess.createTextMessage(); 165 newMessage.setText(text); 166 newMessage.setJMSCorrelationID(message.getJMSCorrelationID()); 167 producer.send(message.getJMSReplyTo(),newMessage); 168 } catch (JMSException e) { 169 e.printStackTrace(); 170 } 171 } 173 public void close(){ 174 try { 175 prodConn.close(); 176 consConn.close(); 177 } catch (JMSException e) { 178 e.printStackTrace(); 179 } 180 } 181 } | Popular Tags |