Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting connection timeout in RestEASY

Tags:

resteasy

JBoss tells us

http://docs.jboss.org/seam/3/rest/latest/reference/en-US/html/rest.client.html

that to set the timeout for a RestEASY ClientRequest we must create a custom ClientExecutor, then call deprecated static methods on ConnManagerParams. This seems rather hokey. Is there a better way? This is RestEASY 2.3.6.

like image 452
Scala Newb Avatar asked Sep 12 '25 20:09

Scala Newb


2 Answers

Here is a clean working solution :-)

@Singleton
public class RestEasyConfig {

    @Inject
    @MyConfig
    private Integer httpClientMaxConnectionsPerRoute;

    @Inject
    @MyConfig
    private Integer httpClientTimeoutMillis;

    @Inject
    @MyConfig
    private Integer httpClientMaxTotalConnections;

    @Produces
    private ClientExecutor clientExecutor;

    @PostConstruct
    public void createExecutor() {
        final BasicHttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, this.httpClientTimeoutMillis);

        final SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

        final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry);
        connManager.setDefaultMaxPerRoute(this.httpClientMaxConnectionsPerRoute);
        connManager.setMaxTotal(this.httpClientMaxTotalConnections);

        final HttpClient httpClient = new DefaultHttpClient(connManager, params);

        this.clientExecutor = new ApacheHttpClient4Executor(httpClient);
    }

}
like image 76
jruillier Avatar answered Sep 15 '25 23:09

jruillier


With RestEASY 3.12.1.Final, as explained by redhat website, I did it this way:

    private Client clientBuilder() {
        return new ResteasyClientBuilder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build()
            .register(ClientRestLoggingFilter.class)
            .register(ObjectMapperContextResolver.class);
    }
like image 41
g.momo Avatar answered Sep 16 '25 00:09

g.momo