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