✨ 事件失效任务
parent
b28ad9452c
commit
cbd7afee8a
@ -0,0 +1,73 @@
|
||||
package org.alis.aliscomponent.executor;
|
||||
|
||||
import cn.hutool.cache.CacheUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.RemovalListener;
|
||||
import com.github.benmanes.caffeine.cache.Scheduler;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author robin
|
||||
* @date 2022/1/7 18:14
|
||||
*/
|
||||
public class CacheEventDelayExecutor implements EventDelayExecutor {
|
||||
|
||||
|
||||
|
||||
private final Cache<String, EventTask> CACHE = Caffeine.newBuilder().maximumSize(10000L).expireAfterWrite(20L, TimeUnit.MILLISECONDS)
|
||||
.executor(ThreadUtil.newExecutor())
|
||||
.scheduler(Scheduler.systemScheduler())
|
||||
.removalListener((RemovalListener<String, EventTask>) (key, value, cause) -> {
|
||||
if (Objects.nonNull(value) && value.isExpire()) {
|
||||
value.getTask().run();
|
||||
}else{
|
||||
rePut(key,value);
|
||||
}
|
||||
}).build();
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void scheduled(Runnable task, Long delay, TimeUnit timeUnit) {
|
||||
CACHE.put(IdUtil.randomUUID(), new EventTask(task, timeUnit.toMillis(delay)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
CACHE.invalidateAll();
|
||||
}
|
||||
|
||||
@ToString
|
||||
public static class EventTask {
|
||||
@Getter
|
||||
private final Runnable task;
|
||||
|
||||
private final long expire;
|
||||
|
||||
public EventTask(Runnable runnable, long expire) {
|
||||
this.task = runnable;
|
||||
this.expire = System.currentTimeMillis() + expire;
|
||||
}
|
||||
|
||||
public boolean isExpire() {
|
||||
return expire - System.currentTimeMillis() <= 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void rePut(String key ,EventTask task){
|
||||
CACHE.put(key,task);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package org.alis.aliscomponent.executor;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author robin
|
||||
* @date 2022/1/7 18:10
|
||||
*/
|
||||
public interface EventDelayExecutor extends DelayExecutor {
|
||||
|
||||
/**
|
||||
* 不建议使用
|
||||
* @deprecated
|
||||
* @param command 具体运行的任务
|
||||
* @param initialDelay 执行延迟的时间
|
||||
* @param delay 延迟时间
|
||||
* @param unit 时间单位
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
default void scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
|
||||
throw new UnsupportedOperationException("event pattern not support");
|
||||
}
|
||||
|
||||
/**
|
||||
* 不建议使用
|
||||
* @deprecated
|
||||
* @param command 具体运行的任务
|
||||
* @param initialDelay 执行延迟的时间
|
||||
* @param delay 延迟时间
|
||||
* @param unit 时间单位
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
default void scheduleAtFixedRate(Runnable command, long initialDelay, long delay, TimeUnit unit) {
|
||||
throw new UnsupportedOperationException("event pattern not support");
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue