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 */
017package org.apache.camel.impl;
018
019import java.util.concurrent.ScheduledExecutorService;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.Exchange;
023import org.apache.camel.PollingConsumer;
024import org.apache.camel.Processor;
025import 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 */
033public class DefaultScheduledPollConsumer extends ScheduledPollConsumer {
034    private PollingConsumer pollingConsumer;
035    private int timeout;
036
037    public DefaultScheduledPollConsumer(DefaultEndpoint defaultEndpoint, Processor processor) {
038        super(defaultEndpoint, processor);
039    }
040
041    public DefaultScheduledPollConsumer(Endpoint endpoint, Processor processor, ScheduledExecutorService executor) {
042        super(endpoint, processor, executor);
043    }
044
045    protected int poll() throws Exception {
046        int messagesPolled = 0;
047
048        while (isPollAllowed()) {
049            Exchange exchange;
050            if (timeout == 0) {
051                exchange = pollingConsumer.receiveNoWait();
052            } else if (timeout < 0) {
053                exchange = pollingConsumer.receive();
054            } else {
055                exchange = pollingConsumer.receive(timeout);
056            }
057
058            if (exchange == null) {
059                break;
060            }
061
062            messagesPolled++;
063            log.trace("Polled {} {}", messagesPolled, exchange);
064
065            // if the result of the polled exchange has output we should create a new exchange and
066            // use the output as input to the next processor
067            if (exchange.hasOut()) {
068                // lets create a new exchange
069                Exchange newExchange = getEndpoint().createExchange();
070                newExchange.getIn().copyFrom(exchange.getOut());
071                exchange = newExchange;
072            }
073            getProcessor().process(exchange);
074        }
075
076        return messagesPolled;
077    }
078
079    public int getTimeout() {
080        return timeout;
081    }
082
083    /**
084     * Sets a timeout to use with {@link PollingConsumer}.
085     * <br/>
086     * <br/>Use <tt>timeout < 0</tt> for {@link PollingConsumer#receive()}.
087     * <br/>Use <tt>timeout == 0</tt> for {@link PollingConsumer#receiveNoWait()}.
088     * <br/>Use <tt>timeout > 0</tt> for {@link PollingConsumer#receive(long)}}.
089     * <br/> The default timeout value is <tt>0</tt>
090     *
091     * @param timeout the timeout value
092     */
093    public void setTimeout(int timeout) {
094        this.timeout = timeout;
095    }
096
097    @Override
098    protected void doStart() throws Exception {
099        pollingConsumer = getEndpoint().createPollingConsumer();
100        ServiceHelper.startService(pollingConsumer);
101        super.doStart();
102    }
103
104    @Override
105    protected void doStop() throws Exception {
106        ServiceHelper.stopService(pollingConsumer);
107        super.doStop();
108    }
109}