首页 > 解决方案 > 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);

    } 
}

标签: javaspringresttemplatehybris

解决方案


推荐阅读