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

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

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

vx6bjr1n1#

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

@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.
}

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

相关问题