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