我正在使用客户端代理创建RESTEasy服务,到目前为止工作正常。但是,我注意到在我的一些函数中,我看到了相同的代码行:
MyClass client = ProxyFactory.create(MyClass.class, "http://localhost:8080");
从函数中取出它并使其成为类的成员变量以减少可能的开销是否更好?此服务将处理10000 reqs / min的负载。感谢
答案 0 :(得分:7)
例如,您可以将MyClass客户端指定为spring bean,并将其注入任何需要的位置。请注意线程安全,因为RestEasy代理客户端在Apache Commons Http Client下面使用,默认情况下是SimpleHttpConnectionManager,它不是线程安全的。
要在多线程环境中运行(在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);