一个有用的类
/**
* @Description
* @ClassName TaskRunnable
* @Date 2022/1/27 16:53
* @Author 王东平
*/
@Slf4j
public class Task {
/**
* 任务ID
*/
private String id;
/**
* 任务名
*/
private String name;
/**
* 超时时间,毫秒
*/
private long timeout = Integer.MAX_VALUE;
/**
* 初始延时,毫秒
*/
private long initialDelay = 1000;
/**
* 定时任务周期,毫秒
*/
private long period = 1000;
/**
* 过期时间,毫秒
*/
private long expireDate;
/**
* 任务
*/
private Runnable taskHandler = () -> {
};
/**
* 过期处理器
*/
private Runnable expireHandler = () -> {
};
/**
* 定时任务future
*/
private ScheduledFuture future;
/**
* 线程池服务
*/
private ScheduledExecutorService service = TaskExecutorConfig.WCE_SCHEDULED;
public Task() {
}
public Task(String id, String name) {
this.name = name;
this.id = id;
}
public Task(String id, String name, int timeout, int initialDelay, int period) {
this.name = name;
this.id = id;
this.timeout = timeout;
this.initialDelay = initialDelay;
this.period = period;
}
public boolean start(TaskMode mode) {
if (null == service) {
return false;
}
switch (mode) {
case FIXED_DELAY:
expireDate = System.currentTimeMillis() + timeout;
future = service.scheduleWithFixedDelay(taskRun(), initialDelay, period, TimeUnit.MILLISECONDS);
break;
case FIXED_RATE:
expireDate = System.currentTimeMillis() + timeout;
future = service.scheduleAtFixedRate(taskRun(), initialDelay, period, TimeUnit.MILLISECONDS);
break;
case DELAY:
expireDate = System.currentTimeMillis() + timeout;
future = service.schedule(taskRun(),period,TimeUnit.MILLISECONDS);
break;
default:
break;
}
return true;
}
private Runnable taskRun() {
return () -> {
try {
synchronized (this) {
if (System.currentTimeMillis() >= expireDate) {
if (null != expireHandler) {
expireHandler.run();
}
close();
}
taskHandler.run();
}
}catch (Exception e) {
log.info("定时任务报错!!",e);
log.error("定时任务报错!!",e);
}
};
}
public void close() {
if (null == future) {
return;
}
if (future.isCancelled()) {
return;
}
future.cancel(false);
}
public boolean isCancelled() {
return future.isCancelled();
}
public long getExpireDate() {
return expireDate;
}
public Runnable getExpireHandler() {
return expireHandler;
}
public void setExpireHandler(Runnable expireHandler) {
this.expireHandler = expireHandler;
}
public long getTimeout() {
return timeout;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getInitialDelay() {
return initialDelay;
}
public void setInitialDelay(long initialDelay) {
this.initialDelay = initialDelay;
}
public long getPeriod() {
return period;
}
public void setPeriod(long period) {
this.period = period;
}
public Runnable getTaskHandler() {
return taskHandler;
}
public void setTaskHandler(Runnable taskHandler) {
this.taskHandler = taskHandler;
}
public ScheduledFuture getFuture() {
return future;
}
public ScheduledExecutorService getService() {
return service;
}
public void setService(ScheduledExecutorService service) {
this.service = service;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Task{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
'}';
}
}
public enum TaskMode {
FIXED_DELAY,
FIXED_RATE,
DELAY
;
}
使用
Task hotWaitTask = new Task();
hotWaitTask.setId(dccId);
hotWaitTask.setName("hotWaitTask");
hotWaitTask.setPeriod(Long.parseLong(wceStrategyBean.getHotWaitTime()));
hotWaitTask.setTaskHandler(() -> {
// TODO 执行逻辑
});
// 启动任务
hotWaitTask.start(TaskMode.DELAY);