RESTEasy Client Proxy Overhead?

前端 未结 1 1912
闹比i
闹比i 2020-12-28 23:35

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:



        
相关标签:
1条回答
  • 2020-12-29 00:29

    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.

    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);
    
    0 讨论(0)
提交回复
热议问题