KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > 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 package org.apache.activemq.tool;
19
20 import java.util.Date JavaDoc;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.DeliveryMode JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28
29 /**
30  * A simple tool for publishing messages
31  *
32  * @version $Revision$
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 JavaDoc[] args) {
42         runTool(args, new ProducerTool());
43     }
44
45     protected static void runTool(String JavaDoc[] 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 JavaDoc connection = createConnection();
74             Session JavaDoc session = createSession(connection);
75             MessageProducer JavaDoc producer = createProducer(session);
76             //connection.start();
77

78             sendLoop(session, producer);
79
80             System.out.println("Done.");
81             close(connection, session);
82         }
83         catch (Exception JavaDoc e) {
84             System.out.println("Caught: " + e);
85             e.printStackTrace();
86         }
87     }
88
89     protected MessageProducer JavaDoc createProducer(Session JavaDoc session) throws JMSException JavaDoc {
90         MessageProducer JavaDoc 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 JavaDoc session, MessageProducer JavaDoc producer) throws Exception JavaDoc {
101         
102         for (int i = 0; i < messageCount; i++) {
103         
104             
105             TextMessage JavaDoc message = session.createTextMessage(createMessageText(i));
106
107             if (verbose) {
108                 String JavaDoc 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 JavaDoc createMessageText(int index) {
125         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(messageSize);
126         buffer.append("Message: " + index + " sent at: " + new Date JavaDoc());
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 }
135
Popular Tags