Spring 定时任务Scheduler监控异常
设置Scheduler任务超时时间
监控Spring 定时任务Scheduler执行情况
1、定义一个执行任务
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class CodeJob {
@Scheduled(cron = "0/5 * * * * ?")
public void execute() {
try {
TimeUnit.SECONDS.sleep(5);
}catch (Exception e) {
log.error("CodeJob execute error", e);
}
System.out.println("CodeJob execute");
}
}
2、自定义ThreadPoolTaskScheduler设置核心数大小,包装Runnable任务对象
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@Slf4j
@Configuration
public class SchedulerConfig {
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
MyThreadPoolTaskScheduler threadPoolTaskScheduler = new MyThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(Runtime.getRuntime().availableProcessors() * 2);
threadPoolTaskScheduler.setThreadNamePrefix("mi-scheduler-");
threadPoolTaskScheduler.initialize();
return threadPoolTaskScheduler;
}
class MyThreadPoolTaskScheduler extends ThreadPoolTaskScheduler {
private Runnable wrap(Runnable runnable) {
return () -> {
try {
Future<?> future = this.submit(runnable);
future.get(3, TimeUnit.SECONDS);
}catch (Exception e) {
log.error("task error", e);
}
};
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
return super.schedule(wrap(task), trigger);
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
return super.schedule(wrap(task), startTime);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
return super.scheduleAtFixedRate(wrap(task), startTime, period);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
return super.scheduleAtFixedRate(wrap(task), period);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
return super.scheduleWithFixedDelay(wrap(task), startTime, delay);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
return super.scheduleWithFixedDelay(wrap(task), delay);
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
return super.schedule(wrap(task), startTime);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
return super.scheduleAtFixedRate(wrap(task), startTime, period);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
return super.scheduleAtFixedRate(wrap(task), period);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
return super.scheduleWithFixedDelay(wrap(task), startTime, delay);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
return super.scheduleWithFixedDelay(wrap(task), delay);
}
}
}