gpt4 book ai didi

java - 无法在 JBoss 上的 Resteasy Client 上设置超时

转载 作者:行者123 更新时间:2023-11-30 08:05:02 28 4
gpt4 key购买 nike

我正在尝试在 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);

目前此代码不会超时,但会永远卡住。我做错了什么?

最佳答案

Jboss 支持的官方回答是使用配置生成器并应用它实现类 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);

关于java - 无法在 JBoss 上的 Resteasy Client 上设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com