I use Springboot2 and Java 8. I have been banging my head to the wall on this one, hours spent but no clue what the problem is. I try to make two independent external calls via CompletableFuture. once both they complete I want to combine their results and return to the user.
On the logs I see first request is successfull but I get HTTP 503 error on the second request just on the line "restTemplate.postForEntity(..)"
Note: I do NOT use any Spring based @Async (or any Spring non blocking code) in my project.
Any idea? thx in advance
@Service
class MyService{
public Detail getResponse(){
Executor exec = Executors.newCachedThreadPool();
CompletableFuture<GenericResponse> c1 = CompletableFuture.supplyAsync(() -> myRestClient.getResponse(myRequestObject),exec);
CompletableFuture<GenericResponse> c2 = CompletableFuture.supplyAsync(() -> myRestClient.getResponse(myRequestObject2),exec);
return c1.thenCombine(c2, detailResponseMapper::mapDetail).join();
}
}
@Component
class MyRestClient{
@Autowired
RestTemplate restTemplate;
private GenericResponse getResponse(MyRequest myRequest) {
ResponseEntity<SofEnvelope> responseEntity = restTemplate.postForEntity(getUrl(myRequest.getPath()), getRequestEntity(myRequest), SofEnvelope.class);
return new GenericResponse(responseEntity.getBody());
}
}
And Error is
java.util.concurrent.CompletionException: org.springframework.web.client.HttpServerErrorException$ServiceUnavailable: 503 Service Unavailable
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1606)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.web.client.HttpServerErrorException$ServiceUnavailable: 503 Service Unavailable
at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:85)
question from:
https://stackoverflow.com/questions/65893201/completablefuture-error-httpservererror-503-service-unavailable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…