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.Exchange;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.RuntimeCamelException;
027    import org.apache.camel.impl.DefaultEndpoint;
028    
029    /**
030     * Represents a timer endpoint that can generate periodic inbound PojoExchanges.
031     *
032     * @version $Revision: 585 $
033     */
034    public class TimerEndpoint extends DefaultEndpoint<Exchange> {
035        private String timerName;
036        private Date time;
037        private long period = 1000;
038        private long delay;
039        private boolean fixedRate;
040        private boolean daemon = true;
041        private Timer timer;
042    
043        public TimerEndpoint(String fullURI, TimerComponent component, String timerName) {
044            super(fullURI, component);
045            this.timer = component.getTimer(this);
046            this.timerName = timerName;
047        }
048    
049        public TimerEndpoint(String endpointUri, Timer timer) {
050            this(endpointUri);
051            this.timer = timer;
052        }
053    
054        public TimerEndpoint(String endpointUri) {
055            super(endpointUri);
056        }
057    
058        public Producer<Exchange> createProducer() throws Exception {
059            throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri());
060        }
061    
062        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
063            return new TimerConsumer(this, processor);
064        }
065    
066        public String getTimerName() {
067            if (timerName == null) {
068                timerName = getEndpointUri();
069            }
070            return timerName;
071        }
072    
073        public void setTimerName(String timerName) {
074            this.timerName = timerName;
075        }
076    
077        public boolean isDaemon() {
078            return daemon;
079        }
080    
081        public void setDaemon(boolean daemon) {
082            this.daemon = daemon;
083        }
084    
085        public long getDelay() {
086            return delay;
087        }
088    
089        public void setDelay(long delay) {
090            this.delay = delay;
091        }
092    
093        public boolean isFixedRate() {
094            return fixedRate;
095        }
096    
097        public void setFixedRate(boolean fixedRate) {
098            this.fixedRate = fixedRate;
099        }
100    
101        public long getPeriod() {
102            return period;
103        }
104    
105        public void setPeriod(long period) {
106            this.period = period;
107        }
108    
109        public Date getTime() {
110            return time;
111        }
112    
113        public void setTime(Date time) {
114            this.time = time;
115        }
116    
117        public boolean isSingleton() {
118            return true;
119        }
120    
121        public Timer getTimer() {
122            if (timer == null) {
123                timer = new Timer();
124            }
125            return timer;
126        }
127    
128        public void setTimer(Timer timer) {
129            this.timer = timer;
130        }
131    }