From fccb6e0b769046ab07a124aafda70db45434e501 Mon Sep 17 00:00:00 2001 From: qinerg Date: Sun, 28 Apr 2013 10:19:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=A4=84=E7=90=86=E7=9A=84=E5=8F=8B=E5=A5=BD=E5=B0=81?= =?UTF-8?q?=E8=A3=85=EF=BC=8C=E7=AE=80=E5=8D=95=E7=9A=84=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81=E5=BC=95?= =?UTF-8?q?=E5=85=A5=E5=A4=8D=E6=9D=82=E7=9A=84quartz=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/nutz/lang/Tasks.java | 159 +++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/org/nutz/lang/Tasks.java diff --git a/src/org/nutz/lang/Tasks.java b/src/org/nutz/lang/Tasks.java new file mode 100644 index 000000000..e88afb392 --- /dev/null +++ b/src/org/nutz/lang/Tasks.java @@ -0,0 +1,159 @@ +package org.nutz.lang; + +import java.text.ParseException; +import java.util.Date; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.nutz.log.Log; +import org.nutz.log.Logs; + +/** + * 定时任务服务的友好封装 + * + * @author QinerG(qinerg@gmail.com) + */ +public abstract class Tasks { + + private static Log logger = Logs.get(); + // 缺省的定时任务线程池大小,默认10个应该够了吧? + private static final int DefaultPoolSize = 10; + private static ScheduledThreadPoolExecutor taskScheduler = new ScheduledThreadPoolExecutor(DefaultPoolSize); + + /** + * 立即启动,并以固定的频率来运行任务。后续任务的启动时间不受前次任务延时影响。 + * @param task 具体待执行的任务 + * @param period 每次执行任务的间隔时间(单位秒) + */ + public static ScheduledFuture scheduleAtFixedRate(Runnable task, int periodSeconds) { + return scheduleAtFixedRate(task, 0, periodSeconds, TimeUnit.SECONDS); + } + + /** + * 在指定的延时之后开始以固定的频率来运行任务。后续任务的启动时间不受前次任务延时影响。 + * @param task 具体待执行的任务 + * @param initialDelay 首次执行任务的延时时间 + * @param periodSeconds 每次执行任务的间隔时间(单位秒) + * @param unit 时间单位 + */ + public static ScheduledFuture scheduleAtFixedRate(Runnable task, long initialDelay, int period, TimeUnit unit) { + return taskScheduler.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS); + } + + /** + * 在指定的时间点开始以固定的频率运行任务。后续任务的启动时间不受前次任务延时影响。 + * @param task 具体待执行的任务 + * @param startTime 首次运行的时间点,支持 "yyyy-MM-dd HH:mm:ss" 格式 + * @param period 每次执行任务的间隔时间 + * @param unit 时间单位 + */ + public static void scheduleAtFixedRate(Runnable task, String startTime, int period, TimeUnit unit) throws ParseException { + Date dt = Times.D(startTime); + scheduleAtFixedRate(task, dt, period, unit); + } + + /** + * 在指定的时间点开始以固定的频率运行任务。后续任务的启动时间不受前次任务延时影响。 + * @param task 具体待执行的任务 + * @param startTime 首次运行的时间点 + * @param period 每次执行任务的间隔时间 + * @param unit 时间单位 + */ + public static void scheduleAtFixedRate(final Runnable task, Date startTime, final int period, final TimeUnit unit) { + final Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + taskScheduler.scheduleAtFixedRate(task, 0, period, unit); + timer.cancel(); + } + }, startTime); + } + + /** + * 立即启动,两次任务间保持固定的时间间隔 + * @param task 具体待执行的任务 + * @param period 两次任务的间隔时间(单位秒) + */ + public static ScheduledFuture scheduleWithFixedDelay(Runnable task, int periodSeconds) { + return scheduleWithFixedDelay(task, 0, periodSeconds, TimeUnit.SECONDS); + } + + /** + * 在指定的延时之后启动,两次任务间保持固定的时间间隔 + * @param task 具体待执行的任务 + * @param initialDelay 首次执行任务的延时时间 + * @param period 两次任务的间隔时间(单位秒) + * @param unit 时间单位 + */ + public static ScheduledFuture scheduleWithFixedDelay(Runnable task, long initialDelay, int period, TimeUnit unit) { + return taskScheduler.scheduleWithFixedDelay(task, initialDelay, period, TimeUnit.SECONDS); + } + + /** + * 在指定的时间点启动,两次任务间保持固定的时间间隔 + * @param task 具体待执行的任务 + * @param startTime 首次运行的时间点,支持 "yyyy-MM-dd HH:mm:ss" 格式 + * @param period 两次任务的间隔时间 + * @param unit 时间单位 + */ + public static void scheduleWithFixedDelay(Runnable task, String startTime, int period, TimeUnit unit) throws ParseException { + Date dt = Times.D(startTime); + scheduleWithFixedDelay(task, dt, period, unit); + } + + /** + * 在指定的时间点启动,两次任务间保持固定的时间间隔 + * @param task 具体待执行的任务 + * @param startTime 首次运行的时间点 + * @param period 两次任务的间隔时间 + * @param unit 时间单位 + */ + public static void scheduleWithFixedDelay(final Runnable task, Date startTime, final int period, final TimeUnit unit) { + final Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + taskScheduler.scheduleWithFixedDelay(task, 0, period, unit); + timer.cancel(); + } + }, startTime); + } + + /** + * 调整线程池大小 + * @param threadPoolSize + */ + public static void resizeThreadPool(int threadPoolSize) { + taskScheduler.setCorePoolSize(threadPoolSize); + } + + /** + * 返回定时任务线程池,可做更高级的应用 + * @return + */ + public static ScheduledThreadPoolExecutor getTaskScheduler() { + return taskScheduler; + } + + /** + * 关闭定时任务服务 + *

系统关闭时可调用此方法终止正在执行的定时任务,一旦关闭后不允许再向线程池中添加任务,否则会报RejectedExecutionException异常

+ */ + public static void depose() { + List awaitingExecution = taskScheduler.shutdownNow(); + logger.infof("Tasks stopping. Tasks awaiting execution: %d", awaitingExecution.size()); + } + + /** + * 重启动定时任务服务 + */ + public static void reset() { + depose(); + taskScheduler = new ScheduledThreadPoolExecutor(DefaultPoolSize); + } +}