diff --git a/src/main/java/swm_nm/morandi/domain/testDuring/scheduler/TestScheduler.java b/src/main/java/swm_nm/morandi/domain/testDuring/scheduler/TestScheduler.java index 163c865a..88e032a4 100644 --- a/src/main/java/swm_nm/morandi/domain/testDuring/scheduler/TestScheduler.java +++ b/src/main/java/swm_nm/morandi/domain/testDuring/scheduler/TestScheduler.java @@ -14,13 +14,11 @@ import java.time.Duration; import java.time.LocalDateTime; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Future; +import java.util.concurrent.*; @Component @RequiredArgsConstructor @Slf4j -@EnableAsync public class TestScheduler { private final TestRepository testRepository; @@ -33,37 +31,41 @@ public void addTest(TestCheckDto testCheckDto) { testMapManager.addTest(testCheckDto); } @Scheduled(fixedRate = 60000) - @Async - public Future callApiPeriodically() { + public void callApiPeriodically() { List deleteList = new ArrayList<>(); ConcurrentHashMap testMap = testMapManager.getTestMap(); + ExecutorService executorService = Executors.newFixedThreadPool(5); + List> futures = new ArrayList<>(); testMap.forEach((testId, testCheckDto) -> { - log.info("Scheduler call testID : " + testId); - Optional result = testRepository.findById(testId); - if (result.isEmpty()) { - log.info("Scheduler Not Found testID : " + testCheckDto.getTestId()); - deleteList.add(testCheckDto); - } - else { - Tests test = result.get(); - if (test.getTestStatus() == TestStatus.COMPLETED) { - log.info("Scheduler Completed testID : " + testId); - deleteList.add(testCheckDto); - return; - } - Duration duration = Duration.between(test.getTestDate(), LocalDateTime.now()); - Long minutes = duration.toMinutes(); - if (minutes > test.getTestTime()) { - log.info("Scheduler exceed minutes testID : " + testId); - deleteList.add(testCheckDto); - return; - } - checkAttemptProblemService.checkAttemptedProblemResult(test, testCheckDto.getBojId()); - } + CompletableFuture future = CompletableFuture.runAsync(() -> { + log.info("Scheduler call testID : " + testId); + Optional result = testRepository.findById(testId); + if (result.isEmpty()) { + log.info("Scheduler Not Found testID : " + testCheckDto.getTestId()); + deleteList.add(testCheckDto); + } else { + Tests test = result.get(); + if (test.getTestStatus() == TestStatus.COMPLETED) { + log.info("Scheduler Completed testID : " + testId); + deleteList.add(testCheckDto); + return; + } + Duration duration = Duration.between(test.getTestDate(), LocalDateTime.now()); + Long minutes = duration.toMinutes(); + if (minutes > test.getTestTime()) { + log.info("Scheduler exceed minutes testID : " + testId); + deleteList.add(testCheckDto); + return; + } + checkAttemptProblemService.checkAttemptedProblemResult(test, testCheckDto.getBojId()); + } + }, executorService); + futures.add(future); }); + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); + deleteList.forEach(testCheckDto -> testMap.remove(testCheckDto.getTestId(), testCheckDto)); - return null; } } diff --git a/src/main/java/swm_nm/morandi/domain/testStart/service/GetProblemsService.java b/src/main/java/swm_nm/morandi/domain/testStart/service/GetProblemsService.java index 2abf078a..f8e4795f 100644 --- a/src/main/java/swm_nm/morandi/domain/testStart/service/GetProblemsService.java +++ b/src/main/java/swm_nm/morandi/domain/testStart/service/GetProblemsService.java @@ -21,6 +21,8 @@ import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -34,6 +36,7 @@ public List getProblemsByApi(TestType testType, String bojId) { List difficultyRanges = testType.getDifficultyRanges(); List bojProblems = BojProblem.initBojProblems(difficultyRanges); String apiUrl = "https://solved.ac/api/v3/search/problem"; + ExecutorService executorService = Executors.newFixedThreadPool(5); List> futures = bojProblems.stream() .map(bojProblem -> CompletableFuture.runAsync(() -> { @@ -60,7 +63,7 @@ public List getProblemsByApi(TestType testType, String bojId) { throw new MorandiException(TestErrorCode.JSON_PARSE_ERROR); } } - })).collect(Collectors.toList()); + }, executorService)).collect(Collectors.toList()); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); diff --git a/src/test/java/swm_nm/morandi/domain/testDuring/scheduler/TestSchedulerTest.java b/src/test/java/swm_nm/morandi/domain/testDuring/scheduler/TestSchedulerTest.java index aec0912d..e92b35d1 100644 --- a/src/test/java/swm_nm/morandi/domain/testDuring/scheduler/TestSchedulerTest.java +++ b/src/test/java/swm_nm/morandi/domain/testDuring/scheduler/TestSchedulerTest.java @@ -60,12 +60,7 @@ void testScheduler() { // when long startTime = System.currentTimeMillis(); - Future future = testScheduler.callApiPeriodically(); - try { - future.get(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } + testScheduler.callApiPeriodically(); long endTime = System.currentTimeMillis(); // then