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.component.scheduler;
018
019import java.util.Date;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.Exchange;
023import org.apache.camel.Processor;
024import org.apache.camel.impl.ScheduledPollConsumer;
025
026public class SchedulerConsumer extends ScheduledPollConsumer {
027
028    public SchedulerConsumer(SchedulerEndpoint endpoint, Processor processor) {
029        super(endpoint, processor);
030    }
031
032    @Override
033    public SchedulerEndpoint getEndpoint() {
034        return (SchedulerEndpoint) super.getEndpoint();
035    }
036
037    @Override
038    protected int poll() throws Exception {
039        return sendTimerExchange();
040    }
041
042    protected int sendTimerExchange() {
043        final Exchange exchange = getEndpoint().createExchange();
044        exchange.setProperty(Exchange.TIMER_NAME, getEndpoint().getName());
045
046        Date now = new Date();
047        exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
048
049        if (log.isTraceEnabled()) {
050            log.trace("Timer {} is firing", getEndpoint().getName());
051        }
052
053        if (!getEndpoint().isSynchronous()) {
054            getAsyncProcessor().process(exchange, new AsyncCallback() {
055                @Override
056                public void done(boolean doneSync) {
057                    // handle any thrown exception
058                    if (exchange.getException() != null) {
059                        getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
060                    }
061                }
062            });
063        } else {
064            try {
065                getProcessor().process(exchange);
066            } catch (Exception e) {
067                exchange.setException(e);
068            }
069
070            // handle any thrown exception
071            if (exchange.getException() != null) {
072                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
073            }
074        }
075
076        // a property can be used to control if the scheduler polled a message or not
077        // for example to overrule and indicate no message was polled, which can affect the scheduler
078        // to leverage backoff on idle etc.
079        boolean polled = exchange.getProperty(Exchange.SCHEDULER_POLLED_MESSAGES, true, boolean.class);
080        return polled ? 1 : 0;
081    }
082
083    @Override
084    protected void doStart() throws Exception {
085        getEndpoint().onConsumerStart(this);
086
087        super.doStart();
088    }
089
090    @Override
091    protected void doStop() throws Exception {
092        getEndpoint().onConsumerStop(this);
093
094        super.doStop();
095    }
096}