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.activemq.thread;
018    
019    import java.util.HashMap;
020    import java.util.Timer;
021    import java.util.TimerTask;
022    import org.apache.activemq.util.ServiceStopper;
023    import org.apache.activemq.util.ServiceSupport;
024    
025    /**
026     *
027     */
028    public final class Scheduler extends ServiceSupport {
029        private final String name;
030        private Timer timer;
031        private final HashMap<Runnable, TimerTask> timerTasks = new HashMap<Runnable, TimerTask>();
032    
033        public Scheduler (String name) {
034            this.name = name;
035        }
036    
037        public void executePeriodically(final Runnable task, long period) {
038            TimerTask timerTask = new SchedulerTimerTask(task);
039            timer.schedule(timerTask, period, period);
040            timerTasks.put(task, timerTask);
041        }
042    
043        /*
044         * execute on rough schedule based on termination of last execution. There is no
045         * compensation (two runs in quick succession) for delays
046         */
047        public synchronized void schedualPeriodically(final Runnable task, long period) {
048            TimerTask timerTask = new SchedulerTimerTask(task);
049            timer.schedule(timerTask, period, period);
050            timerTasks.put(task, timerTask);
051        }
052    
053        public synchronized void cancel(Runnable task) {
054            TimerTask ticket = timerTasks.remove(task);
055            if (ticket != null) {
056                ticket.cancel();
057                timer.purge();//remove cancelled TimerTasks
058            }
059        }
060    
061        public synchronized void executeAfterDelay(final Runnable task, long redeliveryDelay) {
062            TimerTask timerTask = new SchedulerTimerTask(task);
063            timer.schedule(timerTask, redeliveryDelay);
064        }
065    
066        public void shutdown() {
067            timer.cancel();
068        }
069    
070        @Override
071        protected synchronized void doStart() throws Exception {
072            this.timer = new Timer(name, true);
073    
074        }
075    
076        @Override
077        protected synchronized void doStop(ServiceStopper stopper) throws Exception {
078           if (this.timer != null) {
079               this.timer.cancel();
080           }
081    
082        }
083    
084        public String getName() {
085            return name;
086        }
087    }