This commit is contained in:
Leo
2025-02-18 22:33:05 +08:00
parent 8589460fa5
commit 5b0ea491a5
4 changed files with 246 additions and 99 deletions

View File

@@ -0,0 +1,129 @@
package cn.van.business.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class);
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
taskRegistrar.setScheduler(new CustomScheduledExecutorService(scheduledExecutorService));
}
private static class CustomScheduledExecutorService implements ScheduledExecutorService {
private final ScheduledExecutorService delegate;
public CustomScheduledExecutorService(ScheduledExecutorService delegate) {
this.delegate = delegate;
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return delegate.schedule(wrap(command), delay, unit);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return delegate.scheduleAtFixedRate(wrap(command), initialDelay, period, unit);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return delegate.scheduleWithFixedDelay(wrap(command), initialDelay, delay, unit);
}
private Runnable wrap(Runnable command) {
return () -> {
try {
command.run();
} catch (Exception e) {
logger.error("Scheduled task error", e);
}
};
}
// Delegate other methods to the delegate executor
@Override
public void shutdown() {
delegate.shutdown();
}
@Override
public List<Runnable> shutdownNow() {
return delegate.shutdownNow();
}
@Override
public boolean isShutdown() {
return delegate.isShutdown();
}
@Override
public boolean isTerminated() {
return delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return delegate.awaitTermination(timeout, unit);
}
@Override
public <T> Future<T> submit(Callable<T> task) {
return null;
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
return null;
}
@Override
public Future<?> submit(Runnable task) {
return null;
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return Collections.emptyList();
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return Collections.emptyList();
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return null;
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
@Override
public <T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit unit) {
return delegate.schedule(callable, delay, unit);
}
@Override
public void execute(Runnable command) {
delegate.execute(wrap(command));
}
}
}