本文介绍了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 reqs / min的负载。谢谢

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客户端指定为弹簧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客户端代理开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:39
查看更多