001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.tool;
019    
020    import java.util.Date;
021    
022    import javax.jms.Connection;
023    import javax.jms.DeliveryMode;
024    import javax.jms.JMSException;
025    import javax.jms.MessageProducer;
026    import javax.jms.Session;
027    import javax.jms.TextMessage;
028    
029    /**
030     * A simple tool for publishing messages
031     *
032     * @version $Revision$
033     */
034    public class ProducerTool extends ToolSupport {
035    
036        protected int messageCount = 10;
037        protected long sleepTime = 0L;
038        protected boolean verbose = true;
039        protected int messageSize = 255;
040    
041        public static void main(String[] args) {
042            runTool(args, new ProducerTool());
043        }
044    
045        protected static void runTool(String[] args, ProducerTool tool) {
046            if (args.length > 0) {
047                tool.url = args[0];
048            }
049            if (args.length > 1) {
050                tool.topic = args[1].equalsIgnoreCase("true");
051            }
052            if (args.length > 2) {
053                tool.subject = args[2];
054            }
055            if (args.length > 3) {
056                tool.durable = args[3].equalsIgnoreCase("true");
057            }
058            if (args.length > 4) {
059                tool.messageCount = Integer.parseInt(args[4]);
060            }
061            if (args.length > 5) {
062                tool.messageSize = Integer.parseInt(args[5]);
063            }
064            tool.run();
065        }
066    
067        public void run() {
068            try {
069                System.out.println("Connecting to URL: " + url);
070                System.out.println("Publishing a Message with size "+messageSize+" to " + (topic ? "topic" : "queue") + ": " + subject);
071                System.out.println("Using " + (durable ? "durable" : "non-durable") + " publishing");
072    
073                Connection connection = createConnection();
074                Session session = createSession(connection);
075                MessageProducer producer = createProducer(session);
076                //connection.start();
077    
078                sendLoop(session, producer);
079    
080                System.out.println("Done.");
081                close(connection, session);
082            }
083            catch (Exception e) {
084                System.out.println("Caught: " + e);
085                e.printStackTrace();
086            }
087        }
088    
089        protected MessageProducer createProducer(Session session) throws JMSException {
090            MessageProducer producer = session.createProducer(destination);
091            if (durable) {
092                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
093            }
094            else {
095                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
096            }
097            return producer;
098        }
099    
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    }