-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccessTokenAspect.java
64 lines (55 loc) · 2.18 KB
/
AccessTokenAspect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package codesquad.fineants.spring.api.kis.aop;
import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import codesquad.fineants.spring.api.kis.client.KisClient;
import codesquad.fineants.spring.api.kis.manager.KisAccessTokenManager;
import codesquad.fineants.spring.api.kis.properties.OauthKisProperties;
import codesquad.fineants.spring.api.kis.service.KisAccessTokenRedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor
@Aspect
@Component
public class AccessTokenAspect {
private final KisAccessTokenManager manager;
private final KisClient client;
private final KisAccessTokenRedisService redisService;
private final OauthKisProperties oauthKisProperties;
@Pointcut("execution(* codesquad.fineants.spring.api.kis.service.KisService.scheduleRefreshingAllStockCurrentPrice())")
public void scheduleRefreshingAllStockCurrentPrice() {
}
@Pointcut("execution(* codesquad.fineants.spring.api.kis.service.KisService.scheduleRefreshingAllLastDayClosingPrice())")
public void scheduleRefreshingAllLastDayClosingPrice() {
}
@Before(value = "scheduleRefreshingAllStockCurrentPrice() || scheduleRefreshingAllLastDayClosingPrice()")
public void checkAccessTokenExpiration() {
LocalDateTime now = LocalDateTime.now();
if (manager.isAccessTokenExpired(now)) {
redisService.getAccessTokenMap()
.ifPresentOrElse(manager::refreshAccessToken, () -> handleNewAccessToken(now));
}
}
private void handleNewAccessToken(LocalDateTime now) {
CountDownLatch latch = new CountDownLatch(1);
client.fetchAccessToken()
.subscribe(accessToken -> {
redisService.setAccessTokenMap(accessToken, now);
manager.refreshAccessToken(accessToken);
log.info("새로운 액세스 토큰 갱신 완료");
},
error -> {
log.error("새로운 액세스 토큰 발급 에러", error);
latch.countDown();
}, latch::countDown);
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}