1 50 package sample.jms.topics.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 import org.mr.api.jms.MantaConnectionFactory; 65 import org.mr.core.util.ExceptionMonitor; 66 import org.mr.core.util.SystemTime; 67 68 78 public class Responder implements javax.jms.MessageListener { 79 final String QUEUE = "queue"; 80 final String TOPIC = "topic"; 81 String destType, destName; 82 MantaConnectionFactory factory; 83 Queue queue; 84 Topic topic; 85 Connection prodConn, consConn; 86 Session prodSess, consSess; 87 MessageProducer producer; 88 MessageConsumer consumer; 89 private Random random; 90 91 96 public Responder(String destinationName, String destinationType){ 97 factory = new MantaConnectionFactory(); 98 try { 99 if(destinationType.equalsIgnoreCase("queue")){ 100 destType=QUEUE; 101 }else{ 102 if(destinationType.equalsIgnoreCase("topic")){ 103 destType=TOPIC; 104 }else{ 105 try { 106 ExceptionMonitor.getInstance().shout(24, "There is no destination type "+destType); 107 } catch (JMSException e) { 108 e.printStackTrace(); 109 } 110 } } this.destName = destinationName; 113 prodConn = factory.createConnection(); 114 prodSess = prodConn.createSession(false, Session.AUTO_ACKNOWLEDGE); 115 consConn = factory.createConnection(); 116 consSess = consConn.createSession(false, Session.AUTO_ACKNOWLEDGE); 117 if (destType.equals(QUEUE)){ 118 queue = prodSess.createQueue(destName); 119 producer = prodSess.createProducer(null); 120 consumer = consSess.createConsumer(queue); 121 }else{ 122 topic = prodSess.createTopic(destName); 123 producer = prodSess.createProducer(null); 124 consumer = consSess.createConsumer(topic); 125 } 126 consumer.setMessageListener(this); 127 prodConn.start(); 128 consConn.start(); 129 130 } catch (JMSException e) { 131 e.printStackTrace(); 132 } 133 random = new Random (SystemTime.currentTimeMillis()); 134 } 136 139 public void onMessage(Message message) 140 { 141 try { 142 String text; 143 int i= random.nextInt(4); 144 switch (i) { 145 case 0 : text="i agree."; 146 break; 147 case 1 : text="yes, this is true."; 148 break; 149 case 2 : text="have a nice day."; 150 break; 151 case 3 : text="interesting, "+((TextMessage )message).getText(); 152 break; 153 default : text ="yep."; 154 } 155 TextMessage newMessage; 156 newMessage = prodSess.createTextMessage(); 157 newMessage.setText(text); 158 newMessage.setJMSCorrelationID(message.getJMSCorrelationID()); 159 producer.send(message.getJMSReplyTo(),newMessage); 160 } catch (JMSException e) { 161 e.printStackTrace(); 162 } 163 } 165 public void close(){ 166 try { 167 prodConn.close(); 168 consConn.close(); 169 } catch (JMSException e) { 170 e.printStackTrace(); 172 } 173 } 174 175 } | Popular Tags |