@@ -37,7 +37,7 @@ class AppTokenManager {
3737
3838 private final Lock appAccessTokenFetchLock = new ReentrantLock ();
3939
40- private final Map <String , FutureTask <AppTenantAccessToken >> tenantTokenFetchFutureMap = new ConcurrentHashMap <>();
40+ private final Map <String , FutureTask <? >> tenantTokenFetchFutureMap = new ConcurrentHashMap <>();
4141
4242 public AppTokenManager (OpenApiClient openApiClient , App app , AppTicketStorage appTicketStorage ) {
4343 this .app = app ;
@@ -77,6 +77,8 @@ public void refreshAppAccessToken() {
7777 } finally {
7878 appAccessTokenFetchLock .unlock ();
7979 }
80+
81+ LoggerUtil .GLOBAL_LOGGER .debug ("app access token refreshed for appId: {}" , app .getAppId ());
8082 } else {
8183 // if someone is already fetching token, we do nothing but wait for fetching done
8284 try {
@@ -88,7 +90,7 @@ public void refreshAppAccessToken() {
8890 }
8991
9092 public void refreshTenantToken (String tenantKey ) throws LarkClientException {
91- FutureTask <AppTenantAccessToken > doingTask = tenantTokenFetchFutureMap .get (tenantKey );
93+ FutureTask <? > doingTask = tenantTokenFetchFutureMap .get (tenantKey );
9294
9395 if (doingTask == null ) {
9496
@@ -100,6 +102,7 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
100102 }
101103
102104 if (appAccessToken == null || appAccessToken .isExpired ()) {
105+ appAccessToken = null ;
103106 return null ;
104107 }
105108
@@ -124,17 +127,29 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
124127 }
125128 };
126129
127- FutureTask <AppTenantAccessToken > newTask = new FutureTask <>(() -> {
130+ FutureTask <?> newTask = new FutureTask <Void >(() -> {
131+ AppTenantAccessToken appTenantAccessToken ;
128132 // retry fetch tenant access token after refresh app access token
129133 try {
130- return c .call ();
134+ appTenantAccessToken = c .call ();
131135 } catch (LarkClientException e ) {
132136 if (e .getCode () == LarkClientException .APP_ACCESS_TOKEN_INVALID ) {
133137 refreshAppAccessToken ();
134- return c .call ();
138+ appTenantAccessToken = c .call ();
139+ } else {
140+ throw e ;
135141 }
136- throw e ;
137142 }
143+
144+ if (appTenantAccessToken != null ) {
145+ tenantTokenMap .put (tenantKey , appTenantAccessToken );
146+ LoggerUtil .GLOBAL_LOGGER .debug ("tenant access token refreshed for appId: {}, tenantKey: {}" , app .getAppId (), tenantKey );
147+ } else {
148+ LoggerUtil .GLOBAL_LOGGER .error ("tenant access token refreshed failed for appId: {}, tenantKey: {}" , app .getAppId (), tenantKey );
149+ }
150+
151+ return null ;
152+
138153 });
139154
140155 doingTask = tenantTokenFetchFutureMap .putIfAbsent (tenantKey , newTask );
@@ -144,11 +159,8 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
144159 newTask .run ();
145160
146161 try {
147- AppTenantAccessToken appTenantAccessToken = newTask .get ();
148- if (appTenantAccessToken != null ) {
149- LoggerUtil .GLOBAL_LOGGER .debug ("token refreshed for appId: {}, tenantKey: {}" , app .getAppId (), tenantKey );
150- tenantTokenMap .put (tenantKey , appTenantAccessToken );
151- }
162+ newTask .get ();
163+
152164 } catch (InterruptedException e ) {
153165 // wont happen here
154166 } catch (ExecutionException e ) {
@@ -168,6 +180,8 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
168180 try {
169181 // others doing the task, wait for it done
170182 doingTask .get ();
183+ LoggerUtil .GLOBAL_LOGGER .debug ("wait for refreshed for appId: {}, tenantKey: {}, done." , app .getAppId (), tenantKey );
184+
171185 } catch (InterruptedException e ) {
172186 LoggerUtil .GLOBAL_LOGGER .error ("refreshTenantToken InterruptedException " , e );
173187 } catch (ExecutionException e ) {
0 commit comments