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.Timer;
020 import java.util.TimerTask;
021
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Processor;
024 import org.apache.camel.impl.DefaultConsumer;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027
028 /**
029 * The timer consumer.
030 *
031 * @version $Revision: 659782 $
032 */
033 public class TimerConsumer extends DefaultConsumer<Exchange> {
034 private static final transient Log LOG = LogFactory.getLog(TimerConsumer.class);
035 private final TimerEndpoint endpoint;
036 private TimerTask task;
037
038 public TimerConsumer(TimerEndpoint endpoint, Processor processor) {
039 super(endpoint, processor);
040 this.endpoint = endpoint;
041 }
042
043 @Override
044 protected void doStart() throws Exception {
045 task = new TimerTask() {
046 @Override
047 public void run() {
048 sendTimerExchange();
049 }
050 };
051
052 Timer timer = endpoint.getTimer();
053 configureTask(task, timer);
054 }
055
056 @Override
057 protected void doStop() throws Exception {
058 task.cancel();
059 }
060
061 protected void configureTask(TimerTask task, Timer timer) {
062 if (endpoint.isFixedRate()) {
063 if (endpoint.getTime() != null) {
064 timer.scheduleAtFixedRate(task, endpoint.getTime(), endpoint.getPeriod());
065 } else {
066 timer.scheduleAtFixedRate(task, endpoint.getDelay(), endpoint.getPeriod());
067 }
068 } else {
069 if (endpoint.getTime() != null) {
070 if (endpoint.getPeriod() >= 0) {
071 timer.schedule(task, endpoint.getTime(), endpoint.getPeriod());
072 } else {
073 timer.schedule(task, endpoint.getTime());
074 }
075 } else {
076 if (endpoint.getPeriod() >= 0) {
077 timer.schedule(task, endpoint.getDelay(), endpoint.getPeriod());
078 } else {
079 timer.schedule(task, endpoint.getDelay());
080 }
081 }
082 }
083 }
084
085 protected void sendTimerExchange() {
086 Exchange exchange = endpoint.createExchange();
087 exchange.setProperty("org.apache.camel.timer.name", endpoint.getTimerName());
088 exchange.setProperty("org.apache.camel.timer.time", endpoint.getTime());
089 exchange.setProperty("org.apache.camel.timer.period", endpoint.getPeriod());
090 try {
091 getProcessor().process(exchange);
092 } catch (Exception e) {
093 LOG.error("Caught: " + e, e);
094 }
095 }
096 }