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.Consumer;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.RuntimeCamelException;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.spi.ManagementAware;
028    import org.springframework.jmx.export.annotation.ManagedAttribute;
029    import org.springframework.jmx.export.annotation.ManagedResource;
030    
031    /**
032     * Represents a timer endpoint that can generate periodic inbound exchanges triggered by a timer.
033     *
034     * @version $Revision: 816501 $
035     */
036    @ManagedResource(description = "Managed Timer Endpoint")
037    public class TimerEndpoint extends DefaultEndpoint implements ManagementAware<TimerEndpoint> {
038        private String timerName;
039        private Date time;
040        private long period = 1000;
041        private long delay;
042        private boolean fixedRate;
043        private boolean daemon = true;
044        private Timer timer;
045    
046        public TimerEndpoint() {
047        }
048    
049        public TimerEndpoint(String fullURI, TimerComponent component, String timerName) {
050            super(fullURI, component);
051            this.timerName = timerName;
052        }
053    
054        public TimerEndpoint(String endpointUri, Timer timer) {
055            this(endpointUri);
056            this.timer = timer;
057        }
058    
059        public TimerEndpoint(String endpointUri) {
060            super(endpointUri);
061        }
062    
063        public Producer createProducer() throws Exception {
064            throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri());
065        }
066    
067        public Consumer createConsumer(Processor processor) throws Exception {
068            return new TimerConsumer(this, processor);
069        }
070    
071        public Object getManagedObject(TimerEndpoint object) {
072            return this;
073        }
074    
075        @ManagedAttribute(description = "Timer Name")
076        public String getTimerName() {
077            if (timerName == null) {
078                timerName = getEndpointUri();
079            }
080            return timerName;
081        }
082    
083        @ManagedAttribute(description = "Timer Name")
084        public void setTimerName(String timerName) {
085            this.timerName = timerName;
086        }
087    
088        @ManagedAttribute(description = "Timer Daemon")
089        public boolean isDaemon() {
090            return daemon;
091        }
092    
093        @ManagedAttribute(description = "Timer Daemon")
094        public void setDaemon(boolean daemon) {
095            this.daemon = daemon;
096        }
097    
098        @ManagedAttribute(description = "Timer Delay")
099        public long getDelay() {
100            return delay;
101        }
102    
103        @ManagedAttribute(description = "Timer Delay")
104        public void setDelay(long delay) {
105            this.delay = delay;
106        }
107    
108        @ManagedAttribute(description = "Timer FixedRate")
109        public boolean isFixedRate() {
110            return fixedRate;
111        }
112    
113        @ManagedAttribute(description = "Timer FixedRate")
114        public void setFixedRate(boolean fixedRate) {
115            this.fixedRate = fixedRate;
116        }
117    
118        @ManagedAttribute(description = "Timer Period")
119        public long getPeriod() {
120            return period;
121        }
122    
123        @ManagedAttribute(description = "Timer Period")
124        public void setPeriod(long period) {
125            this.period = period;
126        }
127    
128        public Date getTime() {
129            return time;
130        }
131    
132        public void setTime(Date time) {
133            this.time = time;
134        }
135    
136        @ManagedAttribute(description = "Singleton")
137        public boolean isSingleton() {
138            return true;
139        }
140    
141        public Timer getTimer() {
142            if (timer == null) {
143                TimerComponent tc = (TimerComponent) getComponent();
144                timer = tc.getTimer(this);
145            }
146            return timer;
147        }
148    
149        public void setTimer(Timer timer) {
150            this.timer = timer;
151        }
152    
153        @ManagedAttribute(description = "Camel id")
154        public String getCamelId() {
155            return this.getCamelContext().getName();
156        }
157    
158        @ManagedAttribute(description = "Endpoint Uri")
159        public String getEndpointUri() {
160            return super.getEndpointUri();
161        }
162    
163    }