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