Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultApacheHttpClientBuilder修改为单例模式 #310

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
* 闲置连接监控线程
*/
private IdleConnectionMonitorThread idleConnectionMonitorThread;
private HttpClientBuilder httpClientBuilder;
/**
* 持有client对象,仅初始化一次,避免多service实例的时候造成重复初始化的问题
*/
private CloseableHttpClient closeableHttpClient;

private DefaultApacheHttpClientBuilder() {
}

public static DefaultApacheHttpClientBuilder get() {
return new DefaultApacheHttpClientBuilder();
return DefaultApacheHttpClientBuilder.SingletonHolder.INSTANCE;
}

@Override
Expand Down Expand Up @@ -219,7 +222,7 @@ private synchronized void prepare() {
this.idleConnectionMonitorThread.setDaemon(true);
this.idleConnectionMonitorThread.start();

this.httpClientBuilder = HttpClients.custom()
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.setSSLSocketFactory(this.buildSSLConnectionSocketFactory())
Expand All @@ -240,12 +243,13 @@ private synchronized void prepare() {
new AuthScope(this.httpProxyHost, this.httpProxyPort),
new UsernamePasswordCredentials(this.httpProxyUsername,
this.httpProxyPassword));
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
httpClientBuilder.setDefaultCredentialsProvider(provider);
}

if (StringUtils.isNotBlank(this.userAgent)) {
this.httpClientBuilder.setUserAgent(this.userAgent);
httpClientBuilder.setUserAgent(this.userAgent);
}
this.closeableHttpClient = httpClientBuilder.build();
prepared.set(true);
}

Expand Down Expand Up @@ -277,7 +281,14 @@ public CloseableHttpClient build() {
if (!prepared.get()) {
prepare();
}
return this.httpClientBuilder.build();
return this.closeableHttpClient;
}

/**
* DefaultApacheHttpClientBuilder 改为单例模式,并持有唯一的CloseableHttpClient(仅首次调用创建)
*/
private static class SingletonHolder {
private static final DefaultApacheHttpClientBuilder INSTANCE = new DefaultApacheHttpClientBuilder();
}

public static class IdleConnectionMonitorThread extends Thread {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.chanjar.weixin.common.util.http.apache;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DefaultApacheHttpClientBuilderTest {
@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"
);
}
}


public static class TestThread extends Thread {
private CloseableHttpClient client;
private int respState = -1;

@Override
public void run() {
client = DefaultApacheHttpClientBuilder.get().build();
HttpGet httpGet = new HttpGet("http://www.sina.com.cn/");
try (CloseableHttpResponse resp = client.execute(httpGet)){
respState = resp.getStatusLine().getStatusCode();
} catch (IOException ignored) {
}
}

public CloseableHttpClient getClient() {
return client;
}

public int getRespState() {
return respState;
}
}
}