我是 Java 多线程新手,正在尝试使用 Callable 接口(interface)和 Future 类创建一个 Spring 项目。
我正在获取 dynamo 数据库中的所有记录,并且对于每条记录,我都会对外部服务进行多线程调用。
但我收到此错误:
nested exception is java.util.concurrent.ExecutionException: org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
我的代码:
Controller :
@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] at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:707) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at 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] at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:387) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at 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] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
最佳答案
根据JavaDoc :
Exception thrown when attempting to retrieve the result of a task that aborted by throwing an exception.
问题似乎是对于某些客户
来说,调用
restTemplate.postForObject("http://localhost:3000/save", c, String.class);
导致服务器错误,HTTP 响应代码为“500”
<小时/>我在阅读您的评论后才注意到:
您只有一个 MyCallable
供所有客户
共享。
这不起作用,因为您的 MyCallable
是一个有状态对象(它使用 void sendCustomerToInterface(Customer cust)
存储 Customer
且稍后需要在 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);
}
}
并在 Controller 中编写
for(Customer c:customers) {
MyCallable myCallable = new MyCallable(restTemplate, c);
//System.out.println(c);
Future<String> future= executor.submit(myCallable);
myFutureList.add(future);
}
<小时/>
顺便说一句,你的代码效率很低。您可以跳过生成客户
列表,只需编写
for (Customer c: repo.findAll()) {
//...
}
关于java - 嵌套异常是 java.util.concurrent.ExecutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340716/