Spring Boot:@Async无法正常工作

Spring boot : @Async is not working properly

提问人:Sherif Mo Shalaby 提问时间:11/9/2023 更新时间:11/13/2023 访问量:72

问:

如果有人以前遇到过这个问题; 我有一个版本为2.1.1.RELEASE的springboot应用程序; 在此应用程序中 我有一个控制器,它调用一个服务,该服务调用一个电子邮件服务 所以关系是这样的 控制器 -> businessLogicService -> emailService

在emailService中,我用@Async标记了sendMail方法 我通过将@EnableAsync放在主应用程序中来启用它

目前,该方法仍在主线程上运行,主线程会阻止操作,直到它完成。

我已经深入研究了这个问题,但仍然没有运气

我做过的奇怪的练习之一;是克隆 3 个服务 但这次@Async工作正常 所以不知何故,其他的有问题

考虑到应用程序我巨大,电子邮件服务在很多地方都是自动连线的,对于业务LogicService来说也是如此

Java spring-boot 异步

评论

0赞 tgdavies 11/9/2023
你为什么不看看你的工作版本和非工作版本之间的所有差异。
0赞 Sherif Mo Shalaby 11/9/2023
它们完全相同(我只是复制粘贴并重命名它们),并且它们在同一个项目中

答:

0赞 Slevin 11/9/2023 #1

请尝试将以下配置类添加到您的应用程序中,并判断它是否有效:

/**
 * Enables asynchronous task management. Implements {@link AsyncConfigurer} to allow
 * event publishing operations being executed both synchronously and asynchronously.
 */
@EnableAsync
@Configuration(proxyBeanMethods = false)
public class AsyncConfig implements AsyncConfigurer {

    /*
     * Implementing AsyncConfigurer allows to determine an Executor for asynchronous operations only. This way it is
     * sufficient to simply annotate desired methods with @Async for async treatment, event publishing tasks included.
     *
     * In contrast, providing an ApplicationEventMulticaster would cause all event publishing tasks to be executed
     * asynchronously by default. To be able to execute synchronous tasks nevertheless, we would have to adapt
     * ApplicationEventMulticaster#multicastEvent() accordingly: for example, we could decide by means of a marker
     * interface to which executor the task should get passed (https://stackoverflow.com/a/57929153).
     */

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(8);
        taskExecutor.setMaxPoolSize(32);
        taskExecutor.setQueueCapacity(64);
        taskExecutor.setThreadNamePrefix("DallasMultipassExecutor-");
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}

评论

0赞 Sherif Mo Shalaby 11/10/2023
将尝试一下并在此处发布结果
0赞 Sherif Mo Shalaby 11/10/2023
不工作..请记住,我们使用的是spring 2.1.1.Release,因此ProxyBeanMethods不存在
0赞 Slevin 11/10/2023
不使用应该没有任何区别。但是由于这个解决方案不起作用,我无法进一步帮助。如果没有您提供的任何代码示例,我认为有人只能猜测,或者您可能运气好,而有人遇到了完全相同的问题。proyBeanMethods
0赞 Zuka 11/9/2023 #2

从同一类中调用 @Async 方法不起作用,必须从不同的类调用该方法。

评论

0赞 Sherif Mo Shalaby 11/10/2023
我从 diff 类调用它
0赞 Zuka 11/10/2023
@SherifMoShalaby你可以用代码编辑你的问题
0赞 Community 11/12/2023
您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 Srinivas Rao 11/13/2023 #3

如果 EmailService 有一个异步方法 sendMail,则应直接在 businessLogicService 类 method 中调用它。像这样的东西

BusinessLogicService { 
  foo(){
   emailService.sendMail();
  }
}

如果在 businessLogicService 中调用了其他方法,请尝试将其设置为异步。