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.impl;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Processor;
022 import org.apache.camel.RuntimeExchangeException;
023 import org.apache.camel.util.ServiceHelper;
024
025 /**
026 * A simple implementation of {@link org.apache.camel.PollingConsumer} which just uses
027 * a {@link Processor}. This implementation does not support timeout based
028 * receive methods such as {@link #receive(long)}
029 *
030 * @version $Revision: 751655 $
031 */
032 public class ProcessorPollingConsumer extends PollingConsumerSupport {
033 private Processor processor;
034
035 public ProcessorPollingConsumer(Endpoint endpoint, Processor processor) {
036 super(endpoint);
037 this.processor = processor;
038 }
039
040 protected void doStart() throws Exception {
041 ServiceHelper.startService(processor);
042 }
043
044 protected void doStop() throws Exception {
045 ServiceHelper.stopService(processor);
046 }
047
048 public Exchange receive() {
049 Exchange exchange = getEndpoint().createExchange();
050 try {
051 processor.process(exchange);
052 } catch (Exception e) {
053 throw new RuntimeExchangeException(e, exchange);
054 }
055 return exchange;
056 }
057
058 public Exchange receiveNoWait() {
059 return receive();
060 }
061
062 public Exchange receive(long timeout) {
063 return receive();
064 }
065 }