java—如何在soap producer中使用默认soap响应设置超时?

zpqajqem  于 2021-07-22  发布在  Java
关注(0)|答案(1)|浏览(383)

我知道超时是客户端的属性,但是我们需要在2分钟内从springsoap端点发送响应。
如何在springsoap中超时并在soap producer应用程序的指定时间内发送默认响应?
容器:tomcat

  1. @Endpoint
  2. public class SOAPEndpoint {
  3. private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
  4. private Repository repository;
  5. @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getData")
  6. @ResponsePayload
  7. public Response getCountry(@RequestPayload SampleRequest request) {
  8. Response response = new Response();
  9. response.setCountry(repository.retrieveData(request.getParam())); // this lines takes 5 minutes to respond
  10. return response;
  11. }
  12. }
vx6bjr1n

vx6bjr1n1#

我找不到基于配置的解决方案,但以下是可能的基于库的解决方案:
有些数据库允许您设置查询超时,因此如果您可以使用它,那么这似乎是一个不错的方法。如果您指定您使用的数据库,我将深入研究。
您可以使用resilience4j的timelimiter:

  1. @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
  2. @ResponsePayload
  3. public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
  4. GetCountryResponse response = new GetCountryResponse();
  5. TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
  6. try {
  7. Country country = timeLimiter.executeFutureSupplier(() ->
  8. CompletableFuture.supplyAsync(() -> countryRepository.findCountry(request.getName())));
  9. response.setCountry(country);
  10. return response;
  11. } catch (TimeoutException e) {
  12. e.printStackTrace(); // handle timeout.
  13. } catch (Exception e) {
  14. e.printStackTrace(); // handle general error.
  15. }
  16. return null; // You may want to replace this.
  17. }

以上生产商代码源自-https://spring.io/guides/gs/producing-web-service/ 对消费者进行了测试-https://spring.io/guides/gs/consuming-web-service/

展开查看全部

相关问题