On 11/3/2021 8:52 AM, Asankha Perera wrote:
Hi All

I'm trying to use the Async client as in the example ZeroCopyHttpExchange. However, before I do the POST submission of a large payload, I need to do a HEAD and determine the resume location for a large file upload retry. But the code does not execute the second POST after the HEAD completes. I'm sure I have missed something trivial, but wasn't able to figure  it out. So any help is much appreciated

Thanks

asankha


Hi Asankha

HttpAsyncClient 4.1 cannot handle requests submitted from a result callback of another request. This limitation has been resolved in HttpClient 5.x.

You need to move the second message exchange out of the callback of the first one.

Hope this helps

Oleg



public class ZeroCopyHttpExchange {

     public static void main(final String[] args) throws Exception {
        final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();

         try {
             httpclient.start();
             final CountDownLatch latch = new CountDownLatch(1);

             final File uploadFile = new File("C:\\Temp\\LARGE.MP4");
             final File responseFile = new File("C:\\Temp\\response.txt");
             final String messageId = "msg-id-0001-001-01";

            final HttpHead headRequest = new HttpHead("http://localhost:8280/test";);
             headRequest.addHeader("Etag", messageId);
             // headRequest.addHeader("Connection", "close");
            Future<HttpResponse> future = httpclient.execute(headRequest, new FutureCallback<HttpResponse>() {

                 public void completed(final HttpResponse response) {
                    if (response.getStatusLine().getStatusCode() == SC_OK) {
                         System.out.println("Received response to HEAD");
                         try {
                            ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8280/test";, uploadFile,
ContentType.create("application/octet-stream"));
                            ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(responseFile) {

                                 @Override
                                 protected File process(
                                         final HttpResponse response,
                                         final File file,
                                        final ContentType contentType) throws Exception {                                     if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {                                         throw new ClientProtocolException("Upload failed: " + response.getStatusLine());
                                     }
                                     return file;
                                 }

                             };
                             System.out.println("Executing POST");
                            Future<File> future = httpclient.execute(httpost, consumer, null);
                             future.get();
                             System.out.println("Received response");
                             latch.countDown();

                         } catch (Exception e) {
                             e.printStackTrace();
                         }
                     }
                 }

                 public void failed(final Exception ex) {
                     // HEAD request has failed
System.out.println(headRequest.getRequestLine() + "->" + ex);
                 }

                 public void cancelled() {
                     // HEAD request cancelled
System.out.println(headRequest.getRequestLine() + " cancelled");
                 }
             });
             future.get();

             System.out.println("Awaiting latch..");
             latch.await();
             System.out.println("Shutting down");
         } finally {
             httpclient.close();
         }
         System.out.println("Done");
     }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to