在RestEASY中设置连接超时

时间:2014-06-26 13:43:35

标签: resteasy

JBoss告诉我们

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

要设置RestEASY ClientRequest的超时,我们必须创建一个自定义ClientExecutor,然后在ConnManagerParams上调用不推荐使用的静态方法。这似乎很有趣。有没有更好的办法?这是RestEASY 2.3.6。

2 个答案:

答案 0 :(得分:1)

这是一个干净的工作解决方案: - )

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

}

答案 1 :(得分:0)

使用 RestEASY 3.12.1.Final,正如 redhat website 所解释的,我是这样做的:

    private Client clientBuilder() {
        return new ResteasyClientBuilder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build()
            .register(ClientRestLoggingFilter.class)
            .register(ObjectMapperContextResolver.class);
    }