【问题标题】:How to set a timeout with a default soap response in soap producer?如何在肥皂生产者中使用默认肥皂响应设置超时?
【发布时间】:2021-06-11 02:02:48
【问题描述】:

我知道 timeout 是 client 的一个属性,但我们需要 在 2 分钟内发送响应 从 spring soap 端点。

如何在spring soap中超时并在指定时间内从soap生产者应用发送默认响应?

容器: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;
    }
}

【问题讨论】:

    标签: java spring tomcat servlets soap


    【解决方案1】:

    我找不到基于配置的解决方案,但这里有一些可能的基于库的解决方案:

    • 某些数据库允许您设置查询超时,因此如果您可以使用它,这似乎是一个不错的方法。如果你指定你使用的数据库,我会深入研究它。
    • 您可以使用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.
    }
    

    上面的Producer代码来源于-https://spring.io/guides/gs/producing-web-service/ 并针对消费者进行了测试 - https://spring.io/guides/gs/consuming-web-service/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多