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.component.timer;
018
019 import java.util.Date;
020 import java.util.Timer;
021 import java.util.TimerTask;
022
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Processor;
025 import org.apache.camel.impl.DefaultConsumer;
026 import org.apache.camel.util.ExchangeHelper;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030 /**
031 * The timer consumer.
032 *
033 * @version $Revision: 891253 $
034 */
035 public class TimerConsumer extends DefaultConsumer {
036 private static final transient Log LOG = LogFactory.getLog(TimerConsumer.class);
037 private final TimerEndpoint endpoint;
038 private TimerTask task;
039
040 public TimerConsumer(TimerEndpoint endpoint, Processor processor) {
041 super(endpoint, processor);
042 this.endpoint = endpoint;
043 }
044
045 @Override
046 protected void doStart() throws Exception {
047 task = new TimerTask() {
048 @Override
049 public void run() {
050 sendTimerExchange();
051 }
052 };
053
054 Timer timer = endpoint.getTimer();
055 configureTask(task, timer);
056 }
057
058 @Override
059 protected void doStop() throws Exception {
060 task.cancel();
061 task = null;
062 }
063
064 protected void configureTask(TimerTask task, Timer timer) {
065 if (endpoint.isFixedRate()) {
066 if (endpoint.getTime() != null) {
067 timer.scheduleAtFixedRate(task, endpoint.getTime(), endpoint.getPeriod());
068 } else {
069 timer.scheduleAtFixedRate(task, endpoint.getDelay(), endpoint.getPeriod());
070 }
071 } else {
072 if (endpoint.getTime() != null) {
073 if (endpoint.getPeriod() > 0) {
074 timer.schedule(task, endpoint.getTime(), endpoint.getPeriod());
075 } else {
076 timer.schedule(task, endpoint.getTime());
077 }
078 } else {
079 if (endpoint.getPeriod() > 0) {
080 timer.schedule(task, endpoint.getDelay(), endpoint.getPeriod());
081 } else {
082 timer.schedule(task, endpoint.getDelay());
083 }
084 }
085 }
086 }
087
088 protected void sendTimerExchange() {
089 Exchange exchange = endpoint.createExchange();
090 exchange.setProperty(Exchange.TIMER_NAME, endpoint.getTimerName());
091 exchange.setProperty(Exchange.TIMER_TIME, endpoint.getTime());
092 exchange.setProperty(Exchange.TIMER_PERIOD, endpoint.getPeriod());
093
094 Date now = new Date();
095 exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
096 // also set now on in header with same key as quartz to be consistent
097 exchange.getIn().setHeader("firedTime", now);
098
099 if (LOG.isTraceEnabled()) {
100 LOG.trace("Timer " + endpoint.getTimerName() + " is firing");
101 }
102 try {
103 getProcessor().process(exchange);
104
105 // log exception if an exception occurred and was not handled
106 if (exchange.getException() != null) {
107 getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
108 }
109 } catch (Exception e) {
110 getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
111 }
112 }
113 }