【问题标题】:nested exception is java.util.concurrent.ExecutionException嵌套异常是 java.util.concurrent.ExecutionException
【发布时间】:2019-06-17 20:07:39
【问题描述】:

我是 Java 中的多线程新手,我正在尝试使用 Callable 接口和 Future 类创建一个 Spring 项目。

我正在获取 dynamo db 中的所有记录,并且对于每条记录,我都在对外部服务进行多线程调用。

但我收到此错误:

嵌套异常是 java.util.concurrent.ExecutionException: org.springframework.web.client.HttpServerErrorException: 500 null] 根本原因

我的代码:

控制器:

@Autowired
public RestTemplate restTemplate;

@Autowired
public MyCallable myCallable;
@GetMapping("/myApp-multithread")
public String getQuoteOnSepThread() throws InterruptedException, ExecutionException {
    System.out.println("#################################################Multi Threaded Post Call######################");
    ExecutorService executor= Executors.newFixedThreadPool(10);
    List<Future<String>> myFutureList= new ArrayList<Future<String>>();
    long startTime=System.currentTimeMillis()/1000;

    Iterable<Customer> customerIterable=repo.findAll();
    List<Customer> customers=new ArrayList<Customer>();
    customerIterable.forEach(customers::add);


    for(Customer c:customers) {

        myCallable.sendCustomerToInterface(c);
        //System.out.println(c);
        Future<String> future= executor.submit(myCallable);
        myFutureList.add(future);

    }

    for(Future<String> fut:myFutureList) {
        fut.get();
    }
    executor.shutdown();

    long timeElapsed= (System.currentTimeMillis()/1000)-startTime;

    System.out.println("->>>>>>>>>>>>>>>Time Elapsed In Multi Threaded Post Call<<<<<<<<<<<<<<<-"+timeElapsed);
    return "Success";

}

MyCallable 类:

public class MyCallable implements Callable<String>{

@Autowired
public RestTemplate restTemplate;

//int index=-1;

Customer c= c= new Customer();;
public void sendCustomerToInterface(Customer cust) {

    c= cust;
}

@Override
public String call() throws Exception {

    System.out.println("Customer no"+ c.getId() +"On thread Number"+Thread.currentThread().getId());
    return restTemplate.postForObject("http://localhost:3000/save", c, String.class);

}

}

谁能帮我解决这个问题

编辑:

错误的全栈跟踪:

org.springframework.web.client.HttpServerErrorException: 500 null 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:707) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:387) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 com.OCADemoClient.OCADemoClient.MyCallable.call(MyCallable.java:32) ~[classes/:na] 在 com.OCADemoClient.OCADemoClient.MyCallable.call(MyCallable.java:1) ~[classes/:na] 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_181] 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_181] 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_181] 在 java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

【问题讨论】:

  • 你能在 POST 调用时分享完整的堆栈跟踪吗
  • 我已经添加了错误的完整堆栈跟踪
  • 它只是表明 org.springframework.web.client.HttpServerErrorException: 500 表示通信有问题。你能试试你的 restTemplate.postForObject("localhost:3000/save", c, String.class);与任何休息客户端并检查其工作与否。
  • @Pant 在进行建议的更改后仍然出现同样的错误。但这一次,为每个面临错误的元素打印了堆栈跟踪。仍然只有最后一个元素被一次又一次地打印出来
  • @ArunKumar 我试过 Arun,它和 Potman 一起工作得很好

标签: java multithreading spring-boot amazon-dynamodb callable


【解决方案1】:

根据JavaDoc

尝试检索因抛出异常而中止的任务的结果时抛出异常。

问题似乎是对于某些Customer 的调用

    restTemplate.postForObject("http://localhost:3000/save", c, String.class);

导致服务器错误,HTTP 响应代码为“500”


看了你的评论才注意到:

您只有 一个 MyCallable 被所有 Customers 共享。

这不起作用,因为您的 MyCallable 是一个有状态的对象(它将 Customervoid sendCustomerToInterface(Customer cust) 一起存储,并且需要稍后在 call() 方法中检索这个特定的 Customer)。

为了让它工作,你可以像这样重写MyCallable

public class MyCallable implements Callable<String>{

    private RestTemplate restTemplate;
    private Customer c;

    public MyCallable(RestTemplate rt, Customer cust) {
        this.restTemplate = rt;
        this.c = cust;
    }

    @Override
    public String call() throws Exception {
        System.out.println("Customer no"+ c.getId() +"On thread Number"+Thread.currentThread().getId());
        return restTemplate.postForObject("http://localhost:3000/save", c, String.class);

    }
}

在控制器中你会写

for(Customer c:customers) {

    MyCallable myCallable = new MyCallable(restTemplate, c);
    //System.out.println(c);
    Future<String> future= executor.submit(myCallable);
    myFutureList.add(future);

}

顺便说一句,您的代码效率低下。你可以跳过生成customers 列表,直接写

for (Customer c: repo.findAll()) {
    //...
}

【讨论】:

  • 我认为问题是:在 MyCallable 类 System.out.println("Customer no"+ c.getId() +"On thread Number"+Thread.currentThread().getId()) ;.该行仅打印一次又一次发送给它的最后一个客户。我不确定为什么会这样。
  • @KshitizSharma 我在阅读您的评论后更新了我的回复
  • 然后我会在控制器中自动装配 RestTemplate 而不是 MyCallable,对吧?
  • 嘿,谢谢托马斯。这个解决方案有效。 :) 您能否详细说明问题是什么以及如何解决?实际上,我是 Java 新手
  • 基本上,您是在创建一个新的 Callable 对象实例,而不是让 spring 创建单例实例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 2018-07-04
  • 1970-01-01
相关资源
最近更新 更多