Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

sockets - How to check number of bytes downloaded by Apache’s asynchronous CloseableHttpPipeliningClient without waiting for entire response list to be received

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);
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...