Jersey client async POST request to not wait for response

Sumitk picture Sumitk · Apr 28, 2015 · Viewed 8.8k times · Source

I have created a simple Jersey client and it is able to successfully do a POST request with a payload. But right now it waits for a response from the http endpoint:

public void callEndpoint(String endpoint, String payload) {

    try {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource webResource = client.resource(getBaseURI(endpoint));

        log.debug("Sending payload [" + payload + "] to URL - [" + getBaseURI(endpoint) + "]");

        // POST method - Is this blocking?
        // Is it possible to not wait for response here
        ClientResponse response = webResource.accept("application/json")
                                             .type("application/json")
                                             .post(ClientResponse.class, payload);
        if (response.getStatus() != 200) {
            log.error("The endpoint [" + getBaseURI(endpoint) + "] returned a non 200 status code [" + response.getStatus() + "] ");
        }

    } catch (Exception e) {
        log.error("The endpoint for " + endpoint + " - " + getBaseURI(endpoint) + " is not reachable. This is the exception - " + e);
    }

}

private URI getBaseURI(String endpoint) {
    // Get this URI from config
    String URL = "http://www.somewhere.com/v2/" + endpoint;
    return UriBuilder.fromUri(URL).build();
}

Ques: Is it possible for the code to not wait for the response.

I was trying to read the Jersey client docs to find if its possible for my code to not wait for the response? I saw that we can only close the connection once we read the response but its not useful in my case. I want to close the connection as soon as I post the payload to the endpoint.

I just need to fire and forget the POST request as I don't care about the response. This is because the processing takes a lot of time at that endpoint and I don't want the thread to wait for the processing.

Also is it possible to wait for the response for some of the requests but not for all? Is there a parameter that I can set in the client that makes it wait/not wait? I am still reading the java docs so this might be a very simple setting but I wasn't able to find till now so asking here. Thanks!

[Update]

I got it working with the following code but when I run my java example code it prints start & done immediately but the program keeps running for a while and then exits. I am guessing its waiting for the Future response so is it possible that I can make my script not wait for it? The code is:

public static void callEndpoint(String endpoint, String payload) {

    try {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        AsyncWebResource webResource = client.asyncResource(getBaseURI(endpoint));

        // POST method
        System.out.println("start");
        Future<ClientResponse> resp = webResource.accept("application/json")
                .type("application/json")
                .post(ClientResponse.class, payload);
        // This line makes the code wait for output
        //System.out.println(resp.get()); 

    } catch (Exception e) {

        System.out.println ("The endpoint for " + endpoint + " - " + getBaseURI(endpoint) + " is not reachable. This is the exception - " + e);
    }
    System.out.println("done");
}

Answer

Vinay Prabhakar picture Vinay Prabhakar · Feb 23, 2017

I used a TypeListener to make it really asynchronous. The onComplete method would be called when the response is received.

webResource.post(new TypeListener<ClientResponse>(ClientResponse.class) {
             @Override
             public void onComplete(Future<ClientResponse> f) throws InterruptedException {
                 try {
                    ClientResponse response = f.get();
                    if (response == null || response.getClientResponseStatus() != Status.OK) {
                        System.err.println(" Async " + response.getClientResponseStatus()+" "+response.getStatus());
                    } else{
                        System.out.println(" Async " + response.getClientResponseStatus()+" "+response.getStatus()+" "+response.getEntity(String.class));
                    }
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
         });