1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.tool;
19
20 import java.util.Date;
21
22 import javax.jms.Connection;
23 import javax.jms.DeliveryMode;
24 import javax.jms.JMSException;
25 import javax.jms.MessageProducer;
26 import javax.jms.Session;
27 import javax.jms.TextMessage;
28
29 /***
30 * A simple tool for publishing messages
31 *
32 * @version $Revision: 1.8 $
33 */
34 public class ProducerTool extends ToolSupport {
35
36 protected int messageCount = 10;
37 protected long sleepTime = 0L;
38 protected boolean verbose = true;
39 protected int messageSize = 255;
40
41 public static void main(String[] args) {
42 runTool(args, new ProducerTool());
43 }
44
45 protected static void runTool(String[] args, ProducerTool tool) {
46 if (args.length > 0) {
47 tool.url = args[0];
48 }
49 if (args.length > 1) {
50 tool.topic = args[1].equalsIgnoreCase("true");
51 }
52 if (args.length > 2) {
53 tool.subject = args[2];
54 }
55 if (args.length > 3) {
56 tool.durable = args[3].equalsIgnoreCase("true");
57 }
58 if (args.length > 4) {
59 tool.messageCount = Integer.parseInt(args[4]);
60 }
61 if (args.length > 5) {
62 tool.messageSize = Integer.parseInt(args[5]);
63 }
64 tool.run();
65 }
66
67 public void run() {
68 try {
69 System.out.println("Connecting to URL: " + url);
70 System.out.println("Publishing a Message with size "+messageSize+" to " + (topic ? "topic" : "queue") + ": " + subject);
71 System.out.println("Using " + (durable ? "durable" : "non-durable") + " publishing");
72
73 Connection connection = createConnection();
74 Session session = createSession(connection);
75 MessageProducer producer = createProducer(session);
76
77
78 sendLoop(session, producer);
79
80 System.out.println("Done.");
81 close(connection, session);
82 }
83 catch (Exception e) {
84 System.out.println("Caught: " + e);
85 e.printStackTrace();
86 }
87 }
88
89 protected MessageProducer createProducer(Session session) throws JMSException {
90 MessageProducer producer = session.createProducer(destination);
91 if (durable) {
92 producer.setDeliveryMode(DeliveryMode.PERSISTENT);
93 }
94 else {
95 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
96 }
97 return producer;
98 }
99
100 protected void sendLoop(Session session, MessageProducer producer) throws Exception {
101
102 for (int i = 0; i < messageCount; i++) {
103
104
105 TextMessage message = session.createTextMessage(createMessageText(i));
106
107 if (verbose) {
108 String msg = message.getText();
109 if( msg.length() > 50 )
110 msg = msg.substring(0, 50)+"...";
111 System.out.println("Sending message: " + msg);
112 }
113
114 producer.send(message);
115 Thread.sleep(sleepTime);
116 }
117 producer.send(session.createMessage());
118 }
119
120 /***
121 * @param i
122 * @return
123 */
124 private String createMessageText(int index) {
125 StringBuffer buffer = new StringBuffer(messageSize);
126 buffer.append("Message: " + index + " sent at: " + new Date());
127 if( buffer.length() > messageSize ) {
128 return buffer.substring(0, messageSize);
129 }
130 for( int i=buffer.length(); i < messageSize; i++)
131 buffer.append(' ');
132 return buffer.toString();
133 }
134 }