001    /** 
002     * Licensed under the Apache License, Version 2.0 (the "License"); 
003     * you may not use this file except in compliance with the License. 
004     * You may obtain a copy of the License at 
005     * 
006     * http://www.apache.org/licenses/LICENSE-2.0
007     * 
008     * Unless required by applicable law or agreed to in writing, software
009     * distributed under the License is distributed on an "AS IS" BASIS, 
010     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
011     * See the License for the specific language governing permissions and 
012     * limitations under the License. 
013     * 
014     **/
015    package org.jencks.pool;
016    
017    import javax.jms.MessageProducer;
018    import javax.jms.Destination;
019    import javax.jms.JMSException;
020    import javax.jms.Message;
021    
022    /**
023     * A pooled {@link javax.jms.MessageProducer}
024     *
025     * @version $Revision$
026     */
027    public class PooledProducer implements MessageProducer
028    {
029      private MessageProducer messageProducer;
030      private Destination destination;
031      private int deliveryMode;
032      private boolean disableMessageID;
033      private boolean disableMessageTimestamp;
034      private int priority;
035      private long timeToLive;
036    
037      public PooledProducer(final MessageProducer messageProducer,
038                            final Destination destination) throws JMSException
039      {
040        this.messageProducer = messageProducer;
041        this.destination = destination;
042    
043        this.deliveryMode = messageProducer.getDeliveryMode();
044        this.disableMessageID = messageProducer.getDisableMessageID();
045        this.disableMessageTimestamp = messageProducer.getDisableMessageTimestamp();
046        this.priority = messageProducer.getPriority();
047        this.timeToLive = messageProducer.getTimeToLive();
048      }
049    
050      public void close() throws JMSException
051      {
052      }
053    
054      public void send(final Destination destination, final Message message)
055          throws JMSException
056      {
057        send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
058      }
059    
060      public void send(final Message message) throws JMSException
061      {
062        send(this.destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
063      }
064    
065      public void send(final Message message, final int deliveryMode, final int priority,
066                       final long timeToLive) throws JMSException
067      {
068        send(this.destination, message, deliveryMode, priority, timeToLive);
069      }
070    
071      public void send(Destination destination, final Message message, final int deliveryMode,
072                       final int priority, final long timeToLive) throws JMSException
073      {
074        if (destination == null) {
075          destination = this.destination;
076        }
077        MessageProducer messageProducer = getMessageProducer();
078    
079        // just in case let only one thread send at once
080        synchronized (messageProducer) {
081          messageProducer.send(destination, message, deliveryMode, priority, timeToLive);
082        }
083      }
084    
085      public Destination getDestination()
086      {
087        return this.destination;
088      }
089    
090      public int getDeliveryMode()
091      {
092        return this.deliveryMode;
093      }
094    
095      public void setDeliveryMode(final int deliveryMode)
096      {
097        this.deliveryMode = deliveryMode;
098      }
099    
100      public boolean getDisableMessageID()
101      {
102        return this.disableMessageID;
103      }
104    
105      public void setDisableMessageID(final boolean disableMessageID)
106      {
107        this.disableMessageID = disableMessageID;
108      }
109    
110      public boolean getDisableMessageTimestamp()
111      {
112        return this.disableMessageTimestamp;
113      }
114    
115      public void setDisableMessageTimestamp(final boolean disableMessageTimestamp)
116      {
117        this.disableMessageTimestamp = disableMessageTimestamp;
118      }
119    
120      public int getPriority()
121      {
122        return this.priority;
123      }
124    
125      public void setPriority(final int priority)
126      {
127        this.priority = priority;
128      }
129    
130      public long getTimeToLive()
131      {
132        return this.timeToLive;
133      }
134    
135      public void setTimeToLive(final long timeToLive)
136      {
137        this.timeToLive = timeToLive;
138      }
139    
140      // Implementation methods
141      //-------------------------------------------------------------------------
142      protected MessageProducer getMessageProducer()
143      {
144        return this.messageProducer;
145      }
146    }