KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > queues > requestReply > QueueRequest


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
15  * USA
16  *
17  * Created on Aug 11, 2005
18  *
19  */

20 package sample.jms.queues.requestReply;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.jms.*;
28
29 import org.mr.api.jms.MantaConnectionFactory;
30 import org.mr.api.jms.MantaRequestor;
31
32 /**
33  * QueueRequest is a sample to usage of the mantaRequestor class.
34  * It creates a mantaRequestor that sends requests to an inserted queue, and register a
35  * Responder object, which is a message listener the same queue.
36  * The user should inseret text to be sent as requests by the mantaRequestor, and the
37  * Responder will send a suitable reply TextMessage to it. The QueueRequest
38  * will print to the screen the text received by the reply message.
39  *
40  * Usage:
41  * java QueueRequest queueName
42  *
43  * Whereas queueName holds the name of the queue requests should be made up on
44  *
45  * @author nambi sankaran
46  */

47 public class QueueRequest{
48     String JavaDoc queueName;
49     Queue q;
50     MantaConnectionFactory factory;
51     Queue queue;
52     MantaRequestor requestor;
53     Connection conn;
54     Session sess;
55     //QueueReply responder;
56

57     /**
58      * Builds a QueueRequestSample.
59      *
60      */

61     public QueueRequest(String JavaDoc 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         //responder = new Responder(queueName,"queue");
75
handleRequests();
76     }//constructor
77

78     /**
79      * reades entered text, sends it as a request TextMessage to the inseted queue,
80      * and waits for replies on it on the temporary queue of the mantaRequestor.
81      */

82     private void handleRequests(){
83         try
84         {
85            // Read all standard input and send it as a message.
86
java.io.BufferedReader JavaDoc stdin =
87                 new java.io.BufferedReader JavaDoc( new java.io.InputStreamReader JavaDoc( 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 JavaDoc s = stdin.readLine();
97                 
98                 if(s==null){
99                     continue;
100                 }
101                 //trim white spaces
102
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                   /*//operate the next section in order to recieve multiple replies for each request
114                     ArrayList replies=(ArrayList)requestor.request(textMsg,5000, 2);
115                     if (replies==null ||replies.size()==0)
116                         System.out.println("there were no replies");
117                     else{
118                         Iterator iter=replies.iterator();
119                         while (iter.hasNext()){
120                             reply=(TextMessage)iter.next();
121                             String replyText=((TextMessage)reply).getText();
122                             System.out.println("reply: "+replyText);
123                         }//while
124                     } //else
125                    */

126                     reply = requestor.request(textMsg);
127                     String JavaDoc replyText=((TextMessage)reply).getText();
128                     System.out.println("reply: "+replyText);
129                     
130                 }//else if
131
}//while
132
}
133         catch ( java.io.IOException JavaDoc ioe )
134         {
135             ioe.printStackTrace();
136         }
137         catch ( javax.jms.JMSException JavaDoc jmse )
138         {
139             jmse.printStackTrace();
140         }
141         // Close the connection.
142
exit();
143     }
144     
145     /** Cleanup resources and then exit. */
146     private void exit()
147     {
148         try{
149             requestor.close();
150             conn.close();
151         }catch (javax.jms.JMSException JavaDoc jmse){
152             jmse.printStackTrace();
153         }
154         System.exit(0);
155     }
156     
157     public static void main (String JavaDoc args[]){
158         // checks if arguments were inserted
159
if (args.length !=1) {
160             printHelp();
161             waitForAnyKey();
162             System.exit(1);
163         }
164
165         // Qname read from inserted parameters
166
String JavaDoc Qname= args[0];
167
168         // Check values read in.
169
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     }//main
177

178     /** Prints the usage. */
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     /** Waits for a key to be pressed.*/
184     private static void waitForAnyKey(){
185         System.out.print("press any key ...");
186         try {
187             System.in.read();
188         } catch (IOException JavaDoc e) {
189             e.printStackTrace();
190         }
191     }
192 }//QueueRequest
193
Popular Tags