001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.tool.properties;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import java.util.Set;
022    
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    public class JmsProducerProperties extends JmsClientProperties {
027            
028            private static final Logger LOG = LoggerFactory.getLogger(ReflectionUtil.class);
029            
030        public static final String TIME_BASED_SENDING  = "time"; // Produce messages base on a time interval
031        public static final String COUNT_BASED_SENDING = "count"; // Produce a specific count of messages
032        public static final String DELIVERY_MODE_PERSISTENT     = "persistent"; // Persistent message delivery
033        public static final String DELIVERY_MODE_NON_PERSISTENT = "nonpersistent"; // Non-persistent message delivery
034        
035        protected String deliveryMode = DELIVERY_MODE_NON_PERSISTENT; // Message delivery mode
036        protected int messageSize = 1024; // Send 1kb messages by default
037        protected long sendCount  = 1000000; // Send a million messages by default
038        protected long sendDuration = 5 * 60 * 1000; // Send for 5 mins by default
039        protected String sendType = TIME_BASED_SENDING;
040        protected long sendDelay = 0;  // delay in milliseconds between each producer send
041        protected String msgFileName = null; // for sending a particular msg from a file
042        
043        protected Map<String,Object> headerMap = null;
044     
045        
046        // If true, create a different message on each send, otherwise reuse.
047        protected boolean createNewMsg; 
048        
049        public JmsProducerProperties() {
050            this.headerMap = new HashMap();
051        }
052    
053        public String getDeliveryMode() {
054            return deliveryMode;
055        }
056    
057        public void setDeliveryMode(String deliveryMode) {
058            this.deliveryMode = deliveryMode;
059        }
060    
061        public int getMessageSize() {
062            return messageSize;
063        }
064    
065        public void setMessageSize(int messageSize) {
066            this.messageSize = messageSize;
067        }
068    
069        public long getSendCount() {
070            return sendCount;
071        }
072    
073        public void setSendCount(long sendCount) {
074            this.sendCount = sendCount;
075        }
076    
077        public long getSendDuration() {
078            return sendDuration;
079        }
080    
081        public void setSendDuration(long sendDuration) {
082            this.sendDuration = sendDuration;
083        }
084    
085        public String getSendType() {
086            return sendType;
087        }
088    
089        public void setSendType(String sendType) {
090            this.sendType = sendType;
091        }
092    
093        public boolean isCreateNewMsg() {
094            return createNewMsg;
095        }
096    
097        public void setCreateNewMsg(boolean createNewMsg) {
098            this.createNewMsg = createNewMsg;
099        }
100        
101        public void setSendDelay(long delay) {
102            this.sendDelay = delay;
103        }
104        
105        public long getSendDelay() {
106            return this.sendDelay;
107        }
108        
109        
110        /* Operations for supporting message headers */
111        
112        /**
113         * Method for setting a message header. 
114         * @param encodedHeader - the header is encoded as a string using this syntax:
115         * encodedHeader = [headerkey '=' headervalue ':' ]*
116         * E.g. an encodedHeader could read "JMSType=car", or
117         * "JMSType=car:MyHeader=MyValue"
118         *
119         * That implies neither the header key nor the value 
120         * can contain any of the characters ':' and '='.
121         */
122        public void setHeader(String encodedHeader) {
123            
124            // remove any trailing ':' characters
125            if (encodedHeader.endsWith(":")) {
126                    encodedHeader = encodedHeader.substring(0, encodedHeader.length()-1);
127            }
128    
129            // split headers 
130            String headers[] = encodedHeader.split(":");
131            for (String h : headers) {
132                    
133                    // split into header name and value
134                    String tokens[] = h.split("=");
135                    
136                    // sanity check, don't allow empty string for header names
137                    if (tokens.length != 2 || tokens[0].equals("") || tokens[1].equals("") ) {
138                            LOG.error("Error parsing message headers. Header: \"" + h +
139                                            "\". This header will be ignored.");
140                    } else {
141                            this.headerMap.put(tokens[0], tokens[1]);
142                    }
143            }
144        }
145        
146        public Set<String> getHeaderKeys() {
147            return this.headerMap.keySet();
148        }
149        
150        public Object getHeaderValue(String key) {
151            return this.headerMap.get(key);
152        }  
153        
154        public void clearHeaders() {
155            this.headerMap.clear();
156        }
157        
158        public void setMsgFileName(String file) {
159            LOG.info("\"producer.msgFileName\" specified. " +
160                            "Will ignore setting \"producer.messageSize\".");
161            this.msgFileName = file;
162        }
163        
164        public String getMsgFileName() {
165            return this.msgFileName;
166        }
167        
168    }