KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RequesterTool


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import java.util.Arrays JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import javax.jms.Connection JavaDoc;
22 import javax.jms.DeliveryMode JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageConsumer JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29
30 import org.apache.activemq.ActiveMQConnection;
31 import org.apache.activemq.ActiveMQConnectionFactory;
32 import org.apache.activemq.util.IndentPrinter;
33
34 /**
35  * A simple tool for publishing messages
36  *
37  * @version $Revision: 1.2 $
38  */

39 public class RequesterTool {
40
41     private int messageCount = 10;
42     private long sleepTime = 0L;
43     private boolean verbose = true;
44     private int messageSize = 255;
45     private long timeToLive;
46     private String JavaDoc subject = "TOOL.DEFAULT";
47     private String JavaDoc replySubject;
48     private boolean topic = false;
49     private String JavaDoc user = ActiveMQConnection.DEFAULT_USER;
50     private String JavaDoc password = ActiveMQConnection.DEFAULT_PASSWORD;
51     private String JavaDoc url = ActiveMQConnection.DEFAULT_BROKER_URL;
52     private boolean transacted = false;
53     private boolean persistent = false;
54     private String JavaDoc clientId;
55
56     private Destination JavaDoc destination;
57     private Destination JavaDoc replyDest;
58     private MessageProducer JavaDoc producer;
59     private MessageConsumer JavaDoc consumer;
60     private Session JavaDoc session;
61
62     public static void main(String JavaDoc[] args) {
63         RequesterTool requesterTool = new RequesterTool();
64         String JavaDoc[] unknonwn = CommnadLineSupport.setOptions(requesterTool, args);
65         if (unknonwn.length > 0) {
66             System.out.println("Unknown options: " + Arrays.toString(unknonwn));
67             System.exit(-1);
68         }
69         requesterTool.run();
70     }
71
72     public void run() {
73         
74         Connection JavaDoc connection=null;
75         try {
76             
77             System.out.println("Connecting to URL: " + url);
78             System.out.println("Publishing a Message with size " + messageSize + " to " + (topic ? "topic" : "queue") + ": " + subject);
79             System.out.println("Using " + (persistent ? "persistent" : "non-persistent") + " messages");
80             System.out.println("Sleeping between publish " + sleepTime + " ms");
81             
82             // Create the connection
83
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
84             connection = connectionFactory.createConnection();
85             if (persistent && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
86                 connection.setClientID(clientId);
87             }
88             connection.start();
89
90             // Create the Session
91
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
92             
93             // And the Destinations..
94
if (topic) {
95                 destination = session.createTopic(subject);
96                 if( replySubject==null || replySubject.equals("") )
97                     replyDest = session.createTemporaryTopic();
98                 else
99                     replyDest = session.createTopic(replySubject);
100             } else {
101                 destination = session.createQueue(subject);
102                 if( replySubject==null || replySubject.equals("") )
103                     replyDest = session.createTemporaryQueue();
104                 else
105                     replyDest = session.createQueue(replySubject);
106             }
107             System.out.println("Reply Destination: " + replyDest);
108
109             // Create the producer
110
producer = session.createProducer(destination);
111             if (persistent) {
112                 producer.setDeliveryMode(DeliveryMode.PERSISTENT);
113             } else {
114                 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
115             }
116             if (timeToLive != 0) {
117                 System.out.println("Messages time to live " + timeToLive + " ms");
118                 producer.setTimeToLive(timeToLive);
119             }
120
121             // Create the reply consumer
122
consumer = session.createConsumer(replyDest);
123             
124             // Start sending reqests.
125
requestLoop();
126             
127             System.out.println("Done.");
128             
129             // Use the ActiveMQConnection interface to dump the connection stats.
130
ActiveMQConnection c = (ActiveMQConnection) connection;
131             c.getConnectionStats().dump(new IndentPrinter());
132                         
133         } catch (Exception JavaDoc e) {
134             System.out.println("Caught: " + e);
135             e.printStackTrace();
136         } finally {
137             try {
138                 connection.close();
139             } catch (Throwable JavaDoc ignore) {
140             }
141         }
142     }
143
144     protected void requestLoop() throws Exception JavaDoc {
145
146         for (int i = 0; i < messageCount || messageCount == 0; i++) {
147
148             TextMessage JavaDoc message = session.createTextMessage(createMessageText(i));
149             message.setJMSReplyTo(replyDest);
150
151             if (verbose) {
152                 String JavaDoc msg = message.getText();
153                 if (msg.length() > 50) {
154                     msg = msg.substring(0, 50) + "...";
155                 }
156                 System.out.println("Sending message: " + msg);
157             }
158
159             producer.send(message);
160             if (transacted) {
161                 session.commit();
162             }
163
164             System.out.println("Waiting for reponse message...");
165             Message JavaDoc message2 = consumer.receive();
166             if (message2 instanceof TextMessage JavaDoc) {
167                 System.out.println("Reponse message: " + ((TextMessage JavaDoc) message2).getText());
168             } else {
169                 System.out.println("Reponse message: " + message2);
170             }
171             if (transacted) {
172                 session.commit();
173             }
174
175             Thread.sleep(sleepTime);
176
177         }
178     }
179
180     /**
181      * @param i
182      * @return
183      */

184     private String JavaDoc createMessageText(int index) {
185         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(messageSize);
186         buffer.append("Message: " + index + " sent at: " + new Date JavaDoc());
187         if (buffer.length() > messageSize) {
188             return buffer.substring(0, messageSize);
189         }
190         for (int i = buffer.length(); i < messageSize; i++) {
191             buffer.append(' ');
192         }
193         return buffer.toString();
194     }
195
196
197     public void setClientId(String JavaDoc clientId) {
198         this.clientId = clientId;
199     }
200     public void setPersistent(boolean durable) {
201         this.persistent = durable;
202     }
203     public void setMessageCount(int messageCount) {
204         this.messageCount = messageCount;
205     }
206     public void setMessageSize(int messageSize) {
207         this.messageSize = messageSize;
208     }
209     public void setPassword(String JavaDoc password) {
210         this.password = password;
211     }
212     public void setSleepTime(long sleepTime) {
213         this.sleepTime = sleepTime;
214     }
215     public void setSubject(String JavaDoc subject) {
216         this.subject = subject;
217     }
218     public void setTimeToLive(long timeToLive) {
219         this.timeToLive = timeToLive;
220     }
221     public void setTopic(boolean topic) {
222         this.topic = topic;
223     }
224     public void setQueue(boolean queue) {
225         this.topic = !queue;
226     }
227     public void setTransacted(boolean transacted) {
228         this.transacted = transacted;
229     }
230     public void setUrl(String JavaDoc url) {
231         this.url = url;
232     }
233     public void setUser(String JavaDoc user) {
234         this.user = user;
235     }
236     public void setVerbose(boolean verbose) {
237         this.verbose = verbose;
238     }
239     public void setReplySubject(String JavaDoc replySubject) {
240         this.replySubject = replySubject;
241     }
242 }
243
Popular Tags