Hi Damian,
I don't see why either case is a threading problem.
If Client and WebTarget are thread-safe (JAX-RS doesn't indicate either
way, but unofficially they are supposed to be) then either approach
would work fine.
Gili
On 11/12/2014 11:32 AM, Sobieralski, Damian Michael wrote:
> I have a question about thread safety here. This is a much lower level discussion than what have been reading here and touches on Java threading 101.
>
> In a backend service object (we have something like this - this object sits in a servlet container):
>
> protected Client jerseyClient = null;
> protected WebTarget jerseyTarget = null;
>
> These are class variables. This is normally a warning flag for threading issues
>
> Then we have code like this:
>
> protected void readyConnection() throws Exception {
> jerseyClient = ClientBuilder.newBuilder().register(JacksonFeature.class).build();
>
> jerseyTarget = jerseyClient.target(baseUrl).path("api").path("v1");
> }
>
> and methods that use jerseyTarget. Is this a bad idea thread wise? It seems like jersey client is fine being a class variable. But that jerseyTarget in this ready method instead of setting the class variable jerseyTarget should return its own jerseyTarget.
>
> So, is it better to do this?
>
> protected WebTarget readyConnection() throws Exception {
> jerseyClient = ClientBuilder.newBuilder().register(JacksonFeature.class).build();
>
> return jerseyClient.target(baseUrl).path("api").path("v1");
> }
>
> - Damian
>