fix(EventExecutor, ModuleExecutorImpl): implement DisposableBean interface and manage executor shutdown
This commit is contained in:
@@ -12,15 +12,16 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
|
||||
public class EventExecutor {
|
||||
public class EventExecutor implements DisposableBean {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EventExecutor.class);
|
||||
private static final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(4); //Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final BlockingQueue<QueuedEvent> eventQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private final Thread processorThread;
|
||||
|
||||
private final EventHandlerRegistry registry;
|
||||
@@ -45,6 +46,12 @@ public class EventExecutor {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
this.processorThread.interrupt();
|
||||
this.executor.shutdownNow();
|
||||
}
|
||||
|
||||
public void submit(final Event event) {
|
||||
this.eventQueue.offer(new QueuedEvent(event.getClass(), event));
|
||||
}
|
||||
@@ -79,7 +86,7 @@ public class EventExecutor {
|
||||
});
|
||||
}
|
||||
try {
|
||||
for (final var future : EXECUTOR.invokeAll(tasks)) {
|
||||
for (final var future : executor.invokeAll(tasks)) {
|
||||
try {
|
||||
future.get();
|
||||
}
|
||||
|
||||
@@ -10,11 +10,13 @@ import java.util.function.Function;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
|
||||
public class ModuleExecutorImpl implements ModuleExecutor {
|
||||
public class ModuleExecutorImpl implements ModuleExecutor, DisposableBean {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleExecutorImpl.class);
|
||||
private static final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(4); // Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final ModuleService moduleManager;
|
||||
|
||||
@@ -22,6 +24,11 @@ public class ModuleExecutorImpl implements ModuleExecutor {
|
||||
this.moduleManager = moduleManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
this.executor.shutdownNow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
this.runTasks(ModuleExecutorCallbacks.UI_PRE, module -> () -> {
|
||||
@@ -72,7 +79,7 @@ public class ModuleExecutorImpl implements ModuleExecutor {
|
||||
}
|
||||
|
||||
try {
|
||||
EXECUTOR.invokeAll(tasks);
|
||||
executor.invokeAll(tasks);
|
||||
}
|
||||
catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
|
||||
Reference in New Issue
Block a user