ソースを参照

Young - 增加 HTTP 超时配置

Young 2 年 前
コミット
1f9926f78e

+ 42 - 0
support-http/src/main/java/cn/nosum/http/RequestHttpProperties.java

@@ -0,0 +1,42 @@
+package cn.nosum.http;
+
+import lombok.Data;
+
+/**
+ * HTTP 相关配置.
+ *
+ * @author Young
+ */
+@Data
+public class RequestHttpProperties {
+
+    /**
+     * http proxy host.
+     */
+    private String httpProxyHost;
+
+    /**
+     * http proxy port.
+     */
+    private int httpProxyPort;
+
+    /**
+     * http proxy username.
+     */
+    private String httpProxyUsername;
+
+    /**
+     * http proxy password.
+     */
+    private String httpProxyPassword;
+
+    /**
+     * http connectTimeoutSecond.
+     */
+    private Integer connectTimeout = 5000;
+
+    /**
+     * http readTimeoutSecond.
+     */
+    private Integer readTimeout = 5000;
+}

+ 16 - 0
support-http/src/main/java/cn/nosum/http/apache/ApacheHttpClientBuilder.java

@@ -50,6 +50,22 @@ public interface ApacheHttpClientBuilder {
     ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword);
 
     /**
+     * 连接超时时间.
+     *
+     * @param connTimeout 连接超时时间
+     * @return httpclient build interface.
+     */
+    ApacheHttpClientBuilder connectTimeout(Integer connTimeout);
+
+    /**
+     * 请求超时时间.
+     *
+     * @param readTimeout 请求超时时间
+     * @return httpclient build interface.
+     */
+    ApacheHttpClientBuilder readTimeout(Integer readTimeout);
+
+    /**
      * ssl连接socket工厂.
      *
      * @param sslConnectionSocketFactory SSL 连接工厂

+ 12 - 0
support-http/src/main/java/cn/nosum/http/apache/ApacheHttpDnsClientBuilder.java

@@ -92,6 +92,18 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder {
     }
 
     @Override
+    public ApacheHttpClientBuilder connectTimeout(Integer connTimeout) {
+        this.connectionTimeout = connTimeout;
+        return this;
+    }
+
+    @Override
+    public ApacheHttpClientBuilder readTimeout(Integer readTimeout) {
+        this.soTimeout = readTimeout;
+        return this;
+    }
+
+    @Override
     public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
         this.sslConnectionSocketFactory = sslConnectionSocketFactory;
         return this;

+ 14 - 7
support-http/src/main/java/cn/nosum/http/apache/DefaultApacheHttpClientBuilder.java

@@ -139,6 +139,18 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
     }
 
     @Override
+    public ApacheHttpClientBuilder connectTimeout(Integer connTimeout) {
+        this.connectionTimeout = connTimeout;
+        return this;
+    }
+
+    @Override
+    public ApacheHttpClientBuilder readTimeout(Integer readTimeout) {
+        this.soTimeout = readTimeout;
+        return this;
+    }
+
+    @Override
     public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
         this.sslConnectionSocketFactory = sslConnectionSocketFactory;
         return this;
@@ -160,14 +172,9 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
         PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
         connectionManager.setMaxTotal(this.maxTotalConn);
         connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
-        connectionManager.setDefaultSocketConfig(
-                SocketConfig.copy(SocketConfig.DEFAULT)
-                        .setSoTimeout(this.soTimeout)
-                        .build()
-        );
+        connectionManager.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(this.soTimeout).build());
 
-        this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(
-                connectionManager, this.idleConnTimeout, this.checkWaitTime);
+        this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, this.idleConnTimeout, this.checkWaitTime);
         this.idleConnectionMonitorThread.setDaemon(true);
         this.idleConnectionMonitorThread.start();
 

+ 48 - 0
support-http/src/main/java/cn/nosum/http/jodd/JoddSocketHttpConnectionProvider.java

@@ -0,0 +1,48 @@
+package cn.nosum.http.jodd;
+
+import jodd.http.net.SocketHttpConnectionProvider;
+
+import javax.net.SocketFactory;
+import java.io.IOException;
+import java.net.Socket;
+
+/**
+ * JODD SocketHttpConnectionProvider.
+ *
+ * @author Young
+ */
+public class JoddSocketHttpConnectionProvider extends SocketHttpConnectionProvider {
+
+    /**
+     * socket timeout.
+     */
+    private Integer soTimeout;
+
+    /**
+     * connection timeout.
+     */
+    private Integer connectionTimeout;
+
+    public void setSoTimeout(Integer soTimeout) {
+        this.soTimeout = soTimeout;
+    }
+
+    public Integer getSoTimeout() {
+        return soTimeout;
+    }
+
+    public void setConnectionTimeout(Integer connectionTimeout) {
+        this.connectionTimeout = connectionTimeout;
+    }
+
+    public Integer getConnectionTimeout() {
+        return connectionTimeout;
+    }
+
+    @Override
+    protected Socket createSocket(String host, int port, int connectionTimeout) throws IOException {
+        Socket socket = super.createSocket(host, port, this.connectionTimeout);
+        socket.setSoTimeout(this.soTimeout);
+        return socket;
+    }
+}

+ 14 - 4
wx-java-tools/wx-java-cp-start/src/main/java/cn/nosum/wx/cp/config/WxCpConfiguration.java

@@ -1,5 +1,6 @@
 package cn.nosum.wx.cp.config;
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.http.enums.HttpType;
 import cn.nosum.wx.cp.api.WxCpService;
 import cn.nosum.wx.cp.api.impl.WxCpServiceApacheHttpClientImpl;
@@ -8,6 +9,7 @@ import cn.nosum.wx.cp.api.impl.WxCpServiceOkHttpImpl;
 import cn.nosum.wx.cp.config.impl.WxCpDefaultConfigImpl;
 import lombok.val;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.http.impl.BHttpConnectionBase;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
@@ -47,19 +49,27 @@ public class WxCpConfiguration {
             configStorage.setToken(agent.getToken());
             configStorage.setAesKey(agent.getAesKey());
 
+            // HTTP 配置
+            RequestHttpProperties httpProperties = configStorage.getHttpProperties();
+            httpProperties.setReadTimeout(properties.getHttp().getReadTimeout());
+            httpProperties.setConnectTimeout(properties.getHttp().getConnectTimeout());
+
             // 设置代理对象
             WxCpHttpProxyProperties proxy = properties.getHttp().getProxy();
             if (proxy.isEnabled()) {
-                configStorage.setHttpProxyHost(proxy.getHost());
-                configStorage.setHttpProxyPort(proxy.getPort());
-                configStorage.setHttpProxyUsername(proxy.getUserName());
-                configStorage.setHttpProxyPassword(proxy.getPassword());
+                httpProperties.setHttpProxyHost(proxy.getHost());
+                httpProperties.setHttpProxyPort(proxy.getPort());
+                httpProperties.setHttpProxyUsername(proxy.getUserName());
+                httpProperties.setHttpProxyPassword(proxy.getPassword());
             }
+
             WxCpService service = null;
 
             String type = properties.getHttp().getType();
             if (StringUtils.isNotBlank(type)) {
                 if (type.equals(HttpType.APACHE_HTTP.getName())) {
+                    WxCpServiceApacheHttpClientImpl wxCpServiceApacheHttpClient = new WxCpServiceApacheHttpClientImpl();
+                    wxCpServiceApacheHttpClient.getApacheHttpClientBuilder().build();
                     service = new WxCpServiceApacheHttpClientImpl();
                 }
                 if (type.equals(HttpType.JODD_HTTP.getName())) {

+ 11 - 1
wx-java-tools/wx-java-cp-start/src/main/java/cn/nosum/wx/cp/config/WxCpHttpProperties.java

@@ -13,11 +13,21 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 public class WxCpHttpProperties {
 
     /**
-     * 请求类型
+     * 请求类型.
      */
     private String type;
 
     /**
+     * 连接超时时间.
+     */
+    private Integer connectTimeout = 5000;
+
+    /**
+     * 请求超时时间.
+     */
+    private Integer readTimeout = 5000;
+
+    /**
      * 代理配置
      */
     private WxCpHttpProxyProperties proxy;

+ 2 - 1
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/api/impl/BaseWxCpServiceImpl.java

@@ -29,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
@@ -275,7 +276,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
                     int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
                     try {
                         log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
-                        Thread.sleep(sleepMillis);
+                        TimeUnit.MILLISECONDS.sleep(sleepMillis);
                     } catch (InterruptedException interruptedException) {
                         Thread.currentThread().interrupt();
                     }

+ 10 - 6
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/api/impl/WxCpServiceApacheHttpClientImpl.java

@@ -1,6 +1,7 @@
 package cn.nosum.wx.cp.api.impl;
 
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.http.apache.ApacheHttpClientBuilder;
 import cn.nosum.http.apache.DefaultApacheHttpClientBuilder;
 import cn.nosum.http.enums.HttpType;
@@ -25,13 +26,16 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
             apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
         }
 
-        apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
-                .httpProxyPort(this.configStorage.getHttpProxyPort())
-                .httpProxyUsername(this.configStorage.getHttpProxyUsername())
-                .httpProxyPassword(this.configStorage.getHttpProxyPassword());
+        RequestHttpProperties httpProperties = this.configStorage.getHttpProperties();
+        apacheHttpClientBuilder.httpProxyHost(httpProperties.getHttpProxyHost())
+                .httpProxyPort(httpProperties.getHttpProxyPort())
+                .httpProxyUsername(httpProperties.getHttpProxyUsername())
+                .httpProxyPassword(httpProperties.getHttpProxyPassword())
+                .connectTimeout(httpProperties.getConnectTimeout())
+                .readTimeout(httpProperties.getReadTimeout());
 
-        if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
-            this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
+        if (httpProperties.getHttpProxyHost() != null && httpProperties.getHttpProxyPort() > 0) {
+            this.httpProxy = new HttpHost(httpProperties.getHttpProxyHost(), httpProperties.getHttpProxyPort());
         }
 
         this.httpClient = apacheHttpClientBuilder.build();

+ 13 - 7
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/api/impl/WxCpServiceJoddHttpImpl.java

@@ -1,11 +1,12 @@
 package cn.nosum.wx.cp.api.impl;
 
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.http.enums.HttpType;
+import cn.nosum.http.jodd.JoddSocketHttpConnectionProvider;
 import cn.nosum.wx.cp.config.WxCpConfigStorage;
 import jodd.http.HttpConnectionProvider;
 import jodd.http.ProxyInfo;
-import jodd.http.net.SocketHttpConnectionProvider;
 
 /**
  * 企业微信API服务层JODD实现.
@@ -19,16 +20,21 @@ public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionP
 
     @Override
     public void initHttp() {
-        if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
+        RequestHttpProperties httpProperties = this.configStorage.getHttpProperties();
+        if (httpProperties.getHttpProxyHost() != null && httpProperties.getHttpProxyPort() > 0) {
             httpProxy = new ProxyInfo(ProxyInfo.ProxyType.HTTP,
-                    configStorage.getHttpProxyHost(),
-                    configStorage.getHttpProxyPort(),
-                    configStorage.getHttpProxyUsername(),
-                    configStorage.getHttpProxyPassword());
+                    httpProperties.getHttpProxyHost(),
+                    httpProperties.getHttpProxyPort(),
+                    httpProperties.getHttpProxyUsername(),
+                    httpProperties.getHttpProxyPassword());
         }
-        httpClient = new SocketHttpConnectionProvider();
+        JoddSocketHttpConnectionProvider joddSocketHttpConnectionProvider = new JoddSocketHttpConnectionProvider();
+        joddSocketHttpConnectionProvider.setSoTimeout(httpProperties.getReadTimeout());
+        joddSocketHttpConnectionProvider.setConnectionTimeout(httpProperties.getConnectTimeout());
+        httpClient = joddSocketHttpConnectionProvider;
     }
 
+
     @Override
     public HttpConnectionProvider getRequestHttpClient() {
         return httpClient;

+ 14 - 6
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/api/impl/WxCpServiceOkHttpImpl.java

@@ -1,5 +1,6 @@
 package cn.nosum.wx.cp.api.impl;
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.http.enums.HttpType;
 import cn.nosum.http.okhttp.OkHttpProxyInfo;
 import cn.nosum.wx.cp.config.WxCpConfigStorage;
@@ -7,6 +8,8 @@ import lombok.extern.slf4j.Slf4j;
 import okhttp3.Credentials;
 import okhttp3.OkHttpClient;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * 企业微信API服务层 OKHTTP3 实现.
  *
@@ -21,12 +24,12 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH
 
     @Override
     public void initHttp() {
-        //设置代理
-        if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
-            httpProxy = OkHttpProxyInfo.httpProxy(configStorage.getHttpProxyHost(),
-                    configStorage.getHttpProxyPort(),
-                    configStorage.getHttpProxyUsername(),
-                    configStorage.getHttpProxyPassword());
+        RequestHttpProperties httpProperties = this.configStorage.getHttpProperties();
+        if (httpProperties.getHttpProxyHost() != null && httpProperties.getHttpProxyPort() > 0) {
+            httpProxy = OkHttpProxyInfo.httpProxy(httpProperties.getHttpProxyHost(),
+                    httpProperties.getHttpProxyPort(),
+                    httpProperties.getHttpProxyUsername(),
+                    httpProperties.getHttpProxyPassword());
         }
 
         OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
@@ -39,6 +42,11 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH
                         .build();
             });
         }
+
+        clientBuilder
+                .connectTimeout(httpProperties.getConnectTimeout(), TimeUnit.MILLISECONDS)
+                .readTimeout(httpProperties.getReadTimeout(),TimeUnit.MILLISECONDS);
+
         httpClient = clientBuilder.build();
     }
 

+ 8 - 29
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/config/WxCpConfigStorage.java

@@ -1,5 +1,6 @@
 package cn.nosum.wx.cp.config;
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.wx.common.entity.WxAccessToken;
 
 import java.io.File;
@@ -195,35 +196,6 @@ public interface WxCpConfigStorage {
     String getOauth2redirectUri();
 
     /**
-     * Gets http proxy host.
-     *
-     * @return the http proxy host
-     */
-    String getHttpProxyHost();
-
-    /**
-     * Gets http proxy port.
-     *
-     * @return the http proxy port
-     */
-    int getHttpProxyPort();
-
-    /**
-     * Gets http proxy username.
-     *
-     * @return the http proxy username
-     */
-    String getHttpProxyUsername();
-
-    /**
-     * Gets http proxy password.
-     *
-     * @return the http proxy password
-     */
-    String getHttpProxyPassword();
-
-
-    /**
      * 是否自动刷新token
      *
      * @return . boolean
@@ -236,4 +208,11 @@ public interface WxCpConfigStorage {
      * @return key webhook key
      */
     String getWebhookKey();
+
+    /**
+     * 获取 HTTP 配置信息.
+     *
+     * @return HTTP 配置信息
+     */
+    RequestHttpProperties getHttpProperties();
 }

+ 17 - 68
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/config/impl/WxCpDefaultConfigImpl.java

@@ -1,11 +1,11 @@
 package cn.nosum.wx.cp.config.impl;
 
+import cn.nosum.http.RequestHttpProperties;
 import cn.nosum.wx.common.entity.WxAccessToken;
 import cn.nosum.wx.cp.config.WxCpConfigStorage;
 import cn.nosum.wx.cp.constant.WxCpApiPathConsts;
 import cn.nosum.wx.cp.utils.json.WxCpGsonBuilder;
 
-import java.io.File;
 import java.io.Serializable;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -48,23 +48,23 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
      */
     protected transient Lock agentJsapiTicketLock = new ReentrantLock();
 
+
+    /**
+     * The Http Properties.
+     */
+    private final RequestHttpProperties httpProperties = new RequestHttpProperties();
+
     private volatile String corpId;
     private volatile String corpSecret;
     private volatile String token;
     private volatile String aesKey;
     private volatile long expiresTime;
     private volatile String oauth2redirectUri;
-    private volatile String httpProxyHost;
-    private volatile int httpProxyPort;
-    private volatile String httpProxyUsername;
-    private volatile String httpProxyPassword;
     private volatile String jsapiTicket;
     private volatile long jsapiTicketExpiresTime;
     private volatile String agentJsapiTicket;
     private volatile long agentJsapiTicketExpiresTime;
-
     private volatile String baseApiUrl;
-
     private volatile String webhookKey;
 
     @Override
@@ -314,67 +314,6 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
     }
 
     @Override
-    public String getHttpProxyHost() {
-        return this.httpProxyHost;
-    }
-
-    /**
-     * Sets http proxy host.
-     *
-     * @param httpProxyHost the http proxy host
-     */
-    public void setHttpProxyHost(String httpProxyHost) {
-        this.httpProxyHost = httpProxyHost;
-    }
-
-    @Override
-    public int getHttpProxyPort() {
-        return this.httpProxyPort;
-    }
-
-    /**
-     * Sets http proxy port.
-     *
-     * @param httpProxyPort the http proxy port
-     */
-    public void setHttpProxyPort(int httpProxyPort) {
-        this.httpProxyPort = httpProxyPort;
-    }
-
-    @Override
-    public String getHttpProxyUsername() {
-        return this.httpProxyUsername;
-    }
-
-    /**
-     * Sets http proxy username.
-     *
-     * @param httpProxyUsername the http proxy username
-     */
-    public void setHttpProxyUsername(String httpProxyUsername) {
-        this.httpProxyUsername = httpProxyUsername;
-    }
-
-    @Override
-    public String getHttpProxyPassword() {
-        return this.httpProxyPassword;
-    }
-
-    /**
-     * Sets http proxy password.
-     *
-     * @param httpProxyPassword the http proxy password
-     */
-    public void setHttpProxyPassword(String httpProxyPassword) {
-        this.httpProxyPassword = httpProxyPassword;
-    }
-
-    @Override
-    public String toString() {
-        return WxCpGsonBuilder.create().toJson(this);
-    }
-
-    @Override
     public boolean autoRefreshToken() {
         return true;
     }
@@ -384,6 +323,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
         return this.webhookKey;
     }
 
+    @Override
+    public RequestHttpProperties getHttpProperties() {
+        return this.httpProperties;
+    }
+
     /**
      * Sets webhook key.
      *
@@ -394,4 +338,9 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
         this.webhookKey = webhookKey;
         return this;
     }
+
+    @Override
+    public String toString() {
+        return WxCpGsonBuilder.create().toJson(this);
+    }
 }