1 20 package sample.jms.queues.requestReply; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 27 import javax.jms.*; 28 29 import org.mr.api.jms.MantaConnectionFactory; 30 import org.mr.api.jms.MantaRequestor; 31 32 47 public class QueueRequest{ 48 String queueName; 49 Queue q; 50 MantaConnectionFactory factory; 51 Queue queue; 52 MantaRequestor requestor; 53 Connection conn; 54 Session sess; 55 57 61 public QueueRequest(String QName){ 62 factory = new MantaConnectionFactory(); 63 queueName = QName; 64 try { 65 conn = factory.createConnection(); 66 sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 67 q = sess.createQueue(queueName); 68 conn.start(); 69 requestor = new MantaRequestor(sess,q); 70 } catch (JMSException e) { 71 72 e.printStackTrace(); 73 } 74 handleRequests(); 76 } 78 82 private void handleRequests(){ 83 try 84 { 85 java.io.BufferedReader stdin = 87 new java.io.BufferedReader ( new java.io.InputStreamReader ( System.in ) ); 88 System.out.println("Enter request text to send to queue \"" + queueName + "\"."); 89 System.out.println("Press Enter to send each request message."); 90 System.out.println("Empty messages will not be sent."); 91 System.out.println("typing 'exit' will stop the program."); 92 93 while ( true ) 94 { 95 System.out.print(">"); 96 String s = stdin.readLine(); 97 98 if(s==null){ 99 continue; 100 } 101 s =s.trim(); 103 104 if(s.length()==0){ 105 continue; 106 } 107 if (s.equalsIgnoreCase("exit")){ 108 exit(); 109 } 110 else if (s.length()> 0){ 111 Message textMsg, reply; 112 textMsg = sess.createTextMessage(s); 113 126 reply = requestor.request(textMsg); 127 String replyText=((TextMessage)reply).getText(); 128 System.out.println("reply: "+replyText); 129 130 } } } 133 catch ( java.io.IOException ioe ) 134 { 135 ioe.printStackTrace(); 136 } 137 catch ( javax.jms.JMSException jmse ) 138 { 139 jmse.printStackTrace(); 140 } 141 exit(); 143 } 144 145 146 private void exit() 147 { 148 try{ 149 requestor.close(); 150 conn.close(); 151 }catch (javax.jms.JMSException jmse){ 152 jmse.printStackTrace(); 153 } 154 System.exit(0); 155 } 156 157 public static void main (String args[]){ 158 if (args.length !=1) { 160 printHelp(); 161 waitForAnyKey(); 162 System.exit(1); 163 } 164 165 String Qname= args[0]; 167 168 if (Qname == null) { 170 System.err.println ("error: request queue name must be supplied."); 171 printHelp(); 172 System.exit(1); 173 } 174 175 QueueRequest sample = new QueueRequest(Qname); 176 } 178 179 private static void printHelp() { 180 System.err.println ("help: QueueRequestorSample \nSpecify the name of queue up on to make requests.\n"); 181 } 182 183 184 private static void waitForAnyKey(){ 185 System.out.print("press any key ..."); 186 try { 187 System.in.read(); 188 } catch (IOException e) { 189 e.printStackTrace(); 190 } 191 } 192 } | Popular Tags |