|
|
package xyz.wbsite.deployee.util;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.Future;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* 任务/定时任务工具
|
|
|
*
|
|
|
* @author wangbing
|
|
|
* @version 0.0.1
|
|
|
* @since 1.8
|
|
|
*/
|
|
|
public class TaskUtil {
|
|
|
|
|
|
private static Timer timer = new Timer("TaskUtil", true);
|
|
|
private static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
|
/**
|
|
|
* @param task 定时任务
|
|
|
* @param delay 延迟时间
|
|
|
*/
|
|
|
public static void schedule(TimerTask task, long delay) {
|
|
|
timer.schedule(task, delay);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param task 定时任务
|
|
|
* @param time 执行时间
|
|
|
*/
|
|
|
public static void schedule(TimerTask task, Date time) {
|
|
|
timer.schedule(task, time);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param task, 定时任务
|
|
|
* @param delay 延迟时间
|
|
|
* @param period 循环周期(前任务结束-新任务开始)
|
|
|
*/
|
|
|
public static void schedule(TimerTask task, long delay, long period) {
|
|
|
timer.schedule(task, delay, period);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param task, 定时任务
|
|
|
* @param firstTime 执行时间
|
|
|
* @param period 循环周期
|
|
|
*/
|
|
|
public static void schedule(TimerTask task, Date firstTime, long period) {
|
|
|
timer.schedule(task, firstTime, period);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param task, 定时任务
|
|
|
* @param delay 延迟时间
|
|
|
* @param period 循环周期(前任务开始-新任务开始)
|
|
|
*/
|
|
|
public static void scheduleAtFixedRate(TimerTask task, long delay, long period) {
|
|
|
timer.scheduleAtFixedRate(task, delay, period);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param task, 定时任务
|
|
|
* @param firstTime 执行时间
|
|
|
* @param period 循环周期(前任务开始-新任务开始)
|
|
|
*/
|
|
|
public static void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
|
|
|
timer.scheduleAtFixedRate(task, firstTime, period);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消所有任务
|
|
|
*/
|
|
|
public static void cancel() {
|
|
|
timer.cancel();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从任务中删除已取消
|
|
|
*/
|
|
|
public static int purge() {
|
|
|
return timer.purge();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param delay 延时(ms)
|
|
|
*/
|
|
|
public static void schedule(Runnable runnable, long delay) {
|
|
|
service.schedule(runnable, delay, TimeUnit.MILLISECONDS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param delay 延时
|
|
|
* @param unit 时间单位
|
|
|
*/
|
|
|
public static void schedule(Runnable runnable, long delay, TimeUnit unit) {
|
|
|
service.schedule(runnable, delay, unit);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param callable Callable任务
|
|
|
* @param delay 延时(ms)
|
|
|
*/
|
|
|
public static <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay) {
|
|
|
return service.schedule(callable, delay, TimeUnit.MILLISECONDS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param callable Callable任务
|
|
|
* @param delay 延时
|
|
|
* @param unit 时间单位
|
|
|
*/
|
|
|
public static <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
|
|
|
return service.schedule(callable, delay, unit);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param initialDelay 延迟执行时间
|
|
|
* @param period 循环周期(前任务开始-新任务开始)
|
|
|
* @param unit 时间单位
|
|
|
*/
|
|
|
public static ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period, TimeUnit unit) {
|
|
|
return service.scheduleAtFixedRate(runnable, initialDelay, period, unit);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param initialDelay 延迟执行时间
|
|
|
* @param period 循环周期(前任务开始-新任务开始)ms
|
|
|
*/
|
|
|
public static ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period) {
|
|
|
return service.scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param initialDelay 延迟执行时间
|
|
|
* @param delay 循环周期(前任务结束-新任务开始)
|
|
|
* @param unit 时间单位
|
|
|
*/
|
|
|
public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long delay, TimeUnit unit) {
|
|
|
return service.scheduleAtFixedRate(runnable, initialDelay, delay, unit);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行Runnable任务
|
|
|
*
|
|
|
* @param runnable Runnable任务
|
|
|
* @param initialDelay 延迟执行时间
|
|
|
* @param delay 循环周期(前任务结束-新任务开始)ms
|
|
|
*/
|
|
|
public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long delay) {
|
|
|
return service.scheduleAtFixedRate(runnable, initialDelay, delay, TimeUnit.MILLISECONDS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 以获取结果为目的并支持重试的任务,通常为有失败风险的IO或异步操作
|
|
|
*
|
|
|
* @param runnable 任务
|
|
|
* @param <T> 结果
|
|
|
* @param interval 重试间隔
|
|
|
*/
|
|
|
public static <T> T retryTask(Callable<T> runnable, int maxTryCount, long interval) {
|
|
|
for (int i = 0; i < maxTryCount; i++) {
|
|
|
Future<T> submit = service.submit(runnable);
|
|
|
try {
|
|
|
T t = submit.get();
|
|
|
if (t != null) {
|
|
|
return t;
|
|
|
}
|
|
|
// 控制重试时间间隔
|
|
|
if (i < maxTryCount - 1) {
|
|
|
TimeUnit.MILLISECONDS.sleep(interval);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public static <T> T retryTask(Callable<T> runnable) {
|
|
|
return retryTask(runnable, 3, 3000);
|
|
|
}
|
|
|
|
|
|
public static <T> T retryTask(Callable<T> runnable, int maxTryCount) {
|
|
|
return retryTask(runnable, maxTryCount, 3000);
|
|
|
}
|
|
|
|
|
|
public static void repeatTask(Runnable runnable, int repeatCount) {
|
|
|
repeatTask(runnable, repeatCount, 0);
|
|
|
}
|
|
|
|
|
|
public static void repeatTask(Runnable runnable, int repeatCount, long interval) {
|
|
|
for (int i = 0; i < repeatCount; i++) {
|
|
|
service.submit(runnable);
|
|
|
try {
|
|
|
TimeUnit.MILLISECONDS.sleep(interval);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|