Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:24:54+00:00 2026-06-17T05:24:54+00:00

I have a service class that has a number of methods that make REST

  • 0

I have a service class that has a number of methods that make REST calls to a Spring REST service.
Each of the methods looks like this:

public void getUser() {

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(RESOURCE_URL);


        // Get response as String
        String response = service.path("/addUser").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

The above works fine but i am slightly worried that every time the method is called, new instances of ClientConfig, Client and WebResource are created. What would the side effects be of me changing the above and making ClientConfig, Client and WebResource as class level instance variables? i.e. change to this:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(RESOURCE_URL);

public void getUser() {         

        // Get response as String
        String response = service.path("/addUser").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

 public void getUserAccount() {         

        // Get response as String
        String response = service.path("/getUserAccount").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

Is the above likely to fail if multiple users call different methods at the same time? What is the best way to structure the above?

If the Jersey client methods had close() methods, i could have left them the way they were and just close the resources inside the methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:24:55+00:00Added an answer on June 17, 2026 at 5:24 am

    From Jersey documentation:

    For Client:

    Methods to create instances of WebResource are thread-safe. Methods
    that modify configuration and or filters are not guaranteed to be
    thread-safe.

    The creation of a Client instance is an expensive operation and the
    instance may make use of and retain many resources. It is therefore
    recommended that a Client instance is reused for the creation of
    WebResource instances that require the same configuration settings

    For WebResource:

    Methods to create a request and return a response are thread-safe.
    Methods that modify filters are not guaranteed to be thread-safe

    While there is no explicit concurrency documentation for ClientConfig, it’s clear from its source code that its safe to use in a multithreaded environment. The Client class is also thread safe, leaving just the WebResource to consider. Based on its documentation I would dedicate a new WebResource to each thread, meaning your code should look more like this:

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    
    public void getUser() {         
        WebResource service = client.resource(RESOURCE_URL);
    
        // Get response as String
        String response = service
            .path("/addUser")
            .accept(MediaType.TEXT_PLAIN)
            .get(String.class);
    
            return response;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have service classes that look something like this public class UserService :
I have a web service client that has an Authenticator class. The Authenticator requires
I have a REST service and I want to have a helper class that
I have a cart model class that has a List property like so: public
I have ended up with a constructor that looks like this whilst attempting to
I have a service class that I need to execute a method on a
I have a wcf service. The service itself (class that inherits the ServiceContract )
I have created a service class for my network connection so that my app
I have some class that I'm passing as a result of a service method,
i have a class that uses a proxy class inside to call a service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.