Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.util.HashMap;
   20   import java.util.Timer;
   21   import java.util.TimerTask;
   22   
   23   /**
   24    * @version $Revision: 712224 $
   25    */
   26   public final class Scheduler {
   27   
   28       
   29   
   30   	public static final Timer CLOCK_DAEMON = new Timer("ActiveMQ Scheduler", true);
   31       private static final HashMap<Runnable, TimerTask> TIMER_TASKS = new HashMap<Runnable, TimerTask>();
   32   
   33       private Scheduler() {
   34       }
   35   
   36       public static synchronized void executePeriodically(final Runnable task, long period) {
   37       	TimerTask timerTask = new SchedulerTimerTask(task);
   38           CLOCK_DAEMON.scheduleAtFixedRate(timerTask, period, period);
   39           TIMER_TASKS.put(task, timerTask);
   40       }
   41   
   42       public static synchronized void cancel(Runnable task) {
   43       	TimerTask ticket = TIMER_TASKS.remove(task);
   44           if (ticket != null) {
   45               ticket.cancel();
   46               CLOCK_DAEMON.purge();//remove cancelled TimerTasks
   47           }
   48       }
   49   
   50       public static void executeAfterDelay(final Runnable task, long redeliveryDelay) {
   51       	TimerTask timerTask = new SchedulerTimerTask(task);
   52           CLOCK_DAEMON.schedule(timerTask, redeliveryDelay);
   53       }
   54       
   55       public static void shutdown() {
   56           CLOCK_DAEMON.cancel();
   57       }
   58   
   59   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]