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 */
017package org.apache.activemq.thread;
018
019import java.util.HashMap;
020import java.util.Timer;
021import java.util.TimerTask;
022
023import org.apache.activemq.util.ServiceStopper;
024import org.apache.activemq.util.ServiceSupport;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 *
030 */
031public final class Scheduler extends ServiceSupport {
032
033    private static final Logger LOG  = LoggerFactory.getLogger(Scheduler.class);
034    private final String name;
035    private Timer timer;
036    private final HashMap<Runnable, TimerTask> timerTasks = new HashMap<Runnable, TimerTask>();
037
038    public Scheduler(String name) {
039        this.name = name;
040    }
041
042    public synchronized void executePeriodically(final Runnable task, long period) {
043        TimerTask existing = timerTasks.get(task);
044        if (existing != null) {
045            LOG.debug("Task {} already scheduled, cancelling and rescheduling", task);
046            cancel(task);
047        }
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    @Override
076    protected synchronized void doStop(ServiceStopper stopper) throws Exception {
077        if (this.timer != null) {
078            this.timer.cancel();
079        }
080    }
081
082    public String getName() {
083        return name;
084    }
085}