简体   繁体   English

Spring Boot-@Async被忽略

[英]Spring Boot - @Async is ignored

Using Spring Boot 1.5.2.RELEASE and the @Async annotation seems to be ignored. 使用Spring Boot 1.5.2.RELEASE@Async注释似乎被忽略了。

Have setup the environment like this: 设置如下环境:

@SpringBootApplication
@EnableAsync
public class Application extends AsyncConfigurerSupport {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public Executor getAsyncExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("async-task-");

        executor.initialize();

        return executor;
    }

    ...

... the async method itself: ...异步方法本身:

@Service
public class MyService {

    @Async
    public Future<Long> asyncTask() throws Exception {

        Long test = 1023L;

        Thread.sleep(10000);

        return new AsyncResult<>(test);
    }
}

... now I'm trying to use this: ...现在我正在尝试使用此功能:

@RestController
public MyController {

    @Autowired
    public MyService myService;

    @PostMapping("/test")
    public ResponseEntity<MyResponse> test() {
        return new ResponseEntity<>(
                       new MyResponse(myService
                           .asyncTask()
                           .get()), 
                       HttpStatus.OK);
    }
}

... and the controller method still hangs for 10sec instead of to be immediatelly returned. ...,并且该控制器方法仍然挂起10sec而不是立即返回。

The @Async method is called from the different object. @Async方法是从其他对象调用的。 It's neither private nor transactional one as it mentioned at the similar questions. 正如在类似问题中提到的那样,它既不是私有的也不是交易性的。

How to let the method to be invoked asynchronously? 如何让该方法异步调用?

Your test() function is invoking get() on the Future instance. 您的test()函数正在Future实例上调用get()。 The documentation for this function states: "Waits if necessary for the computation to complete, and then retrieves its result." 该函数的文档指出:“等待以完成计算,然后检索其结果。”

So rather than invoking get(), you likely want to return some sort if ID that the caller can use to retrieve the result at a later time (or switch to a synchronous response). 因此,如果调用方可以在以后使用ID检索结果(或切换到同步响应),则可能不希望调用get(),而是返回某种排序。

You should have a look at the Future#get javadoc : 您应该看看Future#get javadoc

Waits if necessary for the computation to complete, and then retrieves its result. 必要时等待计算完成,然后检索其结果。

You are transforming your async method to a synchronous call by calling get method. 您正在通过调用get方法将异步方法转换为同步调用。

Thus instead of calling get , just return the Future . 因此,不必调用get ,而只需返回Future Spring MVC supports future as return type : Spring MVC支持future作为返回类型

A ListenableFuture or CompletableFuture/CompletionStage can be returned when the application wants to produce the value from a thread pool submission. 当应用程序要从线程池提交中产生值时,可以返回ListenableFuture或CompletableFuture / CompletionStage。

Example: 例:

return myService.asyncTask().thenApply(r -> ResponseEntity.ok(new MyResponse(r)));

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

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