4444public class WxMpServiceImpl implements WxMpService {
4545
4646 /**
47- * 全局的是否正在刷新Access Token的flag
48- * true: 正在刷新
49- * false: 没有刷新
47+ * 全局的是否正在刷新access token的锁
5048 */
51- protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean (false );
52-
49+ protected static final Object GLOBAL_ACCESS_TOKEN_REFRESH_LOCK = new Object ();
50+
51+ /**
52+ * 全局的是否正在刷新jsapi_ticket的锁
53+ */
54+ protected static final Object GLOBAL_JSAPI_TICKET_REFRESH_LOCK = new Object ();
55+
5356 protected WxMpConfigStorage wxMpConfigStorage ;
5457
5558 protected final ThreadLocal <Integer > retryTimes = new ThreadLocal <Integer >();
@@ -66,52 +69,45 @@ public boolean checkSignature(String timestamp, String nonce, String signature)
6669 }
6770 }
6871
69- public void accessTokenRefresh () throws WxErrorException {
70- if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .getAndSet (true )) {
71- try {
72- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
73- + "&appid=" + wxMpConfigStorage .getAppId ()
74- + "&secret=" + wxMpConfigStorage .getSecret ()
75- ;
76- try {
77- HttpGet httpGet = new HttpGet (url );
78- if (httpProxy != null ) {
79- RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
80- httpGet .setConfig (config );
81- }
82- CloseableHttpClient httpclient = getHttpclient ();
83- CloseableHttpResponse response = httpclient .execute (httpGet );
84- String resultContent = new BasicResponseHandler ().handleResponse (response );
85- WxError error = WxError .fromJson (resultContent );
86- if (error .getErrorCode () != 0 ) {
87- throw new WxErrorException (error );
72+ public String getAccessToken () throws WxErrorException {
73+ if (wxMpConfigStorage .isAccessTokenExpired ()) {
74+ synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK ) {
75+ if (wxMpConfigStorage .isAccessTokenExpired ()) {
76+ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
77+ + "&appid=" + wxMpConfigStorage .getAppId ()
78+ + "&secret=" + wxMpConfigStorage .getSecret ()
79+ ;
80+ try {
81+ HttpGet httpGet = new HttpGet (url );
82+ if (httpProxy != null ) {
83+ RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
84+ httpGet .setConfig (config );
85+ }
86+ CloseableHttpClient httpclient = getHttpclient ();
87+ CloseableHttpResponse response = httpclient .execute (httpGet );
88+ String resultContent = new BasicResponseHandler ().handleResponse (response );
89+ WxError error = WxError .fromJson (resultContent );
90+ if (error .getErrorCode () != 0 ) {
91+ throw new WxErrorException (error );
92+ }
93+ WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
94+ wxMpConfigStorage .updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
95+ } catch (ClientProtocolException e ) {
96+ throw new RuntimeException (e );
97+ } catch (IOException e ) {
98+ throw new RuntimeException (e );
8899 }
89- WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
90- wxMpConfigStorage .updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
91- } catch (ClientProtocolException e ) {
92- throw new RuntimeException (e );
93- } catch (IOException e ) {
94- throw new RuntimeException (e );
95100 }
96- } finally {
97- GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .set (false );
98101 }
99- } else {
100- // 每隔100ms检查一下是否刷新完毕了
101- while (GLOBAL_ACCESS_TOKEN_REFRESH_FLAG .get ()) {
102- try {
103- Thread .sleep (100 );
104- } catch (InterruptedException e ) {
105- }
106- }
107- // 刷新完毕了,就没他什么事儿了
108102 }
103+ return wxMpConfigStorage .getAccessToken ();
109104 }
110105
106+
111107 public String getJsapiTicket () throws WxErrorException {
112- if (wxMpConfigStorage .isJsapiTokenExpired ()) {
113- synchronized (wxMpConfigStorage ) {
114- if (wxMpConfigStorage .isJsapiTokenExpired ()) {
108+ if (wxMpConfigStorage .isJsapiTicketExpired ()) {
109+ synchronized (GLOBAL_JSAPI_TICKET_REFRESH_LOCK ) {
110+ if (wxMpConfigStorage .isJsapiTicketExpired ()) {
115111 String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi" ;
116112 String responseContent = execute (new SimpleGetRequestExecutor (), url , null );
117113 JsonElement tmpJsonElement = Streams .parse (new JsonReader (new StringReader (responseContent )));
@@ -440,10 +436,7 @@ public String post(String url, String postData) throws WxErrorException {
440436 * @throws WxErrorException
441437 */
442438 public <T , E > T execute (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
443- if (StringUtils .isBlank (wxMpConfigStorage .getAccessToken ())) {
444- accessTokenRefresh ();
445- }
446- String accessToken = wxMpConfigStorage .getAccessToken ();
439+ String accessToken = getAccessToken ();
447440
448441 String uriWithAccessToken = uri ;
449442 uriWithAccessToken += uri .indexOf ('?' ) == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken ;
@@ -458,7 +451,8 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
458451 * 42001 access_token超时
459452 */
460453 if (error .getErrorCode () == 42001 || error .getErrorCode () == 40001 ) {
461- accessTokenRefresh ();
454+ // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
455+ wxMpConfigStorage .expireAccessToken ();
462456 return execute (executor , uri , data );
463457 }
464458 /**
0 commit comments