Is the ProxyFactory replacement in RESTEasy thread safe?

3k Views Asked by At

I developed a service in RESTEasy using ProxyFactory and ClientExecutor like this:

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);

It always worked perfectly. After RESTEasy deprecated both ClientExecutor and ProxyFactory, they provided a new ResteasyClient for external connections, but I don't know if this new ResteasyClient is threadsafe. This is the new sample code from the documentation:

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

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

UPDATE: I used the code with the ResteasyClient and I got many of these errors:

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

Caused by

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

There are 2 best solutions below

0
On

We use this:

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

And after debugging I found out that (at least in our situation) the RESTEasy client uses the ThreadSafeClientConnManager by default so I think there is no need to specify a different one although according to the JavaDoc it is deprecated in favour of PoolingHttpClientConnectionManager (note the extra Http). But this has been fixed in RESTEasy client 3.0.5.Final: https://issues.jboss.org/browse/RESTEASY-948

It's a jungle of HTTP connection managers out there..

1
On

This worked for me. Just needed to find the hook into setting up the Apache HTTP engine. Mostly based on 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);
}