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;
018    
019    import javax.jms.Connection;
020    import javax.jms.ConnectionFactory;
021    import javax.jms.DeliveryMode;
022    import javax.jms.Destination;
023    import javax.jms.JMSException;
024    import javax.jms.Message;
025    import javax.jms.MessageProducer;
026    import javax.jms.Session;
027    
028    /**
029     * 
030     */
031    public class MemProducer {
032        protected Connection connection;
033        protected MessageProducer producer;
034    
035        public MemProducer(ConnectionFactory fac, Destination dest) throws JMSException {
036            connection = fac.createConnection();
037            Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
038            producer = s.createProducer(dest);
039        }
040    
041        public void setDeliveryMode(int mode) throws JMSException {
042            producer.setDeliveryMode(mode);
043        }
044    
045        public void start() throws JMSException {
046            connection.start();
047        }
048    
049        public void stop() throws JMSException {
050            connection.stop();
051        }
052    
053        public void shutDown() throws JMSException {
054            connection.close();
055        }
056    
057        public void sendMessage(Message msg) throws JMSException {
058            sendMessage(msg, null, 0);
059        }
060    
061        /*
062        *   allow producer to attach message counter on its header. This will be used to verify message order
063        *
064        */
065        public void sendMessage(Message msg, String headerName, long headerValue) throws JMSException {
066            if (headerName != null) {
067                msg.setLongProperty(headerName, headerValue);
068            }
069    
070            producer.send(msg);
071    
072        }
073    
074    }