You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Asankha Perera <as...@apache.org> on 2021/11/03 07:52:28 UTC
Using Async Client
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
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");
}
}
--
Asankha C. Perera
SLAppForge Inc, https://slappforge.com
Aayu Technologies LLC, https://aayutechnologies.com
AdroitLogic, https://www.adroitlogic.com
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: Using Async Client
Posted by Asankha Perera <as...@apache.org>.
Thanks Oleg!
Will try the alternative options :)
regards
asankha
On 11/3/2021 3:08 PM, Oleg Kalnichevski wrote:
>
>
> 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
--
Asankha C. Perera
SLAppForge Inc, https://slappforge.com
Aayu Technologies LLC, https://aayutechnologies.com
AdroitLogic, https://www.adroitlogic.com
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: Using Async Client
Posted by Oleg Kalnichevski <ol...@apache.org>.
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-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org