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.timer; 018 019import java.util.Date; 020import java.util.Timer; 021 022import org.apache.camel.Component; 023import org.apache.camel.Consumer; 024import org.apache.camel.MultipleConsumersSupport; 025import org.apache.camel.Processor; 026import org.apache.camel.Producer; 027import org.apache.camel.RuntimeCamelException; 028import org.apache.camel.api.management.ManagedAttribute; 029import org.apache.camel.api.management.ManagedResource; 030import org.apache.camel.impl.DefaultEndpoint; 031import org.apache.camel.spi.Metadata; 032import org.apache.camel.spi.UriEndpoint; 033import org.apache.camel.spi.UriParam; 034import org.apache.camel.spi.UriPath; 035 036/** 037 * The timer component is used for generating message exchanges when a timer fires. 038 * 039 * This component is similar to the scheduler component, but has much less functionality. 040 */ 041@ManagedResource(description = "Managed TimerEndpoint") 042@UriEndpoint(scheme = "timer", title = "Timer", syntax = "timer:timerName", consumerOnly = true, consumerClass = TimerConsumer.class, label = "core,scheduling") 043public class TimerEndpoint extends DefaultEndpoint implements MultipleConsumersSupport { 044 @UriPath @Metadata(required = "true") 045 private String timerName; 046 @UriParam(defaultValue = "1000") 047 private long period = 1000; 048 @UriParam(defaultValue = "1000") 049 private long delay = 1000; 050 @UriParam(defaultValue = "0") 051 private long repeatCount; 052 @UriParam 053 private boolean fixedRate; 054 @UriParam(defaultValue = "true", label = "advanced") 055 private boolean daemon = true; 056 @UriParam(label = "advanced") 057 private Date time; 058 @UriParam(label = "advanced") 059 private String pattern; 060 @UriParam(label = "advanced") 061 private Timer timer; 062 063 public TimerEndpoint() { 064 } 065 066 public TimerEndpoint(String uri, Component component, String timerName) { 067 super(uri, component); 068 this.timerName = timerName; 069 } 070 071 protected TimerEndpoint(String endpointUri, Component component) { 072 super(endpointUri, component); 073 } 074 075 @Override 076 public TimerComponent getComponent() { 077 return (TimerComponent) super.getComponent(); 078 } 079 080 public Producer createProducer() throws Exception { 081 throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri()); 082 } 083 084 public Consumer createConsumer(Processor processor) throws Exception { 085 Consumer answer = new TimerConsumer(this, processor); 086 configureConsumer(answer); 087 return answer; 088 } 089 090 @Override 091 public boolean isSingleton() { 092 return true; 093 } 094 095 @Override 096 protected void doStart() throws Exception { 097 super.doStart(); 098 // do nothing, the timer will be set when the first consumer will request it 099 } 100 101 @Override 102 protected void doStop() throws Exception { 103 setTimer(null); 104 super.doStop(); 105 } 106 107 @ManagedAttribute 108 public boolean isMultipleConsumersSupported() { 109 return true; 110 } 111 112 @ManagedAttribute(description = "Timer Name") 113 public String getTimerName() { 114 if (timerName == null) { 115 timerName = getEndpointUri(); 116 } 117 return timerName; 118 } 119 120 /** 121 * The name of the timer 122 */ 123 @ManagedAttribute(description = "Timer Name") 124 public void setTimerName(String timerName) { 125 this.timerName = timerName; 126 } 127 128 @ManagedAttribute(description = "Timer Daemon") 129 public boolean isDaemon() { 130 return daemon; 131 } 132 133 /** 134 * Specifies whether or not the thread associated with the timer endpoint runs as a daemon. 135 * <p/> 136 * The default value is true. 137 */ 138 @ManagedAttribute(description = "Timer Daemon") 139 public void setDaemon(boolean daemon) { 140 this.daemon = daemon; 141 } 142 143 @ManagedAttribute(description = "Timer Delay") 144 public long getDelay() { 145 return delay; 146 } 147 148 /** 149 * The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time option. 150 * <p/> 151 * The default value is 1000. 152 */ 153 @ManagedAttribute(description = "Timer Delay") 154 public void setDelay(long delay) { 155 this.delay = delay; 156 } 157 158 @ManagedAttribute(description = "Timer FixedRate") 159 public boolean isFixedRate() { 160 return fixedRate; 161 } 162 163 /** 164 * Events take place at approximately regular intervals, separated by the specified period. 165 */ 166 @ManagedAttribute(description = "Timer FixedRate") 167 public void setFixedRate(boolean fixedRate) { 168 this.fixedRate = fixedRate; 169 } 170 171 @ManagedAttribute(description = "Timer Period") 172 public long getPeriod() { 173 return period; 174 } 175 176 /** 177 * If greater than 0, generate periodic events every period milliseconds. 178 * <p/> 179 * The default value is 1000. 180 */ 181 @ManagedAttribute(description = "Timer Period") 182 public void setPeriod(long period) { 183 this.period = period; 184 } 185 186 @ManagedAttribute(description = "Repeat Count") 187 public long getRepeatCount() { 188 return repeatCount; 189 } 190 191 /** 192 * Specifies a maximum limit of number of fires. 193 * So if you set it to 1, the timer will only fire once. 194 * If you set it to 5, it will only fire five times. 195 * A value of zero or negative means fire forever. 196 */ 197 @ManagedAttribute(description = "Repeat Count") 198 public void setRepeatCount(long repeatCount) { 199 this.repeatCount = repeatCount; 200 } 201 202 public Date getTime() { 203 return time; 204 } 205 206 /** 207 * A java.util.Date the first event should be generated. If using the URI, the pattern expected is: yyyy-MM-dd HH:mm:ss or yyyy-MM-dd'T'HH:mm:ss. 208 */ 209 public void setTime(Date time) { 210 this.time = time; 211 } 212 213 public String getPattern() { 214 return pattern; 215 } 216 217 /** 218 * Allows you to specify a custom Date pattern to use for setting the time option using URI syntax. 219 */ 220 public void setPattern(String pattern) { 221 this.pattern = pattern; 222 } 223 224 public Timer getTimer(TimerConsumer consumer) { 225 if (timer != null) { 226 // use custom timer 227 return timer; 228 } 229 return getComponent().getTimer(consumer); 230 } 231 232 /** 233 * To use a custom {@link Timer} 234 */ 235 public void setTimer(Timer timer) { 236 this.timer = timer; 237 } 238 239 public void removeTimer(TimerConsumer consumer) { 240 if (timer == null) { 241 // only remove timer if we are not using a custom timer 242 getComponent().removeTimer(consumer); 243 } 244 } 245 246}