|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.core.type.TypeReference; |
4 | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.beans.factory.DisposableBean; |
| 6 | +import org.springframework.beans.factory.InitializingBean; |
5 | 7 | import org.springframework.boot.context.event.ApplicationReadyEvent; |
6 | 8 | import org.springframework.context.event.EventListener; |
7 | 9 | import org.springframework.http.HttpEntity; |
|
31 | 33 |
|
32 | 34 | @Slf4j |
33 | 35 | @Component |
34 | | -public class AListClient { |
| 36 | +public class AListClient implements InitializingBean, DisposableBean { |
35 | 37 | private final RestTemplate restTemplate = new RestTemplate(); |
36 | 38 | private HttpHeaders httpHeaders = new HttpHeaders(); |
37 | 39 | private AListToken token; |
@@ -92,7 +94,8 @@ private Mono<Void> createAttachmentRecursively(List<String> paths, Long parentId |
92 | 94 | .flatMap(aListAttachment -> { |
93 | 95 | if (aListAttachment.getIs_dir()) { |
94 | 96 | List<String> paths1 = aListAttachment.getPaths(); |
95 | | - return createAttachmentRecursively(paths1, aListAttachment.getId()); |
| 97 | + return createAttachmentRecursively(paths1, aListAttachment.getId()) |
| 98 | + .subscribeOn(Schedulers.boundedElastic()); |
96 | 99 | } else { |
97 | 100 | return Mono.empty(); |
98 | 101 | } |
@@ -205,6 +208,7 @@ public Mono<AListToken> refreshToken(boolean refresh) { |
205 | 208 | Object token1 = apiResult.getData().get("token"); |
206 | 209 | httpHeaders.set("Authorization", String.valueOf(token1)); |
207 | 210 | token.setToken(String.valueOf(token1)); |
| 211 | + log.info("refresh alist token success"); |
208 | 212 | return customClient.update(token2cm(token)) |
209 | 213 | .then(Mono.just(token)); |
210 | 214 | } else { |
@@ -340,14 +344,20 @@ public Mono<AListToken> updateOperateByToken() { |
340 | 344 | return getToken().flatMap(aListToken -> refreshToken(true)); |
341 | 345 | } |
342 | 346 |
|
343 | | - @EventListener(ApplicationReadyEvent.class) |
344 | | - public Mono<Void> initAListToken() { |
345 | | - log.debug("init alist token."); |
346 | | - if (Objects.isNull(token)) { |
| 347 | + @Override |
| 348 | + public void destroy() throws Exception { |
| 349 | + if (Objects.nonNull(refreshTokenTaskDisposable) && !refreshTokenTaskDisposable.isDisposed()) { |
| 350 | + refreshTokenTaskDisposable.dispose(); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public void afterPropertiesSet() throws Exception { |
| 356 | + log.debug("init alist token afterPropertiesSet."); |
| 357 | + if (Objects.isNull(token) || StringUtils.isBlank(token.getToken())) { |
347 | 358 | // token is null, get config from db. |
348 | | - return updateOperateByToken().then(); |
| 359 | + updateOperateByToken().subscribe(); |
349 | 360 | } |
350 | | - return Mono.empty(); |
351 | 361 | } |
352 | 362 |
|
353 | 363 |
|
|
0 commit comments