java - Spring RestTemplate 超时配置不起作用
问题描述
我有一个 hybris 应用程序,它使用 rest 模板调用 restful API 并使用 json 响应。我观察到,当应用程序在加载过程中多次点击 api 时,这些 api 的响应时间显着增加。我将超时设置为 15 秒,但没有应用这些超时。连接仍然保持打开状态并保持等待响应高达 5 分钟。我没有看到任何异常日志记录如此长的请求,它也不会超时。
下面是我用来设置超时的代码。我是否缺少任何东西来设置以启用超时。
public <E, T> E processService(String Url, final T serviceReq,
final Class<E> serviceResponse, final HttpMethod
httpMethod, final MediaType contentType,
final Map<String, String> queryParams, final
Map<String, String> header,
final Map<String, String> pathVariables) throws
Exception {
RestTemplate restTemplate = getRestTemplate();
setTimeoutProperties(restTemplate);
<business logic to fire request and process response>}
private RestTemplate getRestTemplate() {
if (LOG.isDebugEnabled()) {
getRestTemplate().setInterceptors(Collections.singletonList(new
RequestResponseLoggingInterceptor()));
}
return getRestTemplate();
}
private void setTimeoutProperties(RestTemplate template) {
final int readTimeout = configurationService.getConfiguration().getInteger("client.readtimeout",
5000);
final int connectTimeout = configurationService.getConfiguration()
.getInteger("client.connectiontimeout", 5000);
if (template.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
HttpComponentsClientHttpRequestFactory httpRequestFactory = (HttpComponentsClientHttpRequestFactory) template
.getRequestFactory();
httpRequestFactory.setConnectTimeout(connectTimeout);
httpRequestFactory.setReadTimeout(readTimeout);
httpRequestFactory.setConnectionRequestTimeout(connectTimeout);
}
}
解决方案
推荐阅读
- django - How to store Advance Python Scheduler Job on my database
- sql - Querying case-insensitive columns by SQL in Tarantool
- hibernate - Effective way to add Parameterized query For Hibernate
- swagger-ui - 如何在 Swagger-ui SpringDoc open ui 上对 Schemas 进行排序
- django - 如何授予某些用户一次只能查看一个实例的权限?
- r - 在 lapply() data.table 设置中组合 rollapply() 和 weighted.mean() 时出错
- nginx - 使用聚合物构建时未找到 Lit-html
- android - 使用 TransitionManager 向上滑动动画
- azure - 当sql数据库在azure上关闭时,哪个配置逻辑信号用于sql数据库的警报规则?
- unity3d - Unity中如何让角色在不背着他的情况下向8个方向移动?