2013-03-19 60 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