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 023 /** 024 * Singelton, references maintained by users 025 * @version $Revision$ 026 */ 027 public final class Scheduler { 028 029 private final Timer CLOCK_DAEMON = new Timer("ActiveMQ Scheduler", true); 030 private final HashMap<Runnable, TimerTask> TIMER_TASKS = new HashMap<Runnable, TimerTask>(); 031 private static Scheduler instance; 032 033 static { 034 instance = new Scheduler(); 035 } 036 037 private Scheduler() { 038 } 039 040 public static Scheduler getInstance() { 041 return instance; 042 } 043 044 public synchronized void executePeriodically(final Runnable task, long period) { 045 TimerTask timerTask = new SchedulerTimerTask(task); 046 CLOCK_DAEMON.scheduleAtFixedRate(timerTask, period, period); 047 TIMER_TASKS.put(task, timerTask); 048 } 049 050 /* 051 * execute on rough schedual based on termination of last execution. There is no 052 * compensation (two runs in quick succession) for delays 053 */ 054 public synchronized void schedualPeriodically(final Runnable task, long period) { 055 TimerTask timerTask = new SchedulerTimerTask(task); 056 CLOCK_DAEMON.schedule(timerTask, period, period); 057 TIMER_TASKS.put(task, timerTask); 058 } 059 060 public synchronized void cancel(Runnable task) { 061 TimerTask ticket = TIMER_TASKS.remove(task); 062 if (ticket != null) { 063 ticket.cancel(); 064 CLOCK_DAEMON.purge();//remove cancelled TimerTasks 065 } 066 } 067 068 public void executeAfterDelay(final Runnable task, long redeliveryDelay) { 069 TimerTask timerTask = new SchedulerTimerTask(task); 070 CLOCK_DAEMON.schedule(timerTask, redeliveryDelay); 071 } 072 073 public void shutdown() { 074 CLOCK_DAEMON.cancel(); 075 } 076 }