From 0371824ff02437413ffc155054ce2602b10de676 Mon Sep 17 00:00:00 2001 From: Kim-Dong-Jun99 Date: Thu, 11 Jan 2024 18:42:48 +0900 Subject: [PATCH] =?UTF-8?q?Refactor=20:=20=EA=B2=BD=EB=A1=9C=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EC=BB=B4=ED=8F=AC=EB=84=8C=ED=8A=B8=20=EB=B9=84?= =?UTF-8?q?=EB=8F=99=EA=B8=B0=EC=A0=81=EC=9C=BC=EB=A1=9C=20=ED=98=B8?= =?UTF-8?q?=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tentenstomp/config/AsyncConfig.java | 12 ++++++- .../global/component/AsyncPathComponent.java | 33 +++++++++++++++++ .../global/component/PathComponent.java | 36 ++++++++----------- 3 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 src/main/java/org/tenten/tentenstomp/global/component/AsyncPathComponent.java diff --git a/src/main/java/org/tenten/tentenstomp/config/AsyncConfig.java b/src/main/java/org/tenten/tentenstomp/config/AsyncConfig.java index 52275b3..5f810a3 100644 --- a/src/main/java/org/tenten/tentenstomp/config/AsyncConfig.java +++ b/src/main/java/org/tenten/tentenstomp/config/AsyncConfig.java @@ -1,8 +1,18 @@ package org.tenten.tentenstomp.config; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; @Configuration public class AsyncConfig { - + @Bean + public Executor myPool() { + ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); + threadPoolTaskExecutor.setCorePoolSize(5); // 기본 스레드 수 + threadPoolTaskExecutor.setMaxPoolSize(20); // 최대 스레드 수 + return threadPoolTaskExecutor; + } } diff --git a/src/main/java/org/tenten/tentenstomp/global/component/AsyncPathComponent.java b/src/main/java/org/tenten/tentenstomp/global/component/AsyncPathComponent.java new file mode 100644 index 0000000..7e9ece7 --- /dev/null +++ b/src/main/java/org/tenten/tentenstomp/global/component/AsyncPathComponent.java @@ -0,0 +1,33 @@ +package org.tenten.tentenstomp.global.component; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg; +import org.tenten.tentenstomp.global.component.dto.request.TripPlace; + +import java.util.List; + +import static org.tenten.tentenstomp.global.common.enums.Transportation.CAR; + +@Component +@RequiredArgsConstructor +@Slf4j +public class AsyncPathComponent { + private final OdsayComponent odsayComponent; + private final NaverMapComponent naverMapComponent; + @Async + public void calculatePath(TripPlace fromPlace, TripPlace toPlace, List pathInfoMsgs) { + + long startTime = System.currentTimeMillis(); + TripPathInfoMsg.PathInfo pathInfo; + if (toPlace.transportation().equals(CAR)) { + pathInfo = naverMapComponent.calculatePathInfo(fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude()); + } else { + pathInfo = odsayComponent.calculatePathInfo(fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude()); + } + log.info("from " + fromPlace.seqNum() + " to " + toPlace.seqNum() + " executionTime : " + ((System.currentTimeMillis() - startTime) / 1000.0)); + pathInfoMsgs.add(new TripPathInfoMsg(fromPlace.seqNum(), toPlace.seqNum(), fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude(), toPlace.transportation(), pathInfo)); + } +} diff --git a/src/main/java/org/tenten/tentenstomp/global/component/PathComponent.java b/src/main/java/org/tenten/tentenstomp/global/component/PathComponent.java index 89f0d67..83dc02a 100644 --- a/src/main/java/org/tenten/tentenstomp/global/component/PathComponent.java +++ b/src/main/java/org/tenten/tentenstomp/global/component/PathComponent.java @@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg; -import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg.PathInfo; import org.tenten.tentenstomp.global.common.annotation.GetExecutionTime; import org.tenten.tentenstomp.global.component.dto.request.PathCalculateRequest; import org.tenten.tentenstomp.global.component.dto.request.TripPlace; @@ -13,36 +12,29 @@ import java.util.ArrayList; import java.util.List; -import static org.tenten.tentenstomp.global.common.enums.Transportation.CAR; - @Component @RequiredArgsConstructor @Slf4j public class PathComponent { - private final OdsayComponent odsayComponent; - private final NaverMapComponent naverMapComponent; -// @Async - public TripPathInfoMsg calculatePath(TripPlace fromPlace, TripPlace toPlace) { - long startTime = System.currentTimeMillis(); - PathInfo pathInfo; - if (toPlace.transportation().equals(CAR)) { - pathInfo = naverMapComponent.calculatePathInfo(fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude()); - } else { - pathInfo = odsayComponent.calculatePathInfo(fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude()); - } - log.info("from "+fromPlace.seqNum()+" to "+toPlace.seqNum()+" executionTime : "+((System.currentTimeMillis() - startTime) / 1000.0)); - return new TripPathInfoMsg(fromPlace.seqNum(), toPlace.seqNum(), fromPlace.longitude(), fromPlace.latitude(), toPlace.longitude(), toPlace.latitude(), toPlace.transportation(), pathInfo); - } + private final AsyncPathComponent asyncPathComponent; + + @GetExecutionTime public TripPathCalculationResult getTripPath(List tripPlaceList) { List pathCalculateRequests = toPathCalculateRequest(tripPlaceList); - int priceSum = 0; + Integer priceSum = 0; List pathInfoMsgs = new ArrayList<>(); for (PathCalculateRequest calculateRequest : pathCalculateRequests) { - TripPathInfoMsg tripPathInfoMsg = calculatePath(calculateRequest.from(), calculateRequest.to()); - pathInfoMsgs.add(tripPathInfoMsg); - if (tripPathInfoMsg.pathInfo() != null) { - priceSum += tripPathInfoMsg.pathInfo().price(); + asyncPathComponent.calculatePath(calculateRequest.from(), calculateRequest.to(), pathInfoMsgs); + } + while (true) { + if (pathInfoMsgs.size() == pathCalculateRequests.size()) { + break; + } + } + for (TripPathInfoMsg tpm : pathInfoMsgs) { + if (tpm.pathInfo() != null) { + priceSum += tpm.pathInfo().price(); } } return new TripPathCalculationResult(priceSum, pathInfoMsgs);