Search code examples
javajax-rsjersey-client

javax.ws.rs.client.WebTarget POST and get Response in the same time


I have a rest API which provide a POST method which transform the input data and stream it as output (the input is streamed also so we can transfer huge data).

Using curl, I'm able to get the response processed on the same time than that I post the data like this:

curl -uadmin -X POST "http://XXXX:8080/process" -H  "Content-Type: application/octet-stream" --data-binary @myFile.csv > output.csv
Enter host password for user 'admin':
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 5868M    0 14826    0  106k   3627  26777 63:50:21  0:00:04 63:50:17 31293

As you can see, curl is able to receive while sending the data.

My problem is that I would like to achieve the same thing using java (doing an Apache Spark job) but cannot make it working. When I use the program is blocked and I cannot get the Response object:

Response response = (javax.ws.rs.client.WebTarget)target.request().post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM))

Could someone please help me to solve it ? (underneath, java uses jersey-client 2.22.2)


Solution

  • I finally found the good library: async-http-client. Simply using the org.asynchttpclient.handler.TransferCompletionHandler functionality.