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.text.SimpleDateFormat; 020 import java.util.Collection; 021 import java.util.Date; 022 import java.util.HashMap; 023 import java.util.Map; 024 import java.util.Timer; 025 026 import org.apache.camel.Endpoint; 027 import org.apache.camel.impl.DefaultComponent; 028 029 /** 030 * Represents the component that manages {@link TimerEndpoint}. It holds the 031 * list of {@link TimerConsumer} objects that are started. 032 * 033 * @version 034 */ 035 public class TimerComponent extends DefaultComponent { 036 private final Map<String, Timer> timers = new HashMap<String, Timer>(); 037 038 public Timer getTimer(TimerEndpoint endpoint) { 039 String key = endpoint.getTimerName(); 040 if (!endpoint.isDaemon()) { 041 key = "nonDaemon:" + key; 042 } 043 044 Timer answer; 045 synchronized (timers) { 046 answer = timers.get(key); 047 if (answer == null) { 048 // the timer name is also the thread name, so lets resolve a name to be used 049 String name = endpoint.getCamelContext().getExecutorServiceManager().resolveThreadName("timer://" + endpoint.getTimerName()); 050 answer = new Timer(name, endpoint.isDaemon()); 051 timers.put(key, answer); 052 } 053 } 054 return answer; 055 } 056 057 @Override 058 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { 059 TimerEndpoint answer = new TimerEndpoint(uri, this, remaining); 060 061 // convert time from String to a java.util.Date using the supported patterns 062 String time = getAndRemoveParameter(parameters, "time", String.class); 063 String pattern = getAndRemoveParameter(parameters, "pattern", String.class); 064 if (time != null) { 065 SimpleDateFormat sdf; 066 if (pattern != null) { 067 sdf = new SimpleDateFormat(pattern); 068 } else if (time.contains("T")) { 069 sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 070 } else { 071 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 072 } 073 Date date = sdf.parse(time); 074 answer.setTime(date); 075 } 076 077 setProperties(answer, parameters); 078 return answer; 079 } 080 081 @Override 082 protected void doStop() throws Exception { 083 Collection<Timer> collection = timers.values(); 084 for (Timer timer : collection) { 085 timer.cancel(); 086 } 087 timers.clear(); 088 } 089 }