|
9 | 9 | import org.springframework.http.HttpHeaders; |
10 | 10 | import org.springframework.http.MediaType; |
11 | 11 | import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 13 | +import org.springframework.retry.annotation.Backoff; |
12 | 14 | import org.springframework.retry.annotation.Retryable; |
13 | 15 | import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.web.client.ResourceAccessException; |
14 | 17 | import org.springframework.web.client.RestTemplate; |
15 | 18 | import reactor.core.Disposable; |
16 | 19 | import reactor.core.publisher.Flux; |
|
33 | 36 | @Slf4j |
34 | 37 | @Component |
35 | 38 | public class AListClient implements InitializingBean, DisposableBean { |
36 | | - private final RestTemplate restTemplate = new RestTemplate(); |
| 39 | + private RestTemplate restTemplate; |
37 | 40 | private HttpHeaders httpHeaders = new HttpHeaders(); |
38 | 41 | private AListToken token; |
39 | 42 | private Disposable refreshTokenTaskDisposable; |
@@ -102,7 +105,10 @@ private Mono<Void> createAttachmentRecursively(List<String> paths, Long parentId |
102 | 105 | .then(); |
103 | 106 | } |
104 | 107 |
|
105 | | - @Retryable |
| 108 | + @Retryable( |
| 109 | + maxAttempts = 8, |
| 110 | + backoff = @Backoff(delay = 2000) |
| 111 | + ) |
106 | 112 | public AListAttachment[] fetchAttachments(List<String> paths) { |
107 | 113 | Map<String, String> bodyMap = new HashMap<>(); |
108 | 114 | String path = getPathByPathArr(paths); |
@@ -180,7 +186,10 @@ public Mono<AListToken> refreshToken() { |
180 | 186 | return refreshToken(false); |
181 | 187 | } |
182 | 188 |
|
183 | | - @Retryable |
| 189 | + @Retryable( |
| 190 | + maxAttempts = 4, |
| 191 | + backoff = @Backoff(delay = 2000) |
| 192 | + ) |
184 | 193 | public Mono<AListToken> refreshToken(boolean refresh) { |
185 | 194 | if (StringUtils.isBlank(token.getUrl()) |
186 | 195 | || StringUtils.isBlank(token.getUsername()) |
@@ -243,7 +252,10 @@ private String getPathByPathArr(List<String> paths) { |
243 | 252 | return pStr; |
244 | 253 | } |
245 | 254 |
|
246 | | - @Retryable |
| 255 | + @Retryable( |
| 256 | + maxAttempts = 5, |
| 257 | + backoff = @Backoff(delay = 2000) |
| 258 | + ) |
247 | 259 | private AListAttachment fetchAttachmentDetail(String path, AListAttachment attachment) { |
248 | 260 | if (StringUtils.isEmpty(path)) { |
249 | 261 | return attachment; |
@@ -356,6 +368,12 @@ public void afterPropertiesSet() throws Exception { |
356 | 368 | // token is null, get config from db. |
357 | 369 | updateOperateByToken().subscribe(); |
358 | 370 | } |
| 371 | + |
| 372 | + // init rest temp |
| 373 | + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); |
| 374 | + factory.setConnectTimeout(10000); // 设置连接超时时间,10s |
| 375 | + factory.setReadTimeout(10000); |
| 376 | + restTemplate = new RestTemplate(factory); |
359 | 377 | } |
360 | 378 |
|
361 | 379 |
|
|
0 commit comments