嵌套异常是 java.util.concurrent.ExecutionException
nested exception is java.util.concurrent.ExecutionException
我是 Java 中多线程的新手,正在尝试使用 Callable 接口和 Future Class.
创建一个 Spring 项目
我正在获取 dynamo 数据库中的所有记录,对于每条记录,我正在对外部服务进行多线程调用。
但是我收到这个错误:
nested exception is java.util.concurrent.ExecutionException: org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
我的代码:
控制器:
@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";
}
MyCallableClass:
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.
问题似乎是对于某些 Customer
调用
restTemplate.postForObject("http://localhost:3000/save", c, String.class);
导致服务器错误,HTTP 响应代码为“500”
看了你的评论才发现:
您只有 一个 MyCallable
被所有 Customer
人共享。
这行不通,因为您的 MyCallable
是一个有状态对象(它存储 Customer
和 void sendCustomerToInterface(Customer cust)
并且需要稍后在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()) {
//...
}
我是 Java 中多线程的新手,正在尝试使用 Callable 接口和 Future Class.
创建一个 Spring 项目我正在获取 dynamo 数据库中的所有记录,对于每条记录,我正在对外部服务进行多线程调用。
但是我收到这个错误:
nested exception is java.util.concurrent.ExecutionException: org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
我的代码:
控制器:
@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";
}
MyCallableClass:
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.
问题似乎是对于某些 Customer
调用
restTemplate.postForObject("http://localhost:3000/save", c, String.class);
导致服务器错误,HTTP 响应代码为“500”
看了你的评论才发现:
您只有 一个 MyCallable
被所有 Customer
人共享。
这行不通,因为您的 MyCallable
是一个有状态对象(它存储 Customer
和 void sendCustomerToInterface(Customer cust)
并且需要稍后在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()) {
//...
}