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 */
017package org.apache.activemq.jms.pool;
018
019import javax.jms.Destination;
020import javax.jms.InvalidDestinationException;
021import javax.jms.JMSException;
022import javax.jms.Message;
023import javax.jms.MessageProducer;
024
025/**
026 * A pooled {@link MessageProducer}
027 */
028public class PooledProducer implements MessageProducer {
029
030    private final MessageProducer messageProducer;
031    private final Destination destination;
032
033    private int deliveryMode;
034    private boolean disableMessageID;
035    private boolean disableMessageTimestamp;
036    private int priority;
037    private long timeToLive;
038    private boolean anonymous = true;
039
040    public PooledProducer(MessageProducer messageProducer, Destination destination) throws JMSException {
041        this.messageProducer = messageProducer;
042        this.destination = destination;
043        this.anonymous = messageProducer.getDestination() == null;
044
045        this.deliveryMode = messageProducer.getDeliveryMode();
046        this.disableMessageID = messageProducer.getDisableMessageID();
047        this.disableMessageTimestamp = messageProducer.getDisableMessageTimestamp();
048        this.priority = messageProducer.getPriority();
049        this.timeToLive = messageProducer.getTimeToLive();
050    }
051
052    @Override
053    public void close() throws JMSException {
054        if (!anonymous) {
055            this.messageProducer.close();
056        }
057    }
058
059    @Override
060    public void send(Destination destination, Message message) throws JMSException {
061        send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
062    }
063
064    @Override
065    public void send(Message message) throws JMSException {
066        send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
067    }
068
069    @Override
070    public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
071        send(destination, message, deliveryMode, priority, timeToLive);
072    }
073
074    @Override
075    public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
076
077        if (destination == null) {
078            if (messageProducer.getDestination() == null) {
079                throw new UnsupportedOperationException("A destination must be specified.");
080            }
081            throw new InvalidDestinationException("Don't understand null destinations");
082        }
083
084        MessageProducer messageProducer = getMessageProducer();
085
086        // just in case let only one thread send at once
087        synchronized (messageProducer) {
088
089            if (anonymous && this.destination != null && !this.destination.equals(destination)) {
090                throw new UnsupportedOperationException("This producer can only send messages to: " + this.destination);
091            }
092
093            // Producer will do it's own Destination validation so always use the destination
094            // based send method otherwise we might violate a JMS rule.
095            messageProducer.send(destination, message, deliveryMode, priority, timeToLive);
096        }
097    }
098
099    @Override
100    public Destination getDestination() {
101        return destination;
102    }
103
104    @Override
105    public int getDeliveryMode() {
106        return deliveryMode;
107    }
108
109    @Override
110    public void setDeliveryMode(int deliveryMode) {
111        this.deliveryMode = deliveryMode;
112    }
113
114    @Override
115    public boolean getDisableMessageID() {
116        return disableMessageID;
117    }
118
119    @Override
120    public void setDisableMessageID(boolean disableMessageID) {
121        this.disableMessageID = disableMessageID;
122    }
123
124    @Override
125    public boolean getDisableMessageTimestamp() {
126        return disableMessageTimestamp;
127    }
128
129    @Override
130    public void setDisableMessageTimestamp(boolean disableMessageTimestamp) {
131        this.disableMessageTimestamp = disableMessageTimestamp;
132    }
133
134    @Override
135    public int getPriority() {
136        return priority;
137    }
138
139    @Override
140    public void setPriority(int priority) {
141        this.priority = priority;
142    }
143
144    @Override
145    public long getTimeToLive() {
146        return timeToLive;
147    }
148
149    @Override
150    public void setTimeToLive(long timeToLive) {
151        this.timeToLive = timeToLive;
152    }
153
154    // Implementation methods
155    // -------------------------------------------------------------------------
156    protected MessageProducer getMessageProducer() {
157        return messageProducer;
158    }
159
160    protected boolean isAnonymous() {
161        return anonymous;
162    }
163
164    @Override
165    public String toString() {
166        return "PooledProducer { " + messageProducer + " }";
167    }
168}