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    import org.apache.camel.util.ServiceHelper;
026    
027    /**
028     * A default implementation of an event driven {@link org.apache.camel.Consumer} which uses the
029     * {@link PollingConsumer}
030     *
031     * @version 
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 int poll() throws Exception {
045            int messagesPolled = 0;
046    
047            while (isPollAllowed()) {
048                Exchange exchange = pollingConsumer.receiveNoWait();
049                if (exchange == null) {
050                    break;
051                }
052    
053                messagesPolled++;
054                log.trace("Polled {} {}", messagesPolled, exchange);
055    
056                // if the result of the polled exchange has output we should create a new exchange and
057                // use the output as input to the next processor
058                if (exchange.hasOut()) {
059                    // lets create a new exchange
060                    Exchange newExchange = getEndpoint().createExchange();
061                    newExchange.getIn().copyFrom(exchange.getOut());
062                    exchange = newExchange;
063                }
064                getProcessor().process(exchange);
065            }
066    
067            return messagesPolled;
068        }
069    
070        @Override
071        protected void doStart() throws Exception {
072            pollingConsumer = getEndpoint().createPollingConsumer();
073            ServiceHelper.startService(pollingConsumer);
074            super.doStart();
075        }
076    
077        @Override
078        protected void doStop() throws Exception {
079            ServiceHelper.stopService(pollingConsumer);
080            super.doStop();
081        }
082    }