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