1 18 package org.apache.activemq.thread; 19 20 import java.util.HashMap ; 21 22 import java.util.concurrent.ScheduledFuture ; 23 import java.util.concurrent.ScheduledThreadPoolExecutor ; 24 import java.util.concurrent.ThreadFactory ; 25 import java.util.concurrent.TimeUnit ; 26 27 30 public class Scheduler { 31 32 33 static public ScheduledThreadPoolExecutor clockDaemon = new ScheduledThreadPoolExecutor (5, new ThreadFactory (){ 34 public Thread newThread(Runnable runnable) { 35 Thread thread = new Thread (runnable,"ActiveMQ Scheduler"); 36 thread.setDaemon(true); 37 return thread; 38 } 39 }); 40 static { 41 clockDaemon.setKeepAliveTime(5, TimeUnit.SECONDS); 42 } 43 static HashMap clockTickets = new HashMap (); 44 45 synchronized static public void executePeriodically(final Runnable task, long period) { 46 ScheduledFuture ticket = clockDaemon.scheduleAtFixedRate(task, period, period, TimeUnit.MILLISECONDS); 47 clockTickets.put(task, ticket); 48 } 49 50 synchronized static public void cancel(Runnable task) { 51 ScheduledFuture ticket = (ScheduledFuture ) clockTickets.remove(task); 52 if( ticket!=null ) { 53 ticket.cancel(false); 54 clockDaemon.remove(task); 55 } 56 } 57 58 public static void executeAfterDelay(final Runnable task, long redeliveryDelay) { 59 clockDaemon.schedule(task, redeliveryDelay, TimeUnit.MILLISECONDS); 60 } 61 62 } 63 | Popular Tags |