繁体   English   中英

在 Spring 引导中停止由 @Async 启动的线程

[英]Stopping threads started by @Async in Spring Boot

我有一个异步方法。

    @Async
    public CompletableFuture<Void> getRandom(){
     // Return null or some exception is thrown
     // I will be calling restTemplate.getForEntity() here
     // So I can get some correct response or I can get HttpClientErrorException or HttpServerErrorException
    }

这是通过以下方式调用的: -

List<CompletableFuture<Void>> futures = new ArrayList<>() ;

for(int i = 0 ; i < 5 ; i++){
    futures.add(commonUtil.getRandom());
 }
   
   for(CompletableFuture<Void> future : futures){
  try{
    future.get();
  }catch ( ExecutionException ex){

    Throwable ee = ex.getCause() ;
    if(ee instanceof CustomException){
    
       CustomException exception = (CustomException) ee ;
       System.out.println(ex.getMessage());

       throw exception ;

因此,当我在循环中调用 Async 方法时,每当我从其中任何一个中获取自定义异常时,我都想杀死循环内触发的所有其他踏板并将此异常发送到Controller (因为我希望它发送到客户端 )。

我怎样才能做到这一点?

任何帮助表示赞赏。

暂无
暂无

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

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