java - 如何在肥皂生产者中使用默认肥皂响应设置超时?
问题描述
我知道 timeout 是 client 的一个属性,但是我们需要在 2 分钟内从 spring 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;
}
}
解决方案
我找不到基于配置的解决方案,但这里有一些可能的基于库的解决方案:
- 一些数据库允许您设置查询超时,因此如果您可以使用它,这似乎是一个好方法。如果你要指定你使用的数据库,我会深入研究它。
- 您可以使用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/produce-web-service/ 并针对消费者进行了测试 - https://spring.io/guides/gs/sumption-web-service /
推荐阅读
- azure - 使用 ARM 模板和 Key Vault 创建 Azure SQL 的最佳做法是什么
- python - 我如何使用 python 订阅/收听多边形区块链上的事件
- java - Java 11 上的 Log4j2 SMTP Appender:javax.mail 还是 jakarta.mail?
- python - 自定义多色水平条形图 matplotlib
- microservices - 通过 NATS 服务器将分子服务连接到 nestjs 网关
- websocket - Vertx websocket 在一个新的verticle中
- apache-flink - 为什么 Flink 中没有计时器指标来记录 Operator 中特定方法的处理时间。[Apache Flink - 1.12.2]
- reactjs - react中的click函数甚至多次调用并且索引文件中没有使用严格模式
- .net - Identity .net core 3.1 如何使用持久 cookie
- python - 使用 Python 在 SQL 中为复杂的 json 数据自动创建表