1 50 package sample.jms.queues.synchRequest; 51 52 import java.io.IOException ; 53 import javax.jms.*; 54 55 import org.mr.api.jms.MantaConnectionFactory; 56 import org.mr.api.jms.MantaRequestor; 57 58 73 public class QueueRequestSample{ 74 String queueName; 75 Queue q; 76 MantaConnectionFactory factory; 77 Queue queue; 78 MantaRequestor requestor; 79 Connection conn; 80 Session sess; 81 Responder responder; 82 83 87 public QueueRequestSample(String QName){ 88 factory = new MantaConnectionFactory(); 89 queueName = QName; 90 try { 91 conn = factory.createConnection(); 92 sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 93 q = sess.createQueue(queueName); 94 conn.start(); 95 requestor = new MantaRequestor(sess,q); 96 } catch (JMSException e) { 97 98 e.printStackTrace(); 99 } 100 responder = new Responder(queueName,"queue"); 101 handleRequests(); 102 } 104 108 private void handleRequests(){ 109 try 110 { 111 java.io.BufferedReader stdin = 113 new java.io.BufferedReader ( new java.io.InputStreamReader ( System.in ) ); 114 System.out.println("Enter request text to send to queue \"" + queueName + "\"."); 115 System.out.println("Press Enter to send each request message."); 116 System.out.println("Empty messages will not be sent."); 117 System.out.println("Typing 'exit' will stop the program."); 118 119 while ( true ) 120 { 121 System.out.print(">"); 122 String s = stdin.readLine(); 123 124 if(s==null){ 125 continue; 126 } 127 s =s.trim(); 129 130 if(s.length()==0){ 131 continue; 132 } 133 if (s.equalsIgnoreCase("exit")){ 134 exit(); 135 } 136 else if (s.length()> 0){ 137 Message textMsg, reply; 138 textMsg = sess.createTextMessage(s); 139 152 reply = requestor.request(textMsg); 153 String replyText=((TextMessage)reply).getText(); 154 System.out.println("Reply: "+replyText); 155 156 } } } 159 catch ( java.io.IOException ioe ) 160 { 161 ioe.printStackTrace(); 162 } 163 catch ( javax.jms.JMSException jmse ) 164 { 165 jmse.printStackTrace(); 166 } 167 exit(); 169 } 170 171 172 private void exit() 173 { 174 try{ 175 requestor.close(); 176 conn.close(); 177 responder.close(); 178 }catch (javax.jms.JMSException jmse){ 179 jmse.printStackTrace(); 180 } 181 System.exit(0); 182 } 183 184 public static void main (String args[]){ 185 if (args.length !=1) { 187 printHelp(); 188 waitForAnyKey(); 189 System.exit(1); 190 } 191 192 String Qname= args[0]; 194 195 if (Qname == null) { 197 System.err.println ("Error: request queue name must be supplied."); 198 printHelp(); 199 System.exit(1); 200 } 201 202 QueueRequestSample sample = new QueueRequestSample(Qname); 203 } 205 206 private static void printHelp() { 207 System.err.println ("Help: QueueRequestorSample \nSpecify the name of queue up on to make requests.\n"); 208 } 209 210 211 private static void waitForAnyKey(){ 212 System.out.print("Press any key..."); 213 try { 214 System.in.read(); 215 } catch (IOException e) { 216 e.printStackTrace(); 217 } 218 } 219 } | Popular Tags |