From d435bc9c428429cf283a638efc2a53b1b019d3c9 Mon Sep 17 00:00:00 2001 From: OPGames <1242479791@qq.com> Date: Sat, 10 Aug 2019 19:33:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8hutool=E7=9A=84http=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=87=BA=E7=8E=B0?= =?UTF-8?q?Connection=20pool=20shut=20down=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mp/component/WxMaServiceClusterImpl.java | 36 ++++++------------- .../mp/component/WxMpServiceClusterImpl.java | 36 ++++++------------- 2 files changed, 20 insertions(+), 52 deletions(-) diff --git a/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMaServiceClusterImpl.java b/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMaServiceClusterImpl.java index 4c58a72..3ebbd5f 100644 --- a/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMaServiceClusterImpl.java +++ b/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMaServiceClusterImpl.java @@ -2,18 +2,14 @@ package com.yami.shop.mp.component; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.hutool.http.HttpUtil; import com.yami.shop.common.exception.YamiShopBindException; import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicResponseHandler; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; -import java.io.IOException; import java.util.concurrent.TimeUnit; /** @@ -57,28 +53,16 @@ public class WxMaServiceClusterImpl extends WxMaServiceImpl { String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); - try { - HttpGet httpGet = new HttpGet(url); - if (this.getRequestHttpProxy() != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); - httpGet.setConfig(config); - } - try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { - String resultContent = new BasicResponseHandler().handleResponse(response); - WxError error = WxError.fromJson(resultContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); - this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); - - return this.getWxMaConfig().getAccessToken(); - } finally { - httpGet.releaseConnection(); - } - } catch (IOException e) { - throw new RuntimeException(e); + String resultContent = HttpUtil.get(url); + WxError error = WxError.fromJson(resultContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); } + WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); + this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); + + return this.getWxMaConfig().getAccessToken(); + } finally { rLock.unlock(); } diff --git a/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMpServiceClusterImpl.java b/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMpServiceClusterImpl.java index ccfe6d9..5eec0ec 100644 --- a/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMpServiceClusterImpl.java +++ b/yami-shop-mp/src/main/java/com/yami/shop/mp/component/WxMpServiceClusterImpl.java @@ -1,5 +1,6 @@ package com.yami.shop.mp.component; +import cn.hutool.http.HttpUtil; import com.yami.shop.common.exception.YamiShopBindException; import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.bean.WxAccessToken; @@ -7,14 +8,9 @@ import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicResponseHandler; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; -import java.io.IOException; import java.util.concurrent.TimeUnit; /** @@ -40,7 +36,6 @@ public class WxMpServiceClusterImpl extends WxMpServiceImpl { RLock rLock = redissonClient.getLock(REDISSON_LOCK_PREFIX + ":WxMpServiceCluster:getAccessToken"); - Object result = null; try { boolean doingUpdateAccessToken; try { @@ -56,29 +51,18 @@ public class WxMpServiceClusterImpl extends WxMpServiceImpl { if (this.getWxMpConfigStorage().isAccessTokenExpired()) { return this.getWxMpConfigStorage().getAccessToken(); } + String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); - try { - HttpGet httpGet = new HttpGet(url); - if (this.getRequestHttpProxy() != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); - httpGet.setConfig(config); - } - try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { - String resultContent = new BasicResponseHandler().handleResponse(response); - WxError error = WxError.fromJson(resultContent, WxType.MP); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); - this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); - return this.getWxMpConfigStorage().getAccessToken(); - } finally { - httpGet.releaseConnection(); - } - } catch (IOException e) { - throw new RuntimeException(e); + String resultContent = HttpUtil.get(url); + + WxError error = WxError.fromJson(resultContent, WxType.MP); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); } + WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); + this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); + return this.getWxMpConfigStorage().getAccessToken(); } finally { rLock.unlock();