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.Message;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.impl.PollingConsumerSupport;
023    import org.springframework.jms.core.JmsOperations;
024    import org.springframework.jms.core.JmsTemplate;
025    
026    /**
027     * @version $Revision: 18227 $
028     */
029    public class JmsPollingConsumer extends PollingConsumerSupport {
030        private JmsOperations template;
031        private JmsEndpoint jmsEndpoint;
032        private final boolean spring20x;
033    
034        public JmsPollingConsumer(JmsEndpoint endpoint, JmsOperations template) {
035            super(endpoint);
036            this.jmsEndpoint = endpoint;
037            this.template = template;
038            this.spring20x = JmsHelper.isSpring20x();
039        }
040    
041        @Override
042        public JmsEndpoint getEndpoint() {
043            return (JmsEndpoint)super.getEndpoint();
044        }
045    
046        public Exchange receiveNoWait() {
047            // spring have changed the semantic of the receive timeout mode
048            // so we need to determine if running spring 2.0.x or 2.5.x or newer
049            if (spring20x) {
050                // spring 2.0.x
051                return receive(0L);
052            } else {
053                // spring 2.5.x
054                // no wait using -1L does not work properly so wait at most 1 millis to simulate no wait
055                return receive(1);
056            }
057        }
058    
059        public Exchange receive() {
060            // spring have changed the semantic of the receive timeout mode
061            // so we need to determine if running spring 2.0.x or 2.5.x or newer
062            if (spring20x) {
063                // spring 2.0.x
064                return receive(-1L);
065            } else {
066                // spring 2.5.x
067                return receive(0L);
068            }
069        }
070    
071        public Exchange receive(long timeout) {
072            setReceiveTimeout(timeout);
073            Message message = null;
074            // using the selector
075            if (jmsEndpoint.getSelector() != null && jmsEndpoint.getSelector().length() > 0) {
076                message = template.receiveSelected(jmsEndpoint.getSelector());
077            } else {
078                message = template.receive();
079            }
080            if (message != null) {
081                return getEndpoint().createExchange(message);
082            }
083            return null;
084        }
085    
086        protected void doStart() throws Exception {
087        }
088    
089        protected void doStop() throws Exception {
090        }
091    
092        protected void setReceiveTimeout(long timeout) {
093            if (template instanceof JmsTemplate) {
094                JmsTemplate jmsTemplate = (JmsTemplate)template;
095                jmsTemplate.setReceiveTimeout(timeout);
096            } else {
097                throw new IllegalArgumentException("Cannot set the receiveTimeout property on unknown JmsOperations type: " + template.getClass().getName());
098            }
099        }
100    }