【问题标题】:Setting connection timeout in RestEASY在 RestEASY 中设置连接超时
【发布时间】:2014-08-17 09:44:08
【问题描述】:

JBoss 告诉我们

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

要为 RestEASY ClientRequest 设置超时,我们必须创建一个自定义 ClientExecutor,然后在 ConnManagerParams 上调用已弃用的静态方法。这似乎相当胡作非为。有没有更好的办法?这是 RestEASY 2.3.6。

【问题讨论】:

  • 我也有同样的问题,你找到答案了吗?

标签: resteasy


【解决方案1】:

使用 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);
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-05-01
      • 2015-08-14
      • 1970-01-01
      • 2017-03-17
      • 2016-06-09
      • 2018-12-19
      • 2012-06-14
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多