This commit is contained in:
Leo
2025-03-01 23:39:55 +08:00
parent a394f7f268
commit 41fae55b3f
5 changed files with 87 additions and 53 deletions

View File

@@ -24,7 +24,7 @@ public class AsyncConfig implements AsyncConfigurer {
@Bean(name = "threadPoolTaskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); // 核心线程数
executor.setCorePoolSize(16); // 核心线程数
executor.setMaxPoolSize(128); // 最大线程数
executor.setQueueCapacity(500); // 队列容量
executor.setThreadNamePrefix("Async-Executor-");

View File

@@ -6,8 +6,8 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.PreDestroy; // 使用 jakarta.annotation 包
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
@@ -15,20 +15,33 @@ import java.util.concurrent.*;
public class SchedulerConfig implements SchedulingConfigurer {
private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class);
private ScheduledExecutorService scheduledExecutorService;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
// 动态配置线程池大小
int poolSize = Integer.parseInt(System.getenv().getOrDefault("SCHEDULED_THREAD_POOL_SIZE", "10"));
scheduledExecutorService = Executors.newScheduledThreadPool(poolSize);
taskRegistrar.setScheduler(new CustomScheduledExecutorService(scheduledExecutorService));
}
private static class CustomScheduledExecutorService implements ScheduledExecutorService {
private final ScheduledExecutorService delegate;
public CustomScheduledExecutorService(ScheduledExecutorService delegate) {
this.delegate = delegate;
@PreDestroy
public void shutdown() {
if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
scheduledExecutorService.shutdown();
try {
if (!scheduledExecutorService.awaitTermination(60, TimeUnit.SECONDS)) {
scheduledExecutorService.shutdownNow();
}
} catch (InterruptedException e) {
scheduledExecutorService.shutdownNow();
}
}
}
private record CustomScheduledExecutorService(
ScheduledExecutorService delegate) implements ScheduledExecutorService {
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
@@ -49,8 +62,8 @@ public class SchedulerConfig implements SchedulingConfigurer {
return () -> {
try {
command.run();
} catch (Exception e) {
logger.error("Scheduled task error", e);
} catch (Throwable t) { // 捕获所有类型的异常
logger.error("Scheduled task error", t);
}
};
}
@@ -83,37 +96,37 @@ public class SchedulerConfig implements SchedulingConfigurer {
@Override
public <T> Future<T> submit(Callable<T> task) {
return null;
return delegate.submit(task);
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
return null;
return delegate.submit(task, result);
}
@Override
public Future<?> submit(Runnable task) {
return null;
return delegate.submit(task);
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return Collections.emptyList();
return delegate.invokeAll(tasks);
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return Collections.emptyList();
return delegate.invokeAll(tasks, timeout, unit);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return null;
return delegate.invokeAny(tasks);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
return delegate.invokeAny(tasks, timeout, unit);
}
@Override