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.pool;
018    
019    import javax.jms.Destination;
020    import javax.jms.JMSException;
021    import javax.jms.Message;
022    import javax.jms.MessageProducer;
023    
024    import org.apache.activemq.ActiveMQMessageProducer;
025    
026    /**
027     * A pooled {@link MessageProducer}
028     */
029    public class PooledProducer implements MessageProducer {
030    
031        private final ActiveMQMessageProducer messageProducer;
032        private final Destination destination;
033    
034        private int deliveryMode;
035        private boolean disableMessageID;
036        private boolean disableMessageTimestamp;
037        private int priority;
038        private long timeToLive;
039    
040        public PooledProducer(ActiveMQMessageProducer messageProducer, Destination destination) throws JMSException {
041            this.messageProducer = messageProducer;
042            this.destination = destination;
043    
044            this.deliveryMode = messageProducer.getDeliveryMode();
045            this.disableMessageID = messageProducer.getDisableMessageID();
046            this.disableMessageTimestamp = messageProducer.getDisableMessageTimestamp();
047            this.priority = messageProducer.getPriority();
048            this.timeToLive = messageProducer.getTimeToLive();
049        }
050    
051        @Override
052        public void close() throws JMSException {
053        }
054    
055        @Override
056        public void send(Destination destination, Message message) throws JMSException {
057            send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
058        }
059    
060        @Override
061        public void send(Message message) throws JMSException {
062            send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
063        }
064    
065        @Override
066        public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
067            send(destination, message, deliveryMode, priority, timeToLive);
068        }
069    
070        @Override
071        public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
072            if (destination == null) {
073                destination = this.destination;
074            }
075            ActiveMQMessageProducer messageProducer = getMessageProducer();
076    
077            // just in case let only one thread send at once
078            synchronized (messageProducer) {
079                messageProducer.send(destination, message, deliveryMode, priority, timeToLive);
080            }
081        }
082    
083        @Override
084        public Destination getDestination() {
085            return destination;
086        }
087    
088        @Override
089        public int getDeliveryMode() {
090            return deliveryMode;
091        }
092    
093        @Override
094        public void setDeliveryMode(int deliveryMode) {
095            this.deliveryMode = deliveryMode;
096        }
097    
098        @Override
099        public boolean getDisableMessageID() {
100            return disableMessageID;
101        }
102    
103        @Override
104        public void setDisableMessageID(boolean disableMessageID) {
105            this.disableMessageID = disableMessageID;
106        }
107    
108        @Override
109        public boolean getDisableMessageTimestamp() {
110            return disableMessageTimestamp;
111        }
112    
113        @Override
114        public void setDisableMessageTimestamp(boolean disableMessageTimestamp) {
115            this.disableMessageTimestamp = disableMessageTimestamp;
116        }
117    
118        @Override
119        public int getPriority() {
120            return priority;
121        }
122    
123        @Override
124        public void setPriority(int priority) {
125            this.priority = priority;
126        }
127    
128        @Override
129        public long getTimeToLive() {
130            return timeToLive;
131        }
132    
133        @Override
134        public void setTimeToLive(long timeToLive) {
135            this.timeToLive = timeToLive;
136        }
137    
138        // Implementation methods
139        // -------------------------------------------------------------------------
140        protected ActiveMQMessageProducer getMessageProducer() {
141            return messageProducer;
142        }
143    
144        @Override
145        public String toString() {
146            return "PooledProducer { " + messageProducer + " }";
147        }
148    }