简体   繁体   English

Async on spring boot rest rest api - annotation should be on service only or controller

[英]Async on spring boot rest rest api - annotation should be on service only or controller

I have to implement a method with async features in spring boot:我必须在 spring 引导中实现具有异步功能的方法:

I am a bit confused regarding the location of the annotation asyn, basically my rest controller is as follows:我对asyn注释的位置有点困惑,基本上我的rest controller如下:

@RestController
@RequestMapping("/email")
public class EmailController {

    public @ResponseBody ResponseEntity<String> sendMailCon(@RequestBody EmailRequestDto emailRequestDto) {
        LOG.debug("calling method sendMail from controller ");
        //do complex stuff 
        sendMailService.sendEmail(emailRequestDto);
        return new ResponseEntity<>("Mail has been sent successfully", HttpStatus.OK);
    }

And service class is as follows:而服务class如下:

@Component
public class SendMailServiceImpl implements SendMailService {

    private static final Logger LOG = LoggerFactory.getLogger(SendMailServiceImpl.class);

    @Autowired
    private JavaMailSender javaMailSender;
@Override
    @Async("threadPoolExecutor")
    public void sendEmail(EmailRequestDto emailRequestDto) {

        LOG.debug("calling method sendMail do complex stuff");
...
}

I have configured my async bean as follows:我已经按如下方式配置了我的异步 bean:

@EnableAsync
@Configuration
public class AsyncConfig {

    @Bean(name = "threadPoolExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(25);
        executor.setQueueCapacity(100);
        executor.initialize();
        return executor;
    } 

My question is the annotation @Async on the SendMailServiceImpl is correct or i need to add it on the method sendMailCon from controller?我的问题是 SendMailServiceImpl 上的注释 @Async 是否正确,或者我需要将其添加到 controller 的方法 sendMailCon 中?

Your implementation is correct.你的实现是正确的。 In this case you don't need to add @Async on the controller method在这种情况下,您不需要在 controller 方法上添加 @Async

Basically @Async will make method execute in a separate thread ie the caller will not wait for the completion of the called method.Each request to the server is served by a separate thread already, so you have no need to provide @Async on the controller method.基本上@Async将使方法在单独的线程中执行,即调用者不会等待被调用方法的完成。对服务器的每个请求已经由单独的线程提供服务,因此您无需在@Async上提供 @Async方法。

You could keep it in the service layer or better yet another layer down where you actually need the method to be executed asynchronously.In your case you could actually keep the method as async where you use rest template to trigger the mail.If you don't have another class for that it's okay to keep the service layer method as Async.您可以将其保留在服务层或更好的另一层,您实际上需要异步执行该方法。在您的情况下,您实际上可以将方法保持为异步,您使用 rest 模板触发邮件。如果你不这样做没有另一个 class 因为可以将服务层方法保持为异步。

Read

You code is correct and @Async makes your service code to execute in separate thread so controller becomes free immediately after calling the service.您的代码是正确的,@Async 使您的服务代码在单独的线程中执行,因此 controller 在调用服务后立即变得免费。 It works fine when you don't need to return a response from your execution.当您不需要从执行中返回响应时,它可以正常工作。 But if you need to process the request asynchronously and then collect the response to send back to the caller as REST response then you can use WebAsyncTask with Spring.但是,如果您需要异步处理请求,然后收集响应以作为 REST 响应发送回调用方,那么您可以将 WebAsyncTask 与 Spring 一起使用。 Please refer below URL which explains the implementation of async REST service.请参阅下面的 URL,它解释了异步 REST 服务的实现。 https://www.thetechnojournals.com/2019/10/asynchronous-rest-service.html https://www.thetechnojournals.com/2019/10/asynchronous-rest-service.html

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

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