我使用Jersey Client实现Restful客户端,如下所示:
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 60000);
clientConfig.property(ClientProperties.READ_TIMEOUT, 60000);
Client client = ClientBuilder.newClient(clientConfig);
当请求时间超过定义的值时,此代码一直正常工作,并且正确抛出超时异常。但是,当我决定切换到如下使用ApacheConnector时:
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
ApacheConnectorProvider provider = new ApacheConnectorProvider();
clientConfig.connectorProvider(provider);
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 60000);
clientConfig.property(ClientProperties.READ_TIMEOUT, 60000);
Client client = ClientBuilder.newClient(clientConfig);
超时似乎不再有效,所以现在当服务器无法响应时,我的客户端会一直等到永远不会抛出超时异常。
有没有人遇到过这个问题,或者对我在这里做错了什么线索有任何疑问? 感谢
答案 0 :(得分:1)
尝试创建并注册org.apache.http.client.config.RequestConfig
:
clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig.custom()
.setConnectTimeout(60000)
.setSocketTimeout(60000)
.build());
答案 1 :(得分:1)
RequestConfig reqConfig = RequestConfig.custom()
.setConnectionRequestTimeout(time_out_value).build();