Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor : 경로 계산 컴포넌트 비동기적으로 호출 #24

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/org/tenten/tentenstomp/config/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<TripPathInfoMsg> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TripPlace> tripPlaceList) {
List<PathCalculateRequest> pathCalculateRequests = toPathCalculateRequest(tripPlaceList);
int priceSum = 0;
Integer priceSum = 0;
List<TripPathInfoMsg> 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);
Expand Down