java - 嵌套异常是 java.util.concurrent.ExecutionException
问题描述
我是 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 at 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] at 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] at com.OCADemoClient.OCADemoClient.MyCallable.call(MyCallable.java:1) ~[classes/:na] at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_181] at 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]0_181] 在 java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]0_181] 在 java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
解决方案
根据JavaDoc:
尝试检索因抛出异常而中止的任务的结果时抛出异常。
问题似乎是对于某些Customer
电话
restTemplate.postForObject("http://localhost:3000/save", c, String.class);
导致服务器错误,HTTP 响应代码为“500”
我只是在阅读您的评论后才注意到:
您只有一个 MyCallable
在所有Customer
s 上共享。
这是行不通的,因为 yourMyCallable
是一个有状态的对象(它存储了Customer
withvoid sendCustomerToInterface(Customer cust)
并且需要Customer
稍后在call()
方法中检索这个特定的)。
为了使它工作,你可以这样重写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()) {
//...
}
推荐阅读
- python - 尽管设置了边界,但字符在 Python / PyGame 中移出屏幕
- html - (neo)vim coc - html css 的定义
- clojure - 将转导输出与输入结合到哈希图中
- docker - 尝试在 Windows 上的 Docker 容器上挂载主机卷。源文件夹相对于什么?
- asp.net - 如何使用内置验证(或异常范围)验证 Asp.net 中的整数是否属于多个区间?
- gnuplot - 从二进制数据文件中绘制三角形
- encryption - SAP ABAP 中的 RSA/ECB/PKCS1 填充加密
- python - html unescape 并不总是按预期工作
- javascript - 在 WebSphere 服务器上完成的 JS 文件更改未通过 dmgr URL 反映
- spring - JSF无法使用spring boot解析xhtml文件中的bean