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 java.util.Collections;
020    import java.util.List;
021    import javax.jms.JMSException;
022    import javax.jms.Queue;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.spi.BrowsableEndpoint;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.springframework.jms.core.JmsOperations;
029    import org.springframework.jmx.export.annotation.ManagedAttribute;
030    import org.springframework.jmx.export.annotation.ManagedOperation;
031    import org.springframework.jmx.export.annotation.ManagedResource;
032    
033    /**
034     * An endpoint for a JMS Queue which is also browsable
035     *
036     * @version $Revision: 16342 $
037     */
038    @ManagedResource(description = "Managed JMS Queue Endpoint")
039    public class JmsQueueEndpoint extends JmsEndpoint implements BrowsableEndpoint {
040        private static final transient Log LOG = LogFactory.getLog(JmsQueueEndpoint.class);
041    
042        private int maximumBrowseSize = -1;
043        private final QueueBrowseStrategy queueBrowseStrategy;
044    
045        public JmsQueueEndpoint(Queue destination) throws JMSException {
046            this("jms:queue:" + destination.getQueueName(), null);
047            setDestination(destination);
048        }
049        
050        public JmsQueueEndpoint(String uri, JmsComponent component, String destination,
051                JmsConfiguration configuration) {
052            this(uri, component, destination, configuration, null);
053        }
054    
055        public JmsQueueEndpoint(String uri, JmsComponent component, String destination,
056                JmsConfiguration configuration, QueueBrowseStrategy queueBrowseStrategy) {
057            super(uri, component, destination, false, configuration);
058            if (queueBrowseStrategy == null) {
059                this.queueBrowseStrategy = createQueueBrowseStrategy();
060            } else {
061                this.queueBrowseStrategy = queueBrowseStrategy;
062            }
063        }
064    
065        public JmsQueueEndpoint(String endpointUri, String destination, QueueBrowseStrategy queueBrowseStrategy) {
066            super(endpointUri, destination, false);
067            if (queueBrowseStrategy == null) {
068                this.queueBrowseStrategy = createQueueBrowseStrategy();
069            } else {
070                this.queueBrowseStrategy = queueBrowseStrategy;
071            }
072        }
073    
074        public JmsQueueEndpoint(String endpointUri, String destination) {
075            super(endpointUri, destination, false);
076            queueBrowseStrategy = createQueueBrowseStrategy();
077        }
078    
079        @ManagedAttribute
080        public int getMaximumBrowseSize() {
081            return maximumBrowseSize;
082        }
083    
084        /**
085         * If a number is set > 0 then this limits the number of messages that are
086         * returned when browsing the queue
087         */
088        @ManagedAttribute
089        public void setMaximumBrowseSize(int maximumBrowseSize) {
090            this.maximumBrowseSize = maximumBrowseSize;
091        }
092    
093        public List<Exchange> getExchanges() {
094            if (queueBrowseStrategy == null) {
095                return Collections.emptyList();
096            }
097            String queue = getDestinationName();
098            JmsOperations template = getConfiguration().createInOnlyTemplate(this, false, queue);
099            return queueBrowseStrategy.browse(template, queue, this);
100        }
101    
102        @ManagedOperation(description = "Current number of Exchanges in Queue")
103        public long qeueSize() {
104            return getExchanges().size();
105        }
106    
107        @ManagedOperation(description = "Get Exchange from queue by index")
108        public String browseExchange(Integer index) {
109            Exchange exchange = getExchanges().get(index);
110            if (exchange == null) {
111                return null;
112            }
113            // must use java type with JMX such as java.lang.String
114            return exchange.toString();
115        }
116    
117        protected QueueBrowseStrategy createQueueBrowseStrategy() {
118            QueueBrowseStrategy answer = null;
119            try {
120                answer = JmsComponent.tryCreateDefaultQueueBrowseStrategy(getCamelContext());
121            } catch (Throwable e) {
122                LOG.debug("Caught exception trying to create default QueueBrowseStrategy. "
123                          + "This could be due to spring 2.0.x on classpath? Cause: " + e, e);
124            }
125            if (answer == null) {
126                LOG.warn("Cannot browse queues as no QueueBrowseStrategy specified. Are you using Spring 2.0.x by any chance? If you upgrade to 2.5.x or later then queue browsing is supported");
127            }
128            return answer;
129        }
130    
131    }