【发布时间】: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