1 50 51 package sample.jms.topics.synchRequest; 52 53 import java.io.IOException ; 54 55 import javax.jms.*; 56 57 import org.mr.api.jms.MantaConnectionFactory; 58 import org.mr.api.jms.MantaRequestor; 59 60 61 65 66 67 83 public class TopicRequestSample { 84 String topicName; 85 Topic t; 86 MantaConnectionFactory factory; 87 Queue queue; 88 MantaRequestor requestor; 89 Connection conn; 90 Session sess; 91 Responder responder; 92 93 96 public TopicRequestSample(String TName){ 97 factory = new MantaConnectionFactory(); 98 topicName = TName; 99 try { 100 conn = factory.createConnection(); 101 sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 102 t = sess.createTopic(topicName); 103 conn.start(); 104 requestor = new MantaRequestor(sess,t); 105 } catch (JMSException e) { 106 107 e.printStackTrace(); 108 } 109 responder = new Responder(topicName,"topic"); 110 handleRequests(); 111 } 113 117 private void handleRequests(){ 118 try 119 { 120 java.io.BufferedReader stdin = 122 new java.io.BufferedReader ( new java.io.InputStreamReader ( System.in ) ); 123 System.out.println("Enter request text to send to topic \"" + topicName + "\"."); 124 System.out.println("Press Enter to send each request message."); 125 System.out.println("Empty messages will not be sent."); 126 System.out.println("Typing 'exit' will stop the program."); 127 128 while ( true ) 129 { 130 System.out.print(">"); 131 String s = stdin.readLine(); 132 133 if(s==null){ 134 continue; 135 } 136 s =s.trim(); 138 139 if(s.length()==0){ 140 continue; 141 } 142 if (s.equalsIgnoreCase("exit")){ 143 exit(); 144 } 145 else if (s.length()> 0){ 146 Message textMsg, reply; 147 textMsg = sess.createTextMessage(s); 148 161 reply = requestor.request(textMsg); 162 String replyText=((TextMessage)reply).getText(); 163 System.out.println("reply: "+replyText); 164 165 } } } 168 catch ( java.io.IOException ioe ) 169 { 170 ioe.printStackTrace(); 171 } 172 catch ( javax.jms.JMSException jmse ) 173 { 174 jmse.printStackTrace(); 175 } 176 exit(); 178 } 179 180 181 private void exit() 182 { 183 try{ 184 requestor.close(); 185 conn.close(); 186 responder.close(); 187 }catch (javax.jms.JMSException jmse){ 188 jmse.printStackTrace(); 189 } 190 System.exit(0); 191 } 192 193 public static void main (String args[]){ 194 if (args.length !=1) { 196 printHelp(); 197 waitForAnyKey(); 198 System.exit(1); 199 } 200 201 String Tname= args[0]; 203 204 if (Tname == null) { 206 System.err.println ("error: request topic name must be supplied."); 207 printHelp(); 208 System.exit(1); 209 } 210 211 TopicRequestSample sample = new TopicRequestSample(Tname); 212 } 214 215 private static void printHelp() { 216 System.err.println ("help: TopicRequestorSample \nSpecify the name of topic up on to make requests.\n"); 217 } 218 219 220 private static void waitForAnyKey(){ 221 System.out.print("Press any key ..."); 222 try { 223 System.in.read(); 224 } catch (IOException e) { 225 e.printStackTrace(); 226 } 227 } 228 } 230 | Popular Tags |