i have created simple jersey client , able post request payload. right waits response 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 + "] url - [" + getbaseuri(endpoint) + "]"); // post method - blocking? // possible not wait 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 non 200 status code [" + response.getstatus() + "] "); } } catch (exception e) { log.error("the endpoint " + endpoint + " - " + getbaseuri(endpoint) + " not reachable. exception - " + e); } } private uri getbaseuri(string endpoint) { // uri config string url = "http://www.somewhere.com/v2/" + endpoint; return uribuilder.fromuri(url).build(); }
ques: possible code not wait response.
i trying read jersey client docs find if possible code not wait response? saw can close connection once read response not useful in case. want close connection post payload endpoint.
i need fire , forget post request don't care response. because processing takes lot of time @ endpoint , don't want thread wait processing.
also possible wait response of requests not all? there parameter can set in client makes wait/not wait? still reading java docs might simple setting wasn't able find till asking here. thanks!
[update]
i got working following code when run java example code prints start & done program keeps running while , exits. guessing waiting future response possible can make script not wait it? 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); // line makes code wait output //system.out.println(resp.get()); } catch (exception e) { system.out.println ("the endpoint " + endpoint + " - " + getbaseuri(endpoint) + " not reachable. exception - " + e); } system.out.println("done"); }
i used typelistener make asynchronous. oncomplete method called when response 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(); } } });