例如,您可以指定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