例如,您可以指定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);
來源
2013-03-19 19:26:10
emd
謝謝!這將使代碼線程安全? – avillagomez 2013-03-19 20:42:51
@avillagomez - 是 – emd 2013-03-19 20:50:36
@avillagomez - 只要確保MyClass客戶端是單例(它只實例化一次) – emd 2013-03-19 20:59:14