定时任务-注入动态线程池
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
package com.accompany.scheduler.config;
|
||||
|
||||
import com.accompany.common.constant.Constant;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* @Author yubin
|
||||
@@ -20,7 +15,7 @@ import java.util.concurrent.*;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class ScheduleConfig implements SchedulingConfigurer {
|
||||
public class ScheduleConfig {
|
||||
|
||||
@Bean
|
||||
public TaskScheduler taskScheduler() {
|
||||
@@ -32,81 +27,4 @@ public class ScheduleConfig implements SchedulingConfigurer {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
||||
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(30) {
|
||||
// 重写schedule方法,每次定时执行时,都设置一个新的traceid
|
||||
public ScheduledFuture<?> schedule(Runnable command,
|
||||
long delay,
|
||||
TimeUnit unit) {
|
||||
return super.schedule(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
command.run();
|
||||
}, delay, unit);
|
||||
}
|
||||
|
||||
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
|
||||
long delay,
|
||||
TimeUnit unit) {
|
||||
return super.schedule(new Callable<V>() {
|
||||
@Override
|
||||
public V call() throws Exception {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
return callable.call();
|
||||
}
|
||||
}, delay, unit);
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
|
||||
long initialDelay,
|
||||
long period,
|
||||
TimeUnit unit) {
|
||||
return super.scheduleAtFixedRate(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
command.run();
|
||||
}, initialDelay, period, unit);
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
|
||||
long initialDelay,
|
||||
long delay,
|
||||
TimeUnit unit) {
|
||||
return super.scheduleWithFixedDelay(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
command.run();
|
||||
}, initialDelay, delay, unit);
|
||||
}
|
||||
public void execute(Runnable command) {
|
||||
super.execute(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
command.run();
|
||||
});
|
||||
}
|
||||
|
||||
public Future<?> submit(Runnable task) {
|
||||
return super.submit(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
task.run();
|
||||
});
|
||||
}
|
||||
|
||||
public <T> Future<T> submit(Runnable task, T result) {
|
||||
return super.submit(() -> {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
task.run();
|
||||
}, result);
|
||||
}
|
||||
|
||||
public <T> Future<T> submit(Callable<T> task) {
|
||||
return super.submit(new Callable<T>() {
|
||||
@Override
|
||||
public T call() throws Exception {
|
||||
MDC.put(Constant.LOG_TRACE_ID_NAME, UUID.randomUUID().toString());
|
||||
return task.call();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
taskRegistrar.setScheduler(executor);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user