Is there a way I can check the number of bytes downloaded by an Apache’s CloseableHttpPipeliningClient without waiting for the entire pipelined list of file responses to be received? Currently the asynchronous CloseableHttpPipeliningClient only returns when all responses have been received. This is a problem because I am reporting the number of bytes received (from the responses received) every t seconds to a download tracker/monitor that keeps track of the number of bytes downloaded during a time interval and calculates the instantaneous throughput. The main problem is that even if some requested pipelined bytes/responses were downloaded during a particular time interval , I have no way of knowing until the complete list is received. This means that all time intervals will return 0 bytes downloaded except during the time interval in which the asynchronous functionality returns the complete response list. Note the set of files requested and file sizes are known beforehand. In addition, the static files requested (which vary in size) reside on an apache server that I control. Below are 2 versions of the code snippet I am referring too. The first code snippet, uses an anonymous call back function to handle the HTTP Responses. The second version doesn’t utilize a callback method but assumes all responses were received within the 60 second time frame and returned directly by the future. Note I am using Apache's HttpAsyncClient 4.1.3 API and httpclient 4.5.3 API.
Version 1:
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
public void run() {
while (true) {
if (dataController.closeChannel)
return;
//Read in File Request
HttpGet[] httpRequests = fileRequests.getRequests();
//Create the HTTP Pipelined Client
CloseableHttpPipeliningClient httpClient = HttpAsyncClients.createPipelining();
httpClient.start();
try {
httpClient.execute(httpServer(), Arrays. < HttpRequest > asList(httpRequests), new FutureCallback < List < HttpResponse >> () {
@Override
public void completed(List < HttpResponse > responses) {
for (HttpResponse hr: responses) {
Header header = hr.getFirstHeader("Content-Length")
totalBytesReceived += Long.parseUnsignedLong(header.getValue());
byteMonitor.addBytes(totalBytesReceived);
}
responses.clear();
}
@Override
public void failed(final Exception ex) {
remainingRequests.countDown();
System.err.println("Request failed");
}
@Override
public void cancelled() {
remainingRequests.countDown();
System.out.println("Request cancelled");
}
});
} catch (Exception e) {
System.out.println("SOMETHING WENT WRONG WITH EXECUTE");
System.exit(0);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…