RESTEasy 客户端代理开销? [英] RESTEasy Client Proxy Overhead?

查看:36
本文介绍了RESTEasy 客户端代理开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用客户端代理创建 RESTEasy 服务,到目前为止它运行良好.但是,我确实注意到在我的一些函数中我看到了相同的代码行:

I'm creating a RESTEasy service using Client proxies and it works fine so far. However, I did notice that in a few of my functions I see the same line of code:

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

将其从函数中取出并使其成为类的成员变量以减少可能的开销是否更好?此服务将处理 10000 请求/分钟的负载.谢谢

Is it better to take that out of the functions and make it a member variable of the class to reduce possible overhead? This service will handle load of 10000 reqs/min. Thanks

推荐答案

例如,您可以将 MyClass 客户端指定为 spring bean,并将其注入到任何需要的地方.请注意线程安全,因为 RestEasy 代理客户端在 Apache Commons Http 客户端下使用,并且默认使用非线程安全的 SimpleHttpConnectionManager.

You can specify MyClass client as a spring bean, for instance, and inject it wherever it's needed. Be aware of thread safety because the RestEasy proxy client uses underneath the Apache Commons Http Client and as default the SimpleHttpConnectionManager which is not thread safe.

要在多线程环境(在 Servlet 容器中运行)中实现这一点,请执行以下操作:

To achieve this in a multithreaded enironment(running in a Servlet Container) do this:

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);

这篇关于RESTEasy 客户端代理开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆