@Test public void testBuild() throws Exception { DefaultApacheHttpClientBuilder builder1 = DefaultApacheHttpClientBuilder.get(); DefaultApacheHttpClientBuilder builder2 = DefaultApacheHttpClientBuilder.get(); Assert.assertSame(builder1, builder2, "DefaultApacheHttpClientBuilder为单例,获取到的对象应该相同"); List<TestThread> threadList = new ArrayList<>(10); for (int i = 0; i < 10; i++) { TestThread thread = new TestThread(); thread.start(); threadList.add(thread); } for (TestThread testThread : threadList) { testThread.join(); Assert.assertNotEquals(-1,testThread.getRespState(),"请求响应code不应为-1"); } for (int i = 1; i < threadList.size(); i++) { TestThread thread1 = threadList.get(i - 1); TestThread thread2 = threadList.get(i); Assert.assertSame( thread1.getClient(), thread2.getClient(), "DefaultApacheHttpClientBuilder为单例,并持有了相同的HttpClient" ); } }
@Override public CloseableHttpClient build() { if (!prepared.get()) { prepare(); } return this.closeableHttpClient; }
DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { @Override public CloseableHttpClient build() { if (!prepared.get()) { prepare(); } return this.closeableHttpClient; } }
DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { @Override public CloseableHttpClient build() { if (!prepared.get()) { prepare(); } return this.closeableHttpClient; } private  DefaultApacheHttpClientBuilder(); }
DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { @Override public CloseableHttpClient build() { if (!prepared.get()) { prepare(); } return this.closeableHttpClient; } private  DefaultApacheHttpClientBuilder(); static DefaultApacheHttpClientBuilder get(); @Override ApacheHttpClientBuilder httpProxyHost(String httpProxyHost); @Override ApacheHttpClientBuilder httpProxyPort(int httpProxyPort); @Override ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername); @Override ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword); @Override ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory); void setConnectionRequestTimeout(int connectionRequestTimeout); void setConnectionTimeout(int connectionTimeout); void setSoTimeout(int soTimeout); void setIdleConnTimeout(int idleConnTimeout); void setCheckWaitTime(int checkWaitTime); void setMaxConnPerHost(int maxConnPerHost); void setMaxTotalConn(int maxTotalConn); void setUserAgent(String userAgent); IdleConnectionMonitorThread getIdleConnectionMonitorThread(); @Override CloseableHttpClient build(); }
DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { @Override public CloseableHttpClient build() { if (!prepared.get()) { prepare(); } return this.closeableHttpClient; } private  DefaultApacheHttpClientBuilder(); static DefaultApacheHttpClientBuilder get(); @Override ApacheHttpClientBuilder httpProxyHost(String httpProxyHost); @Override ApacheHttpClientBuilder httpProxyPort(int httpProxyPort); @Override ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername); @Override ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword); @Override ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory); void setConnectionRequestTimeout(int connectionRequestTimeout); void setConnectionTimeout(int connectionTimeout); void setSoTimeout(int soTimeout); void setIdleConnTimeout(int idleConnTimeout); void setCheckWaitTime(int checkWaitTime); void setMaxConnPerHost(int maxConnPerHost); void setMaxTotalConn(int maxTotalConn); void setUserAgent(String userAgent); IdleConnectionMonitorThread getIdleConnectionMonitorThread(); @Override CloseableHttpClient build();  }