Search code examples
javajersey-client

Will the com.sun.jersey.api.client.WebResource.Builder.get method wait for response?


I am working on jersey-client 1.19. I have these line of codes to submit a request to server and get the response:

Client client = Client.create();

WebResource webResource = client.resource("http://localhost:8080/RESTfulExample/rest/json/metallica/post");
String input = "{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}";

ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);

if (response.getStatus() != 201) {
    throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

I have a question that when the post method execute, if the connection to server having problem (slow internet connection it will response after 3 min) then the code if (response.getStatus() != 201) will keep running or will wait for the response from the post execution?


Solution

  • Following line is a blocking (synchronous) call to server -

    ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);
    

    It means this line wait for server to respond. Program execution will not continue after this line till some success/error response is received from server.

    It means code written after this line -

    if (response.getStatus() != 201) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }
    

    It will wait for complete execution of previous line (post method response).

    For information, Jersey also supports non-blocking (asynchronous) call to server. Check here for details. Also, I would suggest not to use old version of jersey. Current version is 2.5.1 and there are a lots of difference between jersey 1.x and 2.x