3

我使用 ProxyFactory 和 ClientExecutor 在 RESTEasy 中开发了一个服务,如下所示:

PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient);
MyClass client = ProxyFactory.create(MyClass.class, "http://www.example.com", clientExecutor);

它总是完美地工作。在 RESTEasy 弃用 ClientExecutor 和 ProxyFactory 之后,他们为外部连接提供了一个新的 ResteasyClient,但我不知道这个新的 ResteasyClient 是否是线程安全的。这是文档中的新示例代码:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://example.com/base/uri");

SimpleClient simple = target.proxy(SimpleClient.class);

更新:我将代码与 ResteasyClient 一起使用,但出现了许多错误:

javax.ws.rs.ProcessingException: Unable to invoke request

造成的

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.
4

2 回答 2

4

我们使用这个:

    final ResteasyClient client = new ResteasyClientBuilder()
        .connectionPoolSize(10)
        .maxPooledPerRoute(5)
        .build();

在调试之后我发现(至少在我们的情况下)RESTEasy 客户端默认使用 ThreadSafeClientConnManager 所以我认为没有必要指定一个不同的,尽管根据 JavaDoc 它不推荐使用 PoolingHttpClientConnectionManager (注意额外的Http)。但这已在 RESTEasy 客户端 3.0.5.Final 中得到修复:https ://issues.jboss.org/browse/RESTEASY-948

这是一个 HTTP 连接管理器的丛林......

于 2014-12-08T16:08:13.697 回答
0

这对我有用。只需要找到设置 Apache HTTP 引擎的钩子。主要基于RestEasy 3.0.5.Final API

public static Object setupServiceProxy(@NotNull Class responseClass) {
    ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
    ResteasyClientBuilder builder = new ResteasyClientBuilder().providerFactory(factory);
    ResteasyClient client = builder.httpEngine(setupHttpDefaults()).build();
    ResteasyWebTarget target = client.target(url);
    return target.proxy(responseClass);
}

public static ClientHttpEngine setupHttpDefaults() {
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    BasicHttpContext localContext = new BasicHttpContext();
    return new ApacheHttpClient4Engine(httpClient, localContext);
}
于 2014-08-07T17:30:08.257 回答