繁体   English   中英

在实现AsyncUncaughtExceptionHandler时自动装配问题

[英]Problem autowiring in implementation of AsyncUncaughtExceptionHandler

我有这样的配置类:

@Configuration
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class SpringAsyncConfiguration implements AsyncConfigurer {

    @Autowired
    private AppConfigProperties appConfigProperties;

    @Autowired
    private AsyncExceptionHandler asyncExceptionHandler;

    @Bean("asyncExecutor")
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(appConfigProperties.getThreadpoolCorePoolSize());
        executor.setMaxPoolSize(appConfigProperties.getThreadpoolMaxPoolSize());
        executor.setQueueCapacity(appConfigProperties.getThreadpoolQueueCapacity());
        executor.setThreadNamePrefix("threadPoolExecutor-");
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return asyncExceptionHandler;
    }
}

还有ExceptionHandler在这里:

@Component
@Slf4j
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    @Autowired
    private SynchronizationHelper synchronizationHelper;

    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {

        log.error("*** ASYNC Exception message - " + throwable);

        if("synchronize".equals(method.getName())) {
            synchronizationHelper.(...)
            (...)
        }
    }

}

问题是,在未捕获的异常(在@Async注释的方法中) handleUncaughtException ,即使getAsyncUncaughtExceptionHandler()返回正确的bean,它也不会通过handleUncaughtException方法。

任何想法?

UPDATE

我发现删除类AsyncExceptionHandler中的自动装配( 这不是我想要的 ),然后针对未捕获的异常进入方法handleUncaughtException

这是为什么?

问题来自

@Autowired
private SynchronizationHelper synchronizationHelper;

这个bean synchronizationHelper与许多豆类等,这在某种程度上 (我不清楚地了解究竟是如何)禁止的行为,自动装配asyncExceptionHandler

我创建了一个简单的@Service bean,它只满足我的方法handleUncaughtException (它正在更新DB中任务的状态)中所需的内容,并以相同的方式自动连接,现在一切正常。

如果有人有任何想法,我不愿意在这里进行额外的调查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM