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 javax.jms.DeliveryMode;
21  import javax.jms.Message;
22  import javax.jms.MessageProducer;
23  import java.util.Date;
24  
25  /***
26   * A simple tool for publishing messages
27   *
28   * @version $Revision: 1.6 $
29   */
30  public class ProducerTool extends ToolSupport {
31  
32      protected MessageProducer producer;
33      protected int messageCount = 10;
34      protected long sleepTime = 0L;
35      protected boolean verbose = true;
36  
37      public static void main(String[] args) {
38          ProducerTool tool = new ProducerTool();
39          if (args.length > 0) {
40              tool.url = args[0];
41          }
42          if (args.length > 1) {
43              tool.topic = args[1].equalsIgnoreCase("true");
44          }
45          if (args.length > 2) {
46              tool.subject = args[2];
47          }
48          if (args.length > 3) {
49              tool.durable = args[3].equalsIgnoreCase("true");
50          }
51          tool.run();
52      }
53  
54      public void run() {
55          try {
56              System.out.println("Connecting to URL: " + url);
57              System.out.println("Publishing to " + (topic ? "topic" : "queue") + ": " + subject);
58              System.out.println("Using " + (durable ? "durable" : "non-durable") + " publishing");
59  
60              createSession();
61              producer = session.createProducer(destination);
62              if (durable) {
63                  producer.setDeliveryMode(DeliveryMode.PERSISTENT);
64              }
65              else {
66                  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
67              }
68              connection.start();
69  
70              sendLoop();
71  
72              System.out.println("Done.");
73              close();
74          }
75          catch (Exception e) {
76              System.out.println("Caught: " + e);
77              e.printStackTrace();
78          }
79      }
80  
81      protected void sendLoop() throws Exception {
82          for (int i = 0; i < messageCount; i++) {
83              Message message = session.createTextMessage("Message: " + i + " sent at: " + new Date());
84  
85              if (verbose) {
86                  System.out.println("Sending message: " + message);
87              }
88  
89              producer.send(message);
90              Thread.sleep(sleepTime);
91          }
92      }
93  }