Issue
I know that timeout is a property of client , but we need to send a response in 2 minutes from spring soap endpoint.
How to timeout in spring soap and send a default response within a specified time from soap producer app?
Container : Tomcat
@Endpoint
public class SOAPEndpoint {
private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
private Repository repository;
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getData")
@ResponsePayload
public Response getCountry(@RequestPayload SampleRequest request) {
Response response = new Response();
response.setCountry(repository.retrieveData(request.getParam())); // this lines takes 5 minutes to respond
return response;
}
}
Solution
I wasn't able to find a configuration-based solution, but here are possible library-based solutions:
- Some DBs lets you to set a query timeout, so if it's available for you, it seems like a good approach. If you will specify the DB you use, I will dig into it.
- You can use the TimeLimiter of resilience4j:
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
GetCountryResponse response = new GetCountryResponse();
TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
try {
Country country = timeLimiter.executeFutureSupplier(() ->
CompletableFuture.supplyAsync(() -> countryRepository.findCountry(request.getName())));
response.setCountry(country);
return response;
} catch (TimeoutException e) {
e.printStackTrace(); // handle timeout.
} catch (Exception e) {
e.printStackTrace(); // handle general error.
}
return null; // You may want to replace this.
}
The Producer code above is originated from - https://spring.io/guides/gs/producing-web-service/ and was tested against the Consumer - https://spring.io/guides/gs/consuming-web-service/
Answered By - Most Noble Rabbit