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.Date;
020 import java.util.Timer;
021
022 import org.apache.camel.Component;
023 import org.apache.camel.Consumer;
024 import org.apache.camel.MultipleConsumersSupport;
025 import org.apache.camel.Processor;
026 import org.apache.camel.Producer;
027 import org.apache.camel.RuntimeCamelException;
028 import org.apache.camel.api.management.ManagedAttribute;
029 import org.apache.camel.api.management.ManagedResource;
030 import org.apache.camel.impl.DefaultEndpoint;
031
032 /**
033 * Represents a timer endpoint that can generate periodic inbound exchanges triggered by a timer.
034 *
035 * @version
036 */
037 @ManagedResource(description = "Managed TimerEndpoint")
038 public class TimerEndpoint extends DefaultEndpoint implements MultipleConsumersSupport {
039 private String timerName;
040 private Date time;
041 private long period = 1000;
042 private long delay;
043 private boolean fixedRate;
044 private boolean daemon = true;
045 private Timer timer;
046 private long repeatCount;
047
048 public TimerEndpoint() {
049 }
050
051 public TimerEndpoint(String uri, Component component, String timerName) {
052 super(uri, component);
053 this.timerName = timerName;
054 }
055
056 public Producer createProducer() throws Exception {
057 throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri());
058 }
059
060 public Consumer createConsumer(Processor processor) throws Exception {
061 return new TimerConsumer(this, processor);
062 }
063
064 @Override
065 protected void doStart() throws Exception {
066 super.doStart();
067 // do nothing, the timer will be set when the first consumer will request it
068 }
069
070 @Override
071 protected void doStop() throws Exception {
072 setTimer(null);
073 super.doStop();
074 }
075
076 @ManagedAttribute
077 public boolean isMultipleConsumersSupported() {
078 return true;
079 }
080
081 @ManagedAttribute(description = "Timer Name")
082 public String getTimerName() {
083 if (timerName == null) {
084 timerName = getEndpointUri();
085 }
086 return timerName;
087 }
088
089 @ManagedAttribute(description = "Timer Name")
090 public void setTimerName(String timerName) {
091 this.timerName = timerName;
092 }
093
094 @ManagedAttribute(description = "Timer Daemon")
095 public boolean isDaemon() {
096 return daemon;
097 }
098
099 @ManagedAttribute(description = "Timer Daemon")
100 public void setDaemon(boolean daemon) {
101 this.daemon = daemon;
102 }
103
104 @ManagedAttribute(description = "Timer Delay")
105 public long getDelay() {
106 return delay;
107 }
108
109 @ManagedAttribute(description = "Timer Delay")
110 public void setDelay(long delay) {
111 this.delay = delay;
112 }
113
114 @ManagedAttribute(description = "Timer FixedRate")
115 public boolean isFixedRate() {
116 return fixedRate;
117 }
118
119 @ManagedAttribute(description = "Timer FixedRate")
120 public void setFixedRate(boolean fixedRate) {
121 this.fixedRate = fixedRate;
122 }
123
124 @ManagedAttribute(description = "Timer Period")
125 public long getPeriod() {
126 return period;
127 }
128
129 @ManagedAttribute(description = "Timer Period")
130 public void setPeriod(long period) {
131 this.period = period;
132 }
133
134 @ManagedAttribute(description = "Repeat Count")
135 public long getRepeatCount() {
136 return repeatCount;
137 }
138
139 @ManagedAttribute(description = "Repeat Count")
140 public void setRepeatCount(long repeatCount) {
141 this.repeatCount = repeatCount;
142 }
143
144 public Date getTime() {
145 return time;
146 }
147
148 public void setTime(Date time) {
149 this.time = time;
150 }
151
152 @ManagedAttribute(description = "Singleton")
153 public boolean isSingleton() {
154 return true;
155 }
156
157 public synchronized Timer getTimer() {
158 if (timer == null) {
159 TimerComponent tc = (TimerComponent)getComponent();
160 timer = tc.getTimer(this);
161 }
162 return timer;
163 }
164
165 public synchronized void setTimer(Timer timer) {
166 this.timer = timer;
167 }
168
169 @ManagedAttribute(description = "Camel id")
170 public String getCamelId() {
171 return this.getCamelContext().getName();
172 }
173
174 @ManagedAttribute(description = "Endpoint Uri")
175 public String getEndpointUri() {
176 return super.getEndpointUri();
177 }
178
179 @ManagedAttribute(description = "Endpoint State")
180 public String getState() {
181 return getStatus().name();
182 }
183 }