I am writing REST client using JX-RS 2.0 client API and have Apache CXF 3.1.8 client as implementation in classpath.
My gradle dependency
compile group: 'org.apache.cxf', name: 'cxf-rt-rs-client', version: '3.1.8'
I plan to client the Client instance with root url only once and use it to multiple create WebTarget at runtime as needed by the application. I want to reuse Client object because https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Client.html this says it expensive operation to create and destroy client object.
String baseUrl = "http://localhost:9081/rest-service/rest/";
Client client = ClientBuilder.newClient();
client.register(JacksonJsonProvider.class);
client.register(CustomObjectMapperProvider.class);
Use the client object to create WebTarget
WebTarget webTarget = client.target(baseUrl + path);
I understand that under the cover org.apache.cxf.jaxrs.client.WebClient
(the CXF's client implementation) is used. http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety link says that both CXF WebClient and ProxyClient are not thread safe by default. As per this link there a threadSafe boolean flag that we can set to true while creating CXFs WebClient or ProxyClient.
In my code I want to use standard JX-RS 2.0 client APIs/interfaces and not CXF specific classes like WebClient or ProxyClient.
Questions -
- If I instantiate the standard Client object using code above, is it thread safe? Is it ok to create it once and use it multiple time to WebTarget of different resources?
- List item If it is not thread safe how to achieve thread safety where I want to reuse client object?