KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > topics > synchRequest > TopicRequestSample


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 27, 2005
47  *
48  * Coridan LTD
49  */

50
51 package sample.jms.topics.synchRequest;
52
53 import java.io.IOException JavaDoc;
54
55 import javax.jms.*;
56
57 import org.mr.api.jms.MantaConnectionFactory;
58 import org.mr.api.jms.MantaRequestor;
59
60
61 /*======================================================================================
62   For instructions on how to run this sample please refer to the file
63   sample\jms\topics\synchRequest\Readme.txt under the MantaRay installation directory.
64 ======================================================================================*/

65
66
67 /**
68  * TopicRequestSample is a sample to usage of the mantaRequestor class.
69  * It creates a mantaRequestor that sends requests to an inserted topic, and register a
70  * Responder object, which is a message listener the same topic.
71  * The user should inseret text to be sent as requests by the mantaRequestor, and the
72  * Responder will send a suitable reply TextMessage to it. The RequestSample
73  * will print to the screen the text received by the reply message.
74  *
75  * Usage:
76  * java TopicRequestSample topicName
77  *
78  * Whereas topicName holds the name of the topic requests should be made up on
79  *
80  * @author lital kasif
81  *
82  */

83 public class TopicRequestSample {
84     String JavaDoc topicName;
85     Topic t;
86     MantaConnectionFactory factory;
87     Queue queue;
88     MantaRequestor requestor;
89     Connection conn;
90     Session sess;
91     Responder responder;
92
93     /**
94      * Builds a TopicRequestSample.
95      */

96     public TopicRequestSample(String JavaDoc 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     }//constructor
112

113     /**
114      * reades entered text, sends it as a request TextMessage to the inseted topic,
115      * and waits for replies on it on the temporary queue of the mantaRequestor.
116      */

117     private void handleRequests(){
118         try
119         {
120            // Read all standard input and send it as a message.
121
java.io.BufferedReader JavaDoc stdin =
122                 new java.io.BufferedReader JavaDoc( new java.io.InputStreamReader JavaDoc( 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 JavaDoc s = stdin.readLine();
132
133                 if(s==null){
134                     continue;
135                 }
136                 //trim white spaces
137
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               /*//operate the next section in order to recieve multiple replies for each request
149                     ArrayList replies=(ArrayList)requestor.request(textMsg,10000, 2);
150                     if (replies==null ||replies.size()==0)
151                         System.out.println("there were no replies");
152                     else{
153                         Iterator iter=replies.iterator();
154                         while (iter.hasNext()){
155                             reply=(TextMessage)iter.next();
156                             String replyText=((TextMessage)reply).getText();
157                             System.out.println("reply: "+replyText);
158                         }//while
159                     } //else
160                    */

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

214     /** Prints the usage. */
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     /** Waits for a key to be pressed.*/
220     private static void waitForAnyKey(){
221         System.out.print("Press any key ...");
222         try {
223             System.in.read();
224         } catch (IOException JavaDoc e) {
225             e.printStackTrace();
226         }
227     }
228 }//TopicRequestSample
229

230
Popular Tags