本文介绍了如何从DefaultHttpClient()升级到HttpClientBuilder.create().build()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例程,用于检查Solr是否已对记录进行索引.我有一个过时的创建HTTPClient的方法,该方法正试图删除:

I have a routine which checks if a record has been indexed by Solr. I have a deprecated method of creating a HTTPClient which I'm trying to remove:

  • 来自

  • From

DefaultHttpClient httpClient =新的DefaultHttpClient();

DefaultHttpClient httpClient = new DefaultHttpClient();

收件人

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

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

我现在遇到的问题是两次调用URL后,第三次尝试似乎挂起.我不确定如果有人可以帮助我会缺少什么?

The problem I now have is that after 2 call to the URL, the 3rd attempt seems to hang. I'm not quite sure what I'm missing if anyone can help please?

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

This is my complete method which I've extracted out into a test:

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

推荐答案

以下是一些使用4.3 httpclient生成器的代码..不知道是否有帮助..我使用了此处.因此,我将httpclient的创建包装在可运行文件中,并将其发布到EXEC的查询处理器中.请注意,可运行对象中包含构建器"内容.

here is some code using the builders from 4.3 httpclient.. dont know if it helps .. I use skeleton from here. So , i wrap the creation of the httpclient in a runnable and post it to a que-processor for the EXEC. Note the runnable has your 'builder' stuff in it.

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

这篇关于如何从DefaultHttpClient()升级到HttpClientBuilder.create().build()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:27