001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.jms;
019
020 import org.apache.camel.impl.PollingConsumerSupport;
021 import org.springframework.jms.core.JmsOperations;
022 import org.springframework.jms.core.JmsTemplate;
023 import org.springframework.jms.core.JmsTemplate102;
024
025 import javax.jms.Message;
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 }
071 else if (template instanceof JmsTemplate102) {
072 JmsTemplate102 jmsTemplate102 = (JmsTemplate102) template;
073 jmsTemplate102.setReceiveTimeout(timeout);
074 }
075 else {
076 throw new IllegalArgumentException("Cannot set the receiveTimeout property on unknown JmsOperations type: " + template);
077 }
078 }
079 }