2013-03-19 58 views
6

我使用客戶端代理創建了RESTEasy服務,迄今爲止它工作正常。但是,我也注意到,在我的幾個功能,我看到相同的代碼行:RESTEasy客戶端代理開銷?

MyClass client = ProxyFactory.create(MyClass.class, "http://localhost:8080"); 

是它更好地利用了這一點的功能,使之類的成員變量,以減少可能出現的開銷?這項服務將處理10000 reqs/min的負載。謝謝

回答

7

例如,您可以指定MyClass客戶端作爲spring bean,並在需要時將其注入。請注意線程安全,因爲RestEasy代理客戶端在Apache Commons Http Client下使用,並且默認情況下是SimpleHttpConnectionManager,它不是線程安全的。

要在多線程enironment(運行在servlet容器)實現這一做到這一點:

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 
HttpClient httpClient = new HttpClient(connectionManager); 

// Only needed if you have a authentication 
Credentials credentials = new UsernamePasswordCredentials(username, password); 
httpClient.getState().setCredentials(AuthScope.ANY, credentials); 
httpClient.getParams().setAuthenticationPreemptive(true); 

clientExecutor = new ApacheHttpClientExecutor(httpClient); 

MyClass client = ProxyFactory.create(MyClass.class, "http://localhost:8080", clientExecutor); 
+0

謝謝!這將使代碼線程安全? – avillagomez 2013-03-19 20:42:51

+1

@avillagomez - 是 – emd 2013-03-19 20:50:36

+0

@avillagomez - 只要確保MyClass客戶端是單例(它只實例化一次) – emd 2013-03-19 20:59:14