Skip to content

Commit

Permalink
Merge pull request #24 from FinalDoubleTen/BE-68-Async-Trip-Path
Browse files Browse the repository at this point in the history
Refactor : 경로 계산 컴포넌트 비동기적으로 호출
  • Loading branch information
Kim-Dong-Jun99 authored Jan 11, 2024
2 parents bf77624 + 0371824 commit ac0e3db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
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

0 comments on commit ac0e3db

Please sign in to comment.