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: 640438 $
033     */
034    public class TimerEndpoint extends DefaultEndpoint<Exchange> {
035    
036        private final TimerComponent component;
037        private final String timerName;
038        private Date time;
039        private long period = 1000;
040        private long delay;
041        private boolean fixedRate;
042        private boolean daemon = true;
043        private Timer timer;
044    
045        public TimerEndpoint(String fullURI, TimerComponent component, String timerName) {
046            super(fullURI, component);
047            this.component = component;
048            this.timerName = timerName;
049        }
050    
051        public Producer<Exchange> createProducer() throws Exception {
052            throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: " + getEndpointUri());
053        }
054    
055        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
056            return new TimerConsumer(this, processor);
057        }
058    
059        public TimerComponent getComponent() {
060            return component;
061        }
062    
063        public String getTimerName() {
064            return timerName;
065        }
066    
067        public boolean isDaemon() {
068            return daemon;
069        }
070    
071        public void setDaemon(boolean daemon) {
072            this.daemon = daemon;
073        }
074    
075        public long getDelay() {
076            return delay;
077        }
078    
079        public void setDelay(long delay) {
080            this.delay = delay;
081        }
082    
083        public boolean isFixedRate() {
084            return fixedRate;
085        }
086    
087        public void setFixedRate(boolean fixedRate) {
088            this.fixedRate = fixedRate;
089        }
090    
091        public long getPeriod() {
092            return period;
093        }
094    
095        public void setPeriod(long period) {
096            this.period = period;
097        }
098    
099        public Date getTime() {
100            return time;
101        }
102    
103        public void setTime(Date time) {
104            this.time = time;
105        }
106    
107        public boolean isSingleton() {
108            return true;
109        }
110    
111        public Timer getTimer() {
112            if (timer == null) {
113                timer = component.getTimer(this);
114            }
115            return timer;
116        }
117    }