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 java.util.concurrent.ScheduledExecutorService;
020
021 import org.apache.camel.Endpoint;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Message;
024 import org.apache.camel.PollingConsumer;
025 import org.apache.camel.Processor;
026
027 /**
028 * A default implementation of an event driven {@link org.apache.camel.Consumer} which uses the
029 * {@link PollingConsumer}
030 *
031 * @version $Revision: 769448 $
032 */
033 public class DefaultScheduledPollConsumer extends ScheduledPollConsumer {
034 private PollingConsumer pollingConsumer;
035
036 public DefaultScheduledPollConsumer(DefaultEndpoint defaultEndpoint, Processor processor) {
037 super(defaultEndpoint, processor);
038 }
039
040 public DefaultScheduledPollConsumer(Endpoint endpoint, Processor processor, ScheduledExecutorService executor) {
041 super(endpoint, processor, executor);
042 }
043
044 protected void poll() throws Exception {
045 while (true) {
046 Exchange exchange = pollingConsumer.receiveNoWait();
047 if (exchange == null) {
048 break;
049 }
050
051 // if the result of the polled exchange has output we should create a new exchange and
052 // use the output as input to the next processor
053 if (exchange.hasOut()) {
054 // lets create a new exchange
055 Exchange newExchange = getEndpoint().createExchange();
056 newExchange.getIn().copyFrom(exchange.getOut());
057 exchange = newExchange;
058 }
059 getProcessor().process(exchange);
060 }
061 }
062
063 @Override
064 protected void doStart() throws Exception {
065 pollingConsumer = getEndpoint().createPollingConsumer();
066 super.doStart();
067 }
068
069 @Override
070 protected void doStop() throws Exception {
071 super.doStop();
072 if (pollingConsumer != null) {
073 pollingConsumer.stop();
074 }
075 }
076 }