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    
018    package org.apache.activemq;
019    
020    import javax.jms.JMSException;
021    import javax.jms.Message;
022    import javax.jms.Topic;
023    import javax.jms.TopicPublisher;
024    
025    import org.apache.activemq.command.ActiveMQDestination;
026    
027    /**
028     * A client uses a <CODE>TopicPublisher</CODE> object to publish messages on
029     * a topic. A <CODE>TopicPublisher</CODE> object is the publish-subscribe
030     * form of a message producer.
031     * <p/>
032     * <P>
033     * Normally, the <CODE>Topic</CODE> is specified when a <CODE>TopicPublisher
034     * </CODE> is created. In this case, an attempt to use the <CODE>publish
035     * </CODE> methods for an unidentified <CODE>TopicPublisher</CODE> will throw
036     * a <CODE>java.lang.UnsupportedOperationException</CODE>.
037     * <p/>
038     * <P>
039     * If the <CODE>TopicPublisher</CODE> is created with an unidentified <CODE>
040     * Topic</CODE>, an attempt to use the <CODE>publish</CODE> methods that
041     * assume that the <CODE>Topic</CODE> has been identified will throw a <CODE>
042     * java.lang.UnsupportedOperationException</CODE>.
043     * <p/>
044     * <P>
045     * During the execution of its <CODE>publish</CODE> method, a message must
046     * not be changed by other threads within the client. If the message is
047     * modified, the result of the <CODE>publish</CODE> is undefined.
048     * <p/>
049     * <P>
050     * After publishing a message, a client may retain and modify it without
051     * affecting the message that has been published. The same message object may
052     * be published multiple times.
053     * <p/>
054     * <P>
055     * The following message headers are set as part of publishing a message:
056     * <code>JMSDestination</code>,<code>JMSDeliveryMode</code>,<code>JMSExpiration</code>,
057     * <code>JMSPriority</code>,<code>JMSMessageID</code> and <code>JMSTimeStamp</code>.
058     * When the message is published, the values of these headers are ignored.
059     * After completion of the <CODE>publish</CODE>, the headers hold the values
060     * specified by the method publishing the message. It is possible for the
061     * <CODE>publish</CODE> method not to set <code>JMSMessageID</code> and
062     * <code>JMSTimeStamp</code> if the setting of these headers is explicitly
063     * disabled by the <code>MessageProducer.setDisableMessageID</code> or <code>MessageProducer.setDisableMessageTimestamp</code>
064     * method.
065     * <p/>
066     * <P>
067     * Creating a <CODE>MessageProducer</CODE> provides the same features as
068     * creating a <CODE>TopicPublisher</CODE>. A <CODE>MessageProducer</CODE>
069     * object is recommended when creating new code. The <CODE>TopicPublisher
070     * </CODE> is provided to support existing code.
071     * <p/>
072     * <p/>
073     * <P>
074     * Because <CODE>TopicPublisher</CODE> inherits from <CODE>MessageProducer
075     * </CODE>, it inherits the <CODE>send</CODE> methods that are a part of the
076     * <CODE>MessageProducer</CODE> interface. Using the <CODE>send</CODE>
077     * methods will have the same effect as using the <CODE>publish</CODE>
078     * methods: they are functionally the same.
079     *
080     * @see Session#createProducer(Destination)
081     * @see TopicSession#createPublisher(Topic)
082     */
083    
084    public class ActiveMQTopicPublisher extends ActiveMQMessageProducer implements
085            TopicPublisher {
086    
087        protected ActiveMQTopicPublisher(ActiveMQSession session,
088                                         ActiveMQDestination destination, int sendTimeout) throws JMSException {
089            super(session, session.getNextProducerId(), destination,sendTimeout);
090        }
091    
092        /**
093         * Gets the topic associated with this <CODE>TopicPublisher</CODE>.
094         *
095         * @return this publisher's topic
096         * @throws JMSException if the JMS provider fails to get the topic for this
097         *                      <CODE>TopicPublisher</CODE> due to some internal error.
098         */
099    
100        public Topic getTopic() throws JMSException {
101            return (Topic) super.getDestination();
102        }
103    
104        /**
105         * Publishes a message to the topic. Uses the <CODE>TopicPublisher</CODE>'s
106         * default delivery mode, priority, and time to live.
107         *
108         * @param message the message to publish
109         * @throws JMSException                if the JMS provider fails to publish the message due to
110         *                                     some internal error.
111         * @throws MessageFormatException      if an invalid message is specified.
112         * @throws InvalidDestinationException if a client uses this method with a <CODE>TopicPublisher
113         *                                     </CODE> with an invalid topic.
114         * @throws java.lang.UnsupportedOperationException
115         *                                     if a client uses this method with a <CODE>TopicPublisher
116         *                                     </CODE> that did not specify a topic at creation time.
117         * @see javax.jms.MessageProducer#getDeliveryMode()
118         * @see javax.jms.MessageProducer#getTimeToLive()
119         * @see javax.jms.MessageProducer#getPriority()
120         */
121    
122        public void publish(Message message) throws JMSException {
123            super.send(message);
124        }
125    
126        /**
127         * Publishes a message to the topic, specifying delivery mode, priority,
128         * and time to live.
129         *
130         * @param message      the message to publish
131         * @param deliveryMode the delivery mode to use
132         * @param priority     the priority for this message
133         * @param timeToLive   the message's lifetime (in milliseconds)
134         * @throws JMSException                if the JMS provider fails to publish the message due to
135         *                                     some internal error.
136         * @throws MessageFormatException      if an invalid message is specified.
137         * @throws InvalidDestinationException if a client uses this method with a <CODE>TopicPublisher
138         *                                     </CODE> with an invalid topic.
139         * @throws java.lang.UnsupportedOperationException
140         *                                     if a client uses this method with a <CODE>TopicPublisher
141         *                                     </CODE> that did not specify a topic at creation time.
142         */
143    
144        public void publish(Message message, int deliveryMode, int priority,
145                            long timeToLive) throws JMSException {
146            super.send(message, deliveryMode, priority, timeToLive);
147        }
148    
149        /**
150         * Publishes a message to a topic for an unidentified message producer.
151         * Uses the <CODE>TopicPublisher</CODE>'s default delivery mode,
152         * priority, and time to live.
153         * <p/>
154         * <P>
155         * Typically, a message producer is assigned a topic at creation time;
156         * however, the JMS API also supports unidentified message producers, which
157         * require that the topic be supplied every time a message is published.
158         *
159         * @param topic   the topic to publish this message to
160         * @param message the message to publish
161         * @throws JMSException                if the JMS provider fails to publish the message due to
162         *                                     some internal error.
163         * @throws MessageFormatException      if an invalid message is specified.
164         * @throws InvalidDestinationException if a client uses this method with an invalid topic.
165         * @see javax.jms.MessageProducer#getDeliveryMode()
166         * @see javax.jms.MessageProducer#getTimeToLive()
167         * @see javax.jms.MessageProducer#getPriority()
168         */
169    
170        public void publish(Topic topic, Message message) throws JMSException {
171            super.send(topic, message);
172        }
173    
174        /**
175         * Publishes a message to a topic for an unidentified message producer,
176         * specifying delivery mode, priority and time to live.
177         * <p/>
178         * <P>
179         * Typically, a message producer is assigned a topic at creation time;
180         * however, the JMS API also supports unidentified message producers, which
181         * require that the topic be supplied every time a message is published.
182         *
183         * @param topic        the topic to publish this message to
184         * @param message      the message to publish
185         * @param deliveryMode the delivery mode to use
186         * @param priority     the priority for this message
187         * @param timeToLive   the message's lifetime (in milliseconds)
188         * @throws JMSException                if the JMS provider fails to publish the message due to
189         *                                     some internal error.
190         * @throws MessageFormatException      if an invalid message is specified.
191         * @throws InvalidDestinationException if a client uses this method with an invalid topic.
192         */
193    
194        public void publish(Topic topic, Message message, int deliveryMode,
195                            int priority, long timeToLive) throws JMSException {
196            super.send(topic, message, deliveryMode, priority, timeToLive);
197        }
198    }