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.camel.component.jms;
018    
019    import javax.jms.Destination;
020    import javax.jms.JMSException;
021    import javax.jms.Session;
022    import javax.jms.TemporaryQueue;
023    
024    /**
025     * A <a href="http://activemq.apache.org/jms.html">JMS Endpoint</a>
026     * for working with a {@link TemporaryQueue}
027     * <p/>
028     * <b>Important:</b> Need to be really careful to always use the same Connection otherwise the destination goes stale
029     *
030     * @version $Revision: 19492 $
031     */
032    public class JmsTemporaryQueueEndpoint extends JmsQueueEndpoint implements DestinationEndpoint {
033        private Destination jmsDestination;
034    
035        public JmsTemporaryQueueEndpoint(String uri, JmsComponent component, String destination, JmsConfiguration configuration) {
036            super(uri, component, destination, configuration);
037        }
038    
039        public JmsTemporaryQueueEndpoint(String uri, JmsComponent component, String destination, JmsConfiguration configuration, QueueBrowseStrategy queueBrowseStrategy) {
040            super(uri, component, destination, configuration, queueBrowseStrategy);
041        }
042    
043        public JmsTemporaryQueueEndpoint(String endpointUri, String destination) {
044            super(endpointUri, destination);
045        }
046    
047        public JmsTemporaryQueueEndpoint(TemporaryQueue jmsDestination) throws JMSException {
048            super("jms:temp:queue:" + jmsDestination.getQueueName(), null);
049            this.jmsDestination = jmsDestination;
050            setDestination(jmsDestination);
051        }
052    
053    
054        /**
055         * This endpoint is a singleton so that the temporary destination instances are shared across all
056         * producers and consumers of the same endpoint URI
057         *
058         * @return true
059         */
060        public boolean isSingleton() {
061            return true;
062        }
063        
064        @Override
065        public Object getManagedObject(JmsEndpoint object) {
066            // We don't want to manage this temporary object, so return null
067            return null;
068        }
069    
070        public synchronized Destination getJmsDestination(Session session) throws JMSException {
071            if (jmsDestination == null) {
072                jmsDestination = createJmsDestination(session);
073            }
074            return jmsDestination;
075        }
076    
077        protected Destination createJmsDestination(Session session) throws JMSException {
078            return session.createTemporaryQueue();
079        }
080    }