2014-02-12 62 views
3

我開發使用ProxyFactory裏和ClientExecutor像這樣的RESTEasy服務:RESTEasy線程中的ProxyFactory替換是否安全?

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. 
引起
+0

運行到同樣的問題。你有沒有找到解決方案? – javatestcase

回答

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); 
} 
+0

讓我加入並測試它。謝謝 – avillagomez

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連接管理器在那裏的叢林..

+0

這應該是被接受的解決方案 –

相關問題