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.impl.PollingConsumerSupport;
022    
023    import org.springframework.jms.core.JmsOperations;
024    import org.springframework.jms.core.JmsTemplate;
025    import org.springframework.jms.core.JmsTemplate102;
026    
027    /**
028     * @version $Revision: 1.1 $
029     */
030    public class JmsPollingConsumer extends PollingConsumerSupport<JmsExchange> {
031        private JmsOperations template;
032    
033        public JmsPollingConsumer(JmsEndpoint endpoint, JmsOperations template) {
034            super(endpoint);
035            this.template = template;
036        }
037    
038        @Override
039        public JmsEndpoint getEndpoint() {
040            return (JmsEndpoint)super.getEndpoint();
041        }
042    
043        public JmsExchange receiveNoWait() {
044            return receive(0);
045        }
046    
047        public JmsExchange receive() {
048            return receive(-1);
049        }
050    
051        public JmsExchange receive(long timeout) {
052            setReceiveTimeout(timeout);
053            Message message = template.receive();
054            if (message != null) {
055                return getEndpoint().createExchange(message);
056            }
057            return null;
058        }
059    
060        protected void doStart() throws Exception {
061        }
062    
063        protected void doStop() throws Exception {
064        }
065    
066        protected void setReceiveTimeout(long timeout) {
067            if (template instanceof JmsTemplate) {
068                JmsTemplate jmsTemplate = (JmsTemplate)template;
069                jmsTemplate.setReceiveTimeout(timeout);
070            } else if (template instanceof JmsTemplate102) {
071                JmsTemplate102 jmsTemplate102 = (JmsTemplate102)template;
072                jmsTemplate102.setReceiveTimeout(timeout);
073            } else {
074                throw new IllegalArgumentException("Cannot set the receiveTimeout property on unknown JmsOperations type: " + template);
075            }
076        }
077    }