4646public class WxCpServiceImpl implements WxCpService {
4747
4848 /**
49- * 全局的是否正在刷新Access Token的flag
50- * true: 正在刷新
51- * false: 没有刷新
49+ * 全局的是否正在刷新access token的锁
5250 */
53- protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean ( false );
51+ protected static final Object GLOBAL_ACCESS_TOKEN_REFRESH_LOCK = new Object ( );
5452
5553 protected WxCpConfigStorage wxCpConfigStorage ;
5654
@@ -73,45 +71,40 @@ public void userAuthenticated(String userId) throws WxErrorException {
7371 execute (new SimpleGetRequestExecutor (), url , null );
7472 }
7573
76- public void accessTokenRefresh () throws WxErrorException {
77- if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .getAndSet (true )) {
78- try {
79- String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
80- + "&corpid=" + wxCpConfigStorage .getCorpId ()
81- + "&corpsecret=" + wxCpConfigStorage .getCorpSecret ();
82- try {
83- HttpGet httpGet = new HttpGet (url );
84- if (httpProxy != null ) {
85- RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
86- httpGet .setConfig (config );
87- }
88- CloseableHttpClient httpclient = getHttpclient ();
89- CloseableHttpResponse response = httpclient .execute (httpGet );
90- String resultContent = new BasicResponseHandler ().handleResponse (response );
91- WxError error = WxError .fromJson (resultContent );
92- if (error .getErrorCode () != 0 ) {
93- throw new WxErrorException (error );
74+ public String getAccessToken (boolean forceRefresh ) throws WxErrorException {
75+ if (forceRefresh ) {
76+ wxCpConfigStorage .expireAccessToken ();
77+ }
78+ if (wxCpConfigStorage .isAccessTokenExpired ()) {
79+ synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK ) {
80+ if (wxCpConfigStorage .isAccessTokenExpired ()) {
81+ String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
82+ + "&corpid=" + wxCpConfigStorage .getCorpId ()
83+ + "&corpsecret=" + wxCpConfigStorage .getCorpSecret ();
84+ try {
85+ HttpGet httpGet = new HttpGet (url );
86+ if (httpProxy != null ) {
87+ RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
88+ httpGet .setConfig (config );
89+ }
90+ CloseableHttpClient httpclient = getHttpclient ();
91+ CloseableHttpResponse response = httpclient .execute (httpGet );
92+ String resultContent = new BasicResponseHandler ().handleResponse (response );
93+ WxError error = WxError .fromJson (resultContent );
94+ if (error .getErrorCode () != 0 ) {
95+ throw new WxErrorException (error );
96+ }
97+ WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
98+ wxCpConfigStorage .updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
99+ } catch (ClientProtocolException e ) {
100+ throw new RuntimeException (e );
101+ } catch (IOException e ) {
102+ throw new RuntimeException (e );
94103 }
95- WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
96- wxCpConfigStorage .updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
97- } catch (ClientProtocolException e ) {
98- throw new RuntimeException (e );
99- } catch (IOException e ) {
100- throw new RuntimeException (e );
101- }
102- } finally {
103- GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .set (false );
104- }
105- } else {
106- // 每隔100ms检查一下是否刷新完毕了
107- while (GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .get ()) {
108- try {
109- Thread .sleep (100 );
110- } catch (InterruptedException e ) {
111104 }
112105 }
113- // 刷新完毕了,就没他什么事儿了
114106 }
107+ return wxCpConfigStorage .getAccessToken ();
115108 }
116109
117110 public void messageSend (WxCpMessage message ) throws WxErrorException {
@@ -369,10 +362,7 @@ public String post(String url, String postData) throws WxErrorException {
369362 * @throws WxErrorException
370363 */
371364 public <T , E > T execute (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
372- if (StringUtils .isBlank (wxCpConfigStorage .getAccessToken ())) {
373- accessTokenRefresh ();
374- }
375- String accessToken = wxCpConfigStorage .getAccessToken ();
365+ String accessToken = getAccessToken (false );
376366
377367 String uriWithAccessToken = uri ;
378368 uriWithAccessToken += uri .indexOf ('?' ) == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken ;
@@ -387,7 +377,8 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
387377 * 42001 access_token超时
388378 */
389379 if (error .getErrorCode () == 42001 || error .getErrorCode () == 40001 ) {
390- accessTokenRefresh ();
380+ // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
381+ wxCpConfigStorage .expireAccessToken ();
391382 return execute (executor , uri , data );
392383 }
393384 /**
0 commit comments