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: [email protected]
For additional commands, e-mail: [email protected]