KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > queues > synchRequest > QueueRequestSample


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is lital kasif.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  *
46  * Created on Mar 23, 2005
47  *
48  * Coridan LTD
49  */

50 package sample.jms.queues.synchRequest;
51
52 import java.io.IOException JavaDoc;
53 import javax.jms.*;
54
55 import org.mr.api.jms.MantaConnectionFactory;
56 import org.mr.api.jms.MantaRequestor;
57
58 /**
59  * QueueRequestSample is a sample to usage of the mantaRequestor class.
60  * It creates a mantaRequestor that sends requests to an inserted queue, and register a
61  * Responder object, which is a message listener the same queue.
62  * The user should inseret text to be sent as requests by the mantaRequestor, and the
63  * Responder will send a suitable reply TextMessage to it. The RequestSample
64  * will print to the screen the text received by the reply message.
65  *
66  * Usage:
67  * java QueueRequestSample queueName
68  *
69  * Whereas queueName holds the name of the queue requests should be made up on
70  *
71  * @author lital kasif
72  */

73 public class QueueRequestSample{
74     String JavaDoc queueName;
75     Queue q;
76     MantaConnectionFactory factory;
77     Queue queue;
78     MantaRequestor requestor;
79     Connection conn;
80     Session sess;
81     Responder responder;
82     
83     /**
84      * Builds a QueueRequestSample.
85      *
86      */

87     public QueueRequestSample(String JavaDoc 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     }//constructor
103

104     /**
105      * reades entered text, sends it as a request TextMessage to the inseted queue,
106      * and waits for replies on it on the temporary queue of the mantaRequestor.
107      */

108     private void handleRequests(){
109         try
110         {
111            // Read all standard input and send it as a message.
112
java.io.BufferedReader JavaDoc stdin =
113                 new java.io.BufferedReader JavaDoc( new java.io.InputStreamReader JavaDoc( 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 JavaDoc s = stdin.readLine();
123                 
124                 if(s==null){
125                     continue;
126                 }
127                 //trim white spaces
128
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                   /*//operate the next section in order to recieve multiple replies for each request
140                     ArrayList replies=(ArrayList)requestor.request(textMsg,5000, 2);
141                     if (replies==null ||replies.size()==0)
142                         System.out.println("there were no replies");
143                     else{
144                         Iterator iter=replies.iterator();
145                         while (iter.hasNext()){
146                             reply=(TextMessage)iter.next();
147                             String replyText=((TextMessage)reply).getText();
148                             System.out.println("reply: "+replyText);
149                         }//while
150                     } //else
151                    */

152                     reply = requestor.request(textMsg);
153                     String JavaDoc replyText=((TextMessage)reply).getText();
154                     System.out.println("Reply: "+replyText);
155                     
156                 }//else if
157
}//while
158
}
159         catch ( java.io.IOException JavaDoc ioe )
160         {
161             ioe.printStackTrace();
162         }
163         catch ( javax.jms.JMSException JavaDoc jmse )
164         {
165             jmse.printStackTrace();
166         }
167         // Close the connection.
168
exit();
169     }
170     
171     /** Cleanup resources and then exit. */
172     private void exit()
173     {
174         try{
175             requestor.close();
176             conn.close();
177             responder.close();
178         }catch (javax.jms.JMSException JavaDoc jmse){
179             jmse.printStackTrace();
180         }
181         System.exit(0);
182     }
183     
184     public static void main (String JavaDoc args[]){
185         // checks if arguments were inserted
186
if (args.length !=1) {
187             printHelp();
188             waitForAnyKey();
189             System.exit(1);
190         }
191
192         // Qname read from inserted parameters
193
String JavaDoc Qname= args[0];
194
195         // Check values read in.
196
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     }//main
204

205     /** Prints the usage. */
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     /** Waits for a key to be pressed.*/
211     private static void waitForAnyKey(){
212         System.out.print("Press any key...");
213         try {
214             System.in.read();
215         } catch (IOException JavaDoc e) {
216             e.printStackTrace();
217         }
218     }
219 }//QueueRequestSample
220
Popular Tags