mirror of https://github.com/apache/activemq.git
Switch to using a Timer to schedual tasks. Seems to have much more acurate time delays between executions.
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@574321 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5f96839259
commit
a69c3e78c5
|
@ -17,54 +17,48 @@
|
||||||
package org.apache.activemq.thread;
|
package org.apache.activemq.thread;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Timer;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.TimerTask;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
public final class Scheduler {
|
public final class Scheduler {
|
||||||
|
|
||||||
public static final ScheduledThreadPoolExecutor CLOCK_DAEMON = new ScheduledThreadPoolExecutor(5, createThreadFactory());
|
private static final class SchedulerTimerTask extends TimerTask {
|
||||||
static {
|
private final Runnable task;
|
||||||
CLOCK_DAEMON.setKeepAliveTime(5, TimeUnit.SECONDS);
|
|
||||||
|
private SchedulerTimerTask(Runnable task) {
|
||||||
|
this.task = task;
|
||||||
}
|
}
|
||||||
private static final HashMap<Runnable, ScheduledFuture> CLOCK_TICKETS = new HashMap<Runnable, ScheduledFuture>();
|
|
||||||
|
public void run() {
|
||||||
|
task.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Timer CLOCK_DAEMON = new Timer("ActiveMQ Scheduler", true);
|
||||||
|
private static final HashMap<Runnable, TimerTask> TIMER_TASKS = new HashMap<Runnable, TimerTask>();
|
||||||
|
|
||||||
private Scheduler() {
|
private Scheduler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ThreadFactory createThreadFactory() {
|
|
||||||
return new ThreadFactory() {
|
|
||||||
public Thread newThread(Runnable runnable) {
|
|
||||||
Thread thread = new Thread(runnable, "ActiveMQ Scheduler");
|
|
||||||
thread.setDaemon(true);
|
|
||||||
return thread;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static synchronized void executePeriodically(final Runnable task, long period) {
|
public static synchronized void executePeriodically(final Runnable task, long period) {
|
||||||
ScheduledFuture ticket = CLOCK_DAEMON
|
TimerTask timerTask = new SchedulerTimerTask(task);
|
||||||
.scheduleAtFixedRate(task, period, period, TimeUnit.MILLISECONDS);
|
CLOCK_DAEMON.scheduleAtFixedRate(timerTask, period, period);
|
||||||
CLOCK_TICKETS.put(task, ticket);
|
TIMER_TASKS.put(task, timerTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void cancel(Runnable task) {
|
public static synchronized void cancel(Runnable task) {
|
||||||
ScheduledFuture ticket = CLOCK_TICKETS.remove(task);
|
TimerTask ticket = TIMER_TASKS.remove(task);
|
||||||
if (ticket != null) {
|
if (ticket != null) {
|
||||||
ticket.cancel(false);
|
ticket.cancel();
|
||||||
if (ticket instanceof Runnable) {
|
|
||||||
CLOCK_DAEMON.remove((Runnable)ticket);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeAfterDelay(final Runnable task, long redeliveryDelay) {
|
public static void executeAfterDelay(final Runnable task, long redeliveryDelay) {
|
||||||
CLOCK_DAEMON.schedule(task, redeliveryDelay, TimeUnit.MILLISECONDS);
|
TimerTask timerTask = new SchedulerTimerTask(task);
|
||||||
|
CLOCK_DAEMON.schedule(timerTask, redeliveryDelay);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue