KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ProducerTool


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.MessageProducer JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27
28 import org.apache.activemq.ActiveMQConnection;
29 import org.apache.activemq.ActiveMQConnectionFactory;
30 import org.apache.activemq.util.IndentPrinter;
31
32 /**
33  * A simple tool for publishing messages
34  *
35  * @version $Revision: 1.2 $
36  */

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