package com.qianwen.core.boot.config; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ThreadPoolExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.core.boot.error.ErrorType; import com.qianwen.core.boot.error.ErrorUtil; import com.qianwen.core.context.BladeContext; import com.qianwen.core.context.BladeRunnableWrapper; import com.qianwen.core.launch.props.BladeProperties; import com.qianwen.core.log.event.ErrorLogEvent; import com.qianwen.core.log.model.LogError; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.boot.task.TaskExecutorCustomizer; import org.springframework.boot.task.TaskSchedulerCustomizer; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.lang.NonNull; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.util.ErrorHandler; @EnableScheduling @Configuration @EnableAsync public class BladeExecutorConfiguration extends AsyncConfigurerSupport { private static final Logger log = LoggerFactory.getLogger(BladeExecutorConfiguration.class); private final BladeContext bladeContext; private final BladeProperties bladeProperties; private final ApplicationEventPublisher publisher; public BladeExecutorConfiguration(final BladeContext bladeContext, final BladeProperties bladeProperties, final ApplicationEventPublisher publisher) { this.bladeContext = bladeContext; this.bladeProperties = bladeProperties; this.publisher = publisher; } @Bean public TaskExecutorCustomizer taskExecutorCustomizer() { return taskExecutor -> { taskExecutor.setThreadNamePrefix("async-task-"); taskExecutor.setTaskDecorator(BladeRunnableWrapper::new); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); }; } @Bean public TaskSchedulerCustomizer taskSchedulerCustomizer() { return taskExecutor -> { taskExecutor.setThreadNamePrefix("async-scheduler"); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.setErrorHandler(new BladeErrorHandler(this.bladeContext, this.bladeProperties, this.publisher)); }; } public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new BladeAsyncUncaughtExceptionHandler(this.bladeContext, this.bladeProperties, this.publisher); } private static class BladeAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler { private final BladeContext bladeContext; private final BladeProperties bladeProperties; private final ApplicationEventPublisher eventPublisher; public BladeAsyncUncaughtExceptionHandler(final BladeContext bladeContext, final BladeProperties bladeProperties, final ApplicationEventPublisher eventPublisher) { this.bladeContext = bladeContext; this.bladeProperties = bladeProperties; this.eventPublisher = eventPublisher; } public void handleUncaughtException(@NonNull Throwable error, @NonNull Method method, @NonNull Object... params) { BladeExecutorConfiguration.log.error("Unexpected exception occurred invoking async method: {}", method, error); LogError logError = new LogError(); logError.setParams(ErrorType.ASYNC.getType()); logError.setEnv(this.bladeProperties.getEnv()); logError.setServiceId(this.bladeProperties.getName()); logError.setRequestUri(this.bladeContext.getRequestId()); ErrorUtil.initErrorInfo(error, logError); Map event = new HashMap<>(16); event.put("log", logError); this.eventPublisher.publishEvent(new ErrorLogEvent(event)); } } /* loaded from: blade-core-boot-9.3.0.0-SNAPSHOT.jar:org/springblade/core/boot/config/BladeExecutorConfiguration$BladeErrorHandler.class */ private static class BladeErrorHandler implements ErrorHandler { private final BladeContext bladeContext; private final BladeProperties bladeProperties; private final ApplicationEventPublisher eventPublisher; public BladeErrorHandler(final BladeContext bladeContext, final BladeProperties bladeProperties, final ApplicationEventPublisher eventPublisher) { this.bladeContext = bladeContext; this.bladeProperties = bladeProperties; this.eventPublisher = eventPublisher; } public void handleError(@NonNull Throwable error) { BladeExecutorConfiguration.log.error("Unexpected scheduler exception", error); LogError logError = new LogError(); logError.setParams(ErrorType.SCHEDULER.getType()); logError.setServiceId(this.bladeProperties.getName()); logError.setEnv(this.bladeProperties.getEnv()); logError.setRequestUri(this.bladeContext.getRequestId()); ErrorUtil.initErrorInfo(error, logError); Map event = new HashMap<>(16); event.put("log", logError); this.eventPublisher.publishEvent(new ErrorLogEvent(event)); } } }