如何从DefaultHttpClient()升级到HttpClientBuilder.create()。build()?

时间:2014-03-25 11:23:11

标签: apache-httpclient-4.x apache-commons-httpclient

我有一个例程,用于检查Solr是否已将记录编入索引。我有一个弃用的方法来创建我尝试删除的HTTPClient:

  • DefaultHttpClient httpClient = new DefaultHttpClient();

  • CloseableHttpClient httpClient = HttpClientBuilder.create()。build();

我现在遇到的问题是,在2次调用URL后,第3次尝试似乎挂了。如果有人可以帮忙的话,我不太清楚我错过了什么?

这是我在测试中提取的完整方法:

@Test
public void checkUntilRecordAvailable() {
    String output;

    String solrSingleJobURL = "http://solr01.prod.efinancialcareers.com:8080/solr/jobSearchCollection/select?q=id%3A7618769%0A&fl=*&wt=json&indent=true";

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(solrSingleJobURL);

    StringBuilder jobResponseBuilder;
    Gson gson = new Gson();

    while (true) {
        System.out.print("WAITING FOR SOLR PARTIAL TO RUN " + solrSingleJobURL);

        jobResponseBuilder = new StringBuilder();

        try {
            HttpResponse response = httpClient.execute(httpGet);

            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

            while ((output = br.readLine()) != null) {
                System.out.println(output);
                jobResponseBuilder.append(output);
            }

            JobResponse jobResponse = gson.fromJson(jobResponseBuilder.toString(), JobResponse.class);
            Long numberOfRecordsFound = jobResponse.getNumberOfRecordsFound();

            if (numberOfRecordsFound == 0) {
                System.out.println("- PAUSE FOR 10 SECONDS UNTIL NEXT CHECK");

                Thread.sleep(5000);
            } else {
                System.out.println(" RECORD FOUND ");
                httpClient.close();
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这里是使用4.3 httpclient中的构建器的一些代码..不知道它是否有帮助..我使用来自here的骨架。因此,我将httpclient的创建包装在runnable中,并将其发布到用于EXEC的que处理器。请注意,runnable有你的构建器'其中的东西。

RequestConfig config = null;
private HttpClientContext context;

public void create(int method, final String url, final String data) {
    this.method = method;// GET, POST, HEAD, DELETE etc
    this.url = url;     
    this.data = data; //entity body of POST
    this.config = RequestConfig.custom()
         .setConnectionRequestTimeout(60 * 1000)
         .setSocketTimeout(60 * 1000)               
         .build();
    this.context = HttpClientContext.create();
    ConnectionMgr.getInstance().push(this);
    }

    //above creates a runnable that can be posted to a generic execution que

    //detls on run()  include builder asked about

public void run() {

    handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
    CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(YourConnectionMgr.getInstance())
    .addInterceptorLast(new HttpRequestInterceptor() {
         public void process(
           final HttpRequest request, 
           final HttpContext context) throws HttpException, IOException { 

if (request.getRequestLine().getMethod() == "POST"){
  request.addHeader("Content-Type", mimeType) ;}

}else if(request.getRequestLine().getMethod() == "GET" &&                                      !request.getRequestLine().getUri().toString().contains("ffmpeg")){
    request.addHeader("X-Parse-Application-Id", ParseApplication.key_appId);
}                                   

 }) .build();