事件失效任务

master
robin 3 years ago
parent b28ad9452c
commit cbd7afee8a

@ -1,9 +1,8 @@
package org.alis.aliscomponent.controller;
import lombok.extern.slf4j.Slf4j;
import org.alis.aliscomponent.executor.CacheEventDelayExecutor;
import org.alis.aliscomponent.executor.DelayExecutor;
import org.alis.aliscomponent.executor.DelayQueueDelayExecutor;
import org.alis.aliscomponent.utils.DelayExecutors;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -22,7 +21,7 @@ import java.util.concurrent.TimeUnit;
@Slf4j
public class DelayController {
private final DelayExecutor delayExecutor = new DelayQueueDelayExecutor();
private final DelayExecutor delayExecutor = new CacheEventDelayExecutor();
private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@ -30,8 +29,8 @@ public class DelayController {
public String delay() {
delayExecutor.start();
String format = timeFormatter.format(LocalDateTime.now());
log.info(format);
delayExecutor.scheduleWithFixedDelay(() -> System.out.println(timeFormatter.format(LocalDateTime.now()) + " : 哈哈哈哈!!--"), 0L, 3L, TimeUnit.SECONDS);
delayExecutor.scheduled(() -> System.out.println(format + " : 哈哈哈哈!!--"),
3L, TimeUnit.SECONDS);
return "ok";
}

@ -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");
}
}

@ -1,7 +1,7 @@
package org.alis.aliscomponent.utils;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timer;
import org.alis.aliscomponent.executor.DelayExecutor;
import org.alis.aliscomponent.executor.NettyTimerWheelDelayExecutor;
import java.util.concurrent.TimeUnit;
@ -13,9 +13,15 @@ public final class DelayExecutors {
private DelayExecutors() {
}
private static final Timer TIMER = new HashedWheelTimer();
private static final DelayExecutor EXECUTOR = new NettyTimerWheelDelayExecutor();
static {
EXECUTOR.start();
}
public static void scheduled(Runnable runnable, Long time, TimeUnit timeUnit) {
TIMER.newTimeout(timeout -> runnable.run(), time, timeUnit);
EXECUTOR.scheduled(runnable, time, timeUnit);
}
}

Loading…
Cancel
Save