Rename scheduler methods to better match what they return, add a method to return an Executor that logs exceptions

This commit is contained in:
Vankka 2022-01-13 18:56:48 +02:00
parent 38a7b9dc92
commit 0c7783675d
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 40 additions and 21 deletions

View File

@ -91,7 +91,7 @@ public interface DiscordSRV extends DiscordSRVApi {
@ApiStatus.NonExtendable
default <K, V> Caffeine<K, V> caffeineBuilder() {
return (Caffeine<K, V>) Caffeine.newBuilder()
.executor(scheduler().forkExecutor());
.executor(scheduler().forkJoinPool());
}
// Lifecycle

View File

@ -47,7 +47,7 @@ public class DependencyLoader {
public DependencyLoader(DiscordSRV discordSRV, String[] dependencyResources) {
this(
discordSRV.dataDirectory(),
discordSRV.scheduler().forkExecutor(),
discordSRV.scheduler().forkJoinPool(),
dependencyResources
);
}

View File

@ -31,23 +31,29 @@ public interface Scheduler {
*/
String THREAD_NAME_PREFIX = "DiscordSRV Async ";
/**
* An executor that will actually catch exceptions.
* @return the {@link Executor}
*/
Executor executor();
/**
* Returns the {@link ExecutorService} being used.
* @return the {@link ExecutorService}
*/
ExecutorService executor();
ExecutorService executorService();
/**
* Returns the {@link ScheduledExecutorService} being used.
* @return the {@link ScheduledExecutorService}
*/
ScheduledExecutorService scheduledExecutor();
ScheduledExecutorService scheduledExecutorService();
/**
* Returns the {@link ForkJoinPool} being used.
* @return the {@link ForkJoinPool}
*/
ForkJoinPool forkExecutor();
ForkJoinPool forkJoinPool();
/**
* Runs the provided task as soon as possible.

View File

@ -31,9 +31,10 @@ import java.util.concurrent.*;
public class StandardScheduler implements Scheduler {
private final DiscordSRV discordSRV;
private final ThreadPoolExecutor executor;
private final ScheduledThreadPoolExecutor scheduledExecutor;
private final ThreadPoolExecutor executorService;
private final ScheduledThreadPoolExecutor scheduledExecutorService;
private final ForkJoinPool forkJoinPool;
private final ExceptionHandlingExecutor executor = new ExceptionHandlingExecutor();
public StandardScheduler(DiscordSRV discordSRV) {
this(
@ -43,7 +44,7 @@ public class StandardScheduler implements Scheduler {
16, /* Max pool size */
60, TimeUnit.SECONDS, /* Timeout */
new SynchronousQueue<>(),
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "Scheduled Executor #%s")
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "Executor #%s")
),
new ScheduledThreadPoolExecutor(
2, /* Core pool size */
@ -60,20 +61,20 @@ public class StandardScheduler implements Scheduler {
private StandardScheduler(
DiscordSRV discordSRV,
ThreadPoolExecutor executor,
ScheduledThreadPoolExecutor scheduledExecutor,
ThreadPoolExecutor executorService,
ScheduledThreadPoolExecutor scheduledExecutorService,
ForkJoinPool forkJoinPool
) {
this.discordSRV = discordSRV;
this.executor = executor;
this.scheduledExecutor = scheduledExecutor;
this.executorService = executorService;
this.scheduledExecutorService = scheduledExecutorService;
this.forkJoinPool = forkJoinPool;
}
@Subscribe(priority = EventPriority.LAST)
public void onShuttingDown(DiscordSRVShuttingDownEvent event) {
executor.shutdownNow();
scheduledExecutor.shutdownNow();
executorService.shutdownNow();
scheduledExecutorService.shutdownNow();
forkJoinPool.shutdownNow();
}
@ -88,23 +89,28 @@ public class StandardScheduler implements Scheduler {
}
@Override
public ExecutorService executor() {
public Executor executor() {
return executor;
}
@Override
public ScheduledExecutorService scheduledExecutor() {
return scheduledExecutor;
public ExecutorService executorService() {
return executorService;
}
@Override
public ForkJoinPool forkExecutor() {
public ScheduledExecutorService scheduledExecutorService() {
return scheduledExecutorService;
}
@Override
public ForkJoinPool forkJoinPool() {
return forkJoinPool;
}
@Override
public Future<?> run(@NotNull Runnable task) {
return executor.submit(wrap(task));
return executorService.submit(wrap(task));
}
@Override
@ -114,12 +120,19 @@ public class StandardScheduler implements Scheduler {
@Override
public ScheduledFuture<?> runLater(Runnable task, long timeMillis) {
return scheduledExecutor.schedule(wrap(task), timeMillis, TimeUnit.MILLISECONDS);
return scheduledExecutorService.schedule(wrap(task), timeMillis, TimeUnit.MILLISECONDS);
}
@Override
public ScheduledFuture<?> runAtFixedRate(@NotNull Runnable task, long initialDelayMillis, long rateMillis) {
return scheduledExecutor.scheduleAtFixedRate(wrap(task), initialDelayMillis, rateMillis, TimeUnit.MILLISECONDS);
return scheduledExecutorService.scheduleAtFixedRate(wrap(task), initialDelayMillis, rateMillis, TimeUnit.MILLISECONDS);
}
public class ExceptionHandlingExecutor implements Executor {
@Override
public void execute(@NotNull Runnable command) {
runFork(command);
}
}
}