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