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", description = "If greater than 0, generate periodic events every period milliseconds."
047            + " You can also specify time values using units, such as 60s (60 seconds), 5m30s (5 minutes and 30 seconds), and 1h (1 hour).")
048    private long period = 1000;
049    @UriParam(defaultValue = "1000", description = "Miliseconds before first event is triggered."
050            + " You can also specify time values using units, such as 60s (60 seconds), 5m30s (5 minutes and 30 seconds), and 1h (1 hour).")
051    private long delay = 1000;
052    @UriParam(defaultValue = "0")
053    private long repeatCount;
054    @UriParam
055    private boolean fixedRate;
056    @UriParam(defaultValue = "true", label = "advanced")
057    private boolean daemon = true;
058    @UriParam(label = "advanced")
059    private Date time;
060    @UriParam(label = "advanced")
061    private String pattern;
062    @UriParam(label = "advanced")
063    private Timer timer;
064
065    public TimerEndpoint() {
066    }
067
068    public TimerEndpoint(String uri, Component component, String timerName) {
069        super(uri, component);
070        this.timerName = timerName;
071    }
072    
073    protected TimerEndpoint(String endpointUri, Component component) {
074        super(endpointUri, component);
075    }
076
077    @Override
078    public TimerComponent getComponent() {
079        return (TimerComponent) super.getComponent();
080    }
081
082    public Producer createProducer() throws Exception {
083        throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri());
084    }
085
086    public Consumer createConsumer(Processor processor) throws Exception {
087        Consumer answer = new TimerConsumer(this, processor);
088        configureConsumer(answer);
089        return answer;
090    }
091
092    @Override
093    public boolean isSingleton() {
094        return true;
095    }
096
097    @Override
098    protected void doStart() throws Exception {
099        super.doStart();
100        // do nothing, the timer will be set when the first consumer will request it
101    }
102
103    @Override
104    protected void doStop() throws Exception {
105        setTimer(null);
106        super.doStop();
107    }
108
109    @ManagedAttribute
110    public boolean isMultipleConsumersSupported() {
111        return true;
112    }
113
114    @ManagedAttribute(description = "Timer Name")
115    public String getTimerName() {
116        if (timerName == null) {
117            timerName = getEndpointUri();
118        }
119        return timerName;
120    }
121
122    /**
123     * The name of the timer
124     */
125    @ManagedAttribute(description = "Timer Name")
126    public void setTimerName(String timerName) {
127        this.timerName = timerName;
128    }
129
130    @ManagedAttribute(description = "Timer Daemon")
131    public boolean isDaemon() {
132        return daemon;
133    }
134
135    /**
136     * Specifies whether or not the thread associated with the timer endpoint runs as a daemon.
137     * <p/>
138     * The default value is true.
139     */
140    @ManagedAttribute(description = "Timer Daemon")
141    public void setDaemon(boolean daemon) {
142        this.daemon = daemon;
143    }
144
145    @ManagedAttribute(description = "Timer Delay")
146    public long getDelay() {
147        return delay;
148    }
149
150    /**
151     * The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time option.
152     * <p/>
153     * The default value is 1000.
154     * You can also specify time values using units, such as 60s (60 seconds), 5m30s (5 minutes and 30 seconds), and 1h (1 hour).
155     * @see <a href="http://camel.apache.org/how-do-i-specify-time-period-in-a-human-friendly-syntax.html">human friendly syntax</a>
156     */
157    @ManagedAttribute(description = "Timer Delay")
158    public void setDelay(long delay) {
159        this.delay = delay;
160    }
161
162    @ManagedAttribute(description = "Timer FixedRate")
163    public boolean isFixedRate() {
164        return fixedRate;
165    }
166
167    /**
168     * Events take place at approximately regular intervals, separated by the specified period.
169     */
170    @ManagedAttribute(description = "Timer FixedRate")
171    public void setFixedRate(boolean fixedRate) {
172        this.fixedRate = fixedRate;
173    }
174
175    @ManagedAttribute(description = "Timer Period")
176    public long getPeriod() {
177        return period;
178    }
179
180    /**
181     * If greater than 0, generate periodic events every period milliseconds.
182     * <p/>
183     * The default value is 1000.
184     * You can also specify time values using units, such as 60s (60 seconds), 5m30s (5 minutes and 30 seconds), and 1h (1 hour).
185     * @see <a href="http://camel.apache.org/how-do-i-specify-time-period-in-a-human-friendly-syntax.html">human friendly syntax</a>
186     */
187    @ManagedAttribute(description = "Timer Period")
188    public void setPeriod(long period) {
189        this.period = period;
190    }
191
192    @ManagedAttribute(description = "Repeat Count")
193    public long getRepeatCount() {
194        return repeatCount;
195    }
196
197    /**
198     * Specifies a maximum limit of number of fires.
199     * So if you set it to 1, the timer will only fire once.
200     * If you set it to 5, it will only fire five times.
201     * A value of zero or negative means fire forever.
202     */
203    @ManagedAttribute(description = "Repeat Count")
204    public void setRepeatCount(long repeatCount) {
205        this.repeatCount = repeatCount;
206    }
207
208    public Date getTime() {
209        return time;
210    }
211
212    /**
213     * 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.
214     */
215    public void setTime(Date time) {
216        this.time = time;
217    }
218
219    public String getPattern() {
220        return pattern;
221    }
222
223    /**
224     * Allows you to specify a custom Date pattern to use for setting the time option using URI syntax.
225     */
226    public void setPattern(String pattern) {
227        this.pattern = pattern;
228    }
229
230    public Timer getTimer(TimerConsumer consumer) {
231        if (timer != null) {
232            // use custom timer
233            return timer;
234        }
235        return getComponent().getTimer(consumer);
236    }
237
238    /**
239     * To use a custom {@link Timer}
240     */
241    public void setTimer(Timer timer) {
242        this.timer = timer;
243    }
244
245    public void removeTimer(TimerConsumer consumer) {
246        if (timer == null) {
247            // only remove timer if we are not using a custom timer
248            getComponent().removeTimer(consumer);
249        }
250    }
251
252}