| @@ -1,8 +1,6 @@ | |||||
| package me.chanjar.weixin.common.util.http; | package me.chanjar.weixin.common.util.http; | ||||
| import java.io.IOException; | |||||
| import java.util.concurrent.TimeUnit; | |||||
| import me.chanjar.weixin.common.util.StringUtils; | |||||
| import org.apache.http.annotation.NotThreadSafe; | import org.apache.http.annotation.NotThreadSafe; | ||||
| import org.apache.http.auth.AuthScope; | import org.apache.http.auth.AuthScope; | ||||
| import org.apache.http.auth.UsernamePasswordCredentials; | import org.apache.http.auth.UsernamePasswordCredentials; | ||||
| @@ -22,8 +20,11 @@ import org.apache.http.impl.client.HttpClientBuilder; | |||||
| import org.apache.http.impl.client.HttpClients; | import org.apache.http.impl.client.HttpClients; | ||||
| import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||||
| import org.apache.http.protocol.HttpContext; | import org.apache.http.protocol.HttpContext; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import me.chanjar.weixin.common.util.StringUtils; | |||||
| import java.io.IOException; | |||||
| import java.util.concurrent.TimeUnit; | |||||
| /** | /** | ||||
| * httpclient 连接管理器 | * httpclient 连接管理器 | ||||
| @@ -31,6 +32,7 @@ import me.chanjar.weixin.common.util.StringUtils; | |||||
| */ | */ | ||||
| @NotThreadSafe | @NotThreadSafe | ||||
| public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | ||||
| protected final Logger log = LoggerFactory.getLogger(DefaultApacheHttpClientBuilder.class); | |||||
| private int connectionRequestTimeout = 3000; | private int connectionRequestTimeout = 3000; | ||||
| private int connectionTimeout = 5000; | private int connectionTimeout = 5000; | ||||
| private int soTimeout = 5000; | private int soTimeout = 5000; | ||||
| @@ -98,6 +100,82 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | |||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * 获取链接的超时时间设置,默认3000ms | |||||
| * <p> | |||||
| * 设置为零时不超时,一直等待. | |||||
| * 设置为负数是使用系统默认设置(非上述的3000ms的默认值,而是httpclient的默认设置). | |||||
| * </p> | |||||
| * @param connectionRequestTimeout 获取链接的超时时间设置(单位毫秒),默认3000ms | |||||
| */ | |||||
| public void setConnectionRequestTimeout(int connectionRequestTimeout) { | |||||
| this.connectionRequestTimeout = connectionRequestTimeout; | |||||
| } | |||||
| /** | |||||
| * 建立链接的超时时间,默认为5000ms.由于是在链接池获取链接,此设置应该并不起什么作用 | |||||
| * <p> | |||||
| * 设置为零时不超时,一直等待. | |||||
| * 设置为负数是使用系统默认设置(非上述的5000ms的默认值,而是httpclient的默认设置). | |||||
| * </p> | |||||
| * @param connectionTimeout 建立链接的超时时间设置(单位毫秒),默认5000ms | |||||
| */ | |||||
| public void setConnectionTimeout(int connectionTimeout) { | |||||
| this.connectionTimeout = connectionTimeout; | |||||
| } | |||||
| /** | |||||
| * 默认NIO的socket超时设置,默认5000ms. | |||||
| * @see java.net.SocketOptions#SO_TIMEOUT | |||||
| * @param soTimeout 默认NIO的socket超时设置,默认5000ms. | |||||
| */ | |||||
| public void setSoTimeout(int soTimeout) { | |||||
| this.soTimeout = soTimeout; | |||||
| } | |||||
| /** | |||||
| * 空闲链接的超时时间,默认60000ms. | |||||
| * <p> | |||||
| * 超时的链接将在下一次空闲链接检查是被销毁 | |||||
| * </p> | |||||
| * @param idleConnTimeout 空闲链接的超时时间,默认60000ms. | |||||
| */ | |||||
| public void setIdleConnTimeout(int idleConnTimeout) { | |||||
| this.idleConnTimeout = idleConnTimeout; | |||||
| } | |||||
| /** | |||||
| * 检查空间链接的间隔周期,默认60000ms. | |||||
| * @param checkWaitTime 检查空间链接的间隔周期,默认60000ms. | |||||
| */ | |||||
| public void setCheckWaitTime(int checkWaitTime) { | |||||
| this.checkWaitTime = checkWaitTime; | |||||
| } | |||||
| /** | |||||
| * 每路的最大链接数,默认10 | |||||
| * @param maxConnPerHost 每路的最大链接数,默认10 | |||||
| */ | |||||
| public void setMaxConnPerHost(int maxConnPerHost) { | |||||
| this.maxConnPerHost = maxConnPerHost; | |||||
| } | |||||
| /** | |||||
| * 最大总连接数,默认50 | |||||
| * @param maxTotalConn 最大总连接数,默认50 | |||||
| */ | |||||
| public void setMaxTotalConn(int maxTotalConn) { | |||||
| this.maxTotalConn = maxTotalConn; | |||||
| } | |||||
| /** | |||||
| * 自定义httpclient的User Agent | |||||
| * @param userAgent User Agent | |||||
| */ | |||||
| public void setUserAgent(String userAgent) { | |||||
| this.userAgent = userAgent; | |||||
| } | |||||
| public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { | public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { | ||||
| return this.idleConnectionMonitorThread; | return this.idleConnectionMonitorThread; | ||||
| } | } | ||||