JBoss 无法在 Resteasy Client 上设置超时
Cannot set timeout on Resteasy Client on JBoss
我正在尝试在 JBoss EAP
服务器上创建一个休息客户端,我需要确保超时会在连接卡住时终止连接。
我的代码如下所示:
int timeout = 5000;
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.setDefaultRequestConfig(defaultRequestConfig).build();
// Previous try
// HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);
JSONObject jData = new JSONObject();
jData.put("param1", param1);
jData.put("param2", param2);
cr.accept("application/json");
cr.body("application/json", jData.toString());
ClientResponse<String> cres = cr.post(String.class);
目前这段代码不会超时,只会永远卡住。我做错了什么?
显然这是 RestEasy
的一个已知问题,已在版本 3.0.9.Final
上修复。
您可以查看错误报告 here。
要修复它,请升级 RestEasy
或使用已弃用的 DefaultHttpClient
:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);
Jboss 支持的官方回答是使用配置生成器并应用它实现 class ApacheHttpClient4Executor 中的方法 loadHttpMethod。这是一个具有一些配置的客户端示例:
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
// ......
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(100);
requestBuilder.setSocketTimeout(100);
requestBuilder.setConnectionRequestTimeout(100);
final RequestConfig config = requestBuilder.build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultRequestConfig(config);
CloseableHttpClient client = httpClientBuilder.build();
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(
client) {
@Override
public void loadHttpMethod(ClientRequest request,
HttpRequestBase httpMethod) throws Exception {
httpMethod.setConfig(config);
super.loadHttpMethod(request, httpMethod);
}
};
// use it in your ClientRequest or Proxy interface
final FooService fooService = ProxyFactory.create(FooService.class, host, executor);
我正在尝试在 JBoss EAP
服务器上创建一个休息客户端,我需要确保超时会在连接卡住时终止连接。
我的代码如下所示:
int timeout = 5000;
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.setDefaultRequestConfig(defaultRequestConfig).build();
// Previous try
// HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);
JSONObject jData = new JSONObject();
jData.put("param1", param1);
jData.put("param2", param2);
cr.accept("application/json");
cr.body("application/json", jData.toString());
ClientResponse<String> cres = cr.post(String.class);
目前这段代码不会超时,只会永远卡住。我做错了什么?
显然这是 RestEasy
的一个已知问题,已在版本 3.0.9.Final
上修复。
您可以查看错误报告 here。
要修复它,请升级 RestEasy
或使用已弃用的 DefaultHttpClient
:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);
Jboss 支持的官方回答是使用配置生成器并应用它实现 class ApacheHttpClient4Executor 中的方法 loadHttpMethod。这是一个具有一些配置的客户端示例:
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
// ......
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(100);
requestBuilder.setSocketTimeout(100);
requestBuilder.setConnectionRequestTimeout(100);
final RequestConfig config = requestBuilder.build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultRequestConfig(config);
CloseableHttpClient client = httpClientBuilder.build();
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(
client) {
@Override
public void loadHttpMethod(ClientRequest request,
HttpRequestBase httpMethod) throws Exception {
httpMethod.setConfig(config);
super.loadHttpMethod(request, httpMethod);
}
};
// use it in your ClientRequest or Proxy interface
final FooService fooService = ProxyFactory.create(FooService.class, host, executor);