Spring的ThreadPoolTask​​Executor是非阻塞的吗?

时间:2016-01-29 00:46:36

标签: spring spring-mvc asynchronous

如果我将一个回调方法添加到从使用spring @Async注释注释的方法返回的ListenableFuture中,那么该线程将从@Async块使用的线程池中取出,直到返回回调为止,或者是否将线程返回给在回调被调用之前要重用的线程池?

NOT 询问控制器使用的线程是否返回给servlet。我只询问ThreadPoolTask​​Executor中的线程:

@Controller
@EnableAutoConfiguration
public class ListenableFutureAsyncController {

    @Autowired
    IHeavyLiftingService heavyLiftingService;

    @RequestMapping("/")
    @ResponseBody
    DeferredResult<String> home() {
        // Create DeferredResult
        final DeferredResult<String> result = new DeferredResult<>();

        //Call to the async service
        ListenableFuture<ResponseEntity<String>> future = heavyLiftingService.heavyLifting();

        future.addCallback(
          new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> response) {
                result.setResult(response.getBody());
            }

            @Override
            public void onFailure(Throwable t) {
                result.setErrorResult(t.getMessage());
            }
        });
        // Return the thread to servlet container, 
        // the response will be processed by another thread.
        return result;
    }

}

实现@Async方法的服务

@Service
public class HeavyLiftingServiceImpl implements IHeavyLiftingService {

    @Async(value="asyncServiceThreadPoolTaskExecutor")
    public ListenableFuture<String> heavyLifting() {
        ...
        ...
        ...
    }
}

asyncServiceThreadPoolTask​​Executor是org.springframework.scheduling.concurrent.ThreadPoolTask​​Executor的一个实例

如果可能,请提供描述此行为的文档的链接。

0 个答案:

没有答案