From 16f99fab2a489e2b0b144b7e8376ef015c0d4a41 Mon Sep 17 00:00:00 2001 From: Hong-Mu Date: Wed, 1 May 2024 02:44:01 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=EC=98=88=EB=B3=B4=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=20=ED=8F=AC=EB=A7=B7=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weather/application/service/WeatherService.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java b/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java index f969123..146667e 100644 --- a/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java +++ b/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java @@ -79,7 +79,7 @@ public WeatherData getWeatherData(int nx, int ny) { String baseTime = getBaseTime(); String fcstDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); - String fcstTime = LocalTime.now().format(DateTimeFormatter.ofPattern("HH00")); + String fcstTime = LocalTime.now().plusHours(1).format(DateTimeFormatter.ofPattern("HH00")); WeatherAPIRequest request = WeatherAPIRequest.builder() .serviceKey(publicDataProperty.getAccessKey()) @@ -113,7 +113,7 @@ public WeatherData getWeatherData(int nx, int ny) { .nx(nx) .ny(ny) .build(); - + log.info("newRequest: {}", newRequest); WeatherAPIResponse newResult = dataGoAPI.getWeather( newRequest.getServiceKey(), newRequest.getBaseDate(), @@ -121,6 +121,7 @@ public WeatherData getWeatherData(int nx, int ny) { newRequest.getNx(), newRequest.getNy() ); + log.info("newResult: {}", newResult); Float tmx = Float.parseFloat(newResult.getValueByCategoryAndDate("TMX", fcstDate)); Float tmn = Float.parseFloat(newResult.getValueByCategoryAndDate("TMN", fcstDate)); @@ -175,10 +176,12 @@ private String getTmpMaxMinBaseDate() { private String getTmpMaxMinBaseTime() { LocalTime now = LocalTime.now(); + int result = 0; if(now.isAfter(LocalTime.of(2, 30))) { - return baseHours.get(0) + "00"; + result = baseHours.get(0); } else { - return baseHours.get(baseHours.size() - 1) + "00"; + result = baseHours.get(baseHours.size() - 1); } + return String.format("%02d00", result); } } From 959b36ca5c990db73e6e94918420d6d6b841954c Mon Sep 17 00:00:00 2001 From: Hong-Mu Date: Wed, 1 May 2024 04:07:10 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/WeatherService.java | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java b/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java index 146667e..003eb9c 100644 --- a/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java +++ b/src/main/java/com/seoultech/sanEseo/weather/application/service/WeatherService.java @@ -5,6 +5,8 @@ import com.seoultech.sanEseo.district.domain.District; import com.seoultech.sanEseo.global.config.DataGoAPI; import com.seoultech.sanEseo.global.config.DataSeoulAPI; +import com.seoultech.sanEseo.global.exception.BusinessException; +import com.seoultech.sanEseo.global.exception.ErrorType; import com.seoultech.sanEseo.global.property.PublicDataProperty; import com.seoultech.sanEseo.weather.adapter.in.web.WeatherResponse; import com.seoultech.sanEseo.weather.domain.PollutionData; @@ -22,6 +24,10 @@ import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; @Slf4j @RequiredArgsConstructor @@ -33,11 +39,22 @@ public class WeatherService { private final DataGoAPI dataGoAPI; private final DataSeoulAPI dataSeoulAPI; + private ExecutorService executorService = Executors.newCachedThreadPool(); + public WeatherResponse getWeatherResponse(Long districtId) { District district = districtService.findById(districtId); - PollutionData pollutionData = getPollution(district.getPollutionAPI().getCode()); - WeatherData weatherData = getWeatherData(district.getWeatherAPI().getX(), district.getWeatherAPI().getY()); + Future task1 = executorService.submit(() -> getPollution(district.getPollutionAPI().getCode())); + Future task2 = executorService.submit(() -> getWeatherData(district.getWeatherAPI().getX(), district.getWeatherAPI().getY())); + + PollutionData pollutionData = null; + WeatherData weatherData = null; + try { + pollutionData = task1.get(); + weatherData = task2.get(); + } catch (InterruptedException | ExecutionException e) { + throw new BusinessException(ErrorType.INTERNAL_ERROR, "비동기 처리 오류"); + } return WeatherResponse.builder() .districtId(districtId) @@ -54,7 +71,6 @@ public WeatherResponse getWeatherResponse(Long districtId) { } public PollutionData getPollution(int code) { - try { PollutionAPIResponse result = dataSeoulAPI.getPollution( publicDataProperty.getSeoulAccessKey(), @@ -74,6 +90,7 @@ public PollutionData getPollution(int code) { } public WeatherData getWeatherData(int nx, int ny) { + try { String baseDate = getBaseDate(); String baseTime = getBaseTime(); @@ -89,43 +106,43 @@ public WeatherData getWeatherData(int nx, int ny) { .ny(ny) .build(); - log.info("request: {}", request); - WeatherAPIResponse result = dataGoAPI.getWeather( + Future task1 = executorService.submit(() -> dataGoAPI.getWeather( request.getServiceKey(), request.getBaseDate(), request.getBaseTime(), request.getNx(), request.getNy() - ); - log.info("result: {}", result); - Float tmp = Float.parseFloat(result.getValueByCategoryAndDateTime("TMP", fcstDate, fcstTime)); - String pcp = result.getValueByCategoryAndDateTime("PCP", fcstDate, fcstTime); - int reh = Integer.parseInt(result.getValueByCategoryAndDateTime("REH", fcstDate, fcstTime)); - log.info("TMP: {}, PCP: {}, REH: {}", tmp, pcp, reh); + )); String tmpMaxMinBaseDate = getTmpMaxMinBaseDate(); String tmpMaxMinBaseTime = getTmpMaxMinBaseTime(); - WeatherAPIRequest newRequest = WeatherAPIRequest.builder() + WeatherAPIRequest newRequest = WeatherAPIRequest.builder() .serviceKey(publicDataProperty.getAccessKey()) .baseDate(tmpMaxMinBaseDate) .baseTime(tmpMaxMinBaseTime) .nx(nx) .ny(ny) .build(); - log.info("newRequest: {}", newRequest); - WeatherAPIResponse newResult = dataGoAPI.getWeather( - newRequest.getServiceKey(), - newRequest.getBaseDate(), - newRequest.getBaseTime(), - newRequest.getNx(), - newRequest.getNy() - ); - log.info("newResult: {}", newResult); + Future task2 = executorService.submit(() -> dataGoAPI.getWeather( + newRequest.getServiceKey(), + newRequest.getBaseDate(), + newRequest.getBaseTime(), + newRequest.getNx(), + newRequest.getNy())); + + + WeatherAPIResponse result = task1.get(); + Float tmp = Float.parseFloat(result.getValueByCategoryAndDateTime("TMP", fcstDate, fcstTime)); + String pcp = result.getValueByCategoryAndDateTime("PCP", fcstDate, fcstTime); + int reh = Integer.parseInt(result.getValueByCategoryAndDateTime("REH", fcstDate, fcstTime)); + + + WeatherAPIResponse newResult = task2.get(); Float tmx = Float.parseFloat(newResult.getValueByCategoryAndDate("TMX", fcstDate)); Float tmn = Float.parseFloat(newResult.getValueByCategoryAndDate("TMN", fcstDate)); - log.info("TMX: {}, TMN: {}", tmx, tmn); + return new WeatherData(tmp, tmx, tmn, pcp, reh); } catch (Exception e) { From 8fd51fda67a34bd63a7750a613efbd35a29943c6 Mon Sep 17 00:00:00 2001 From: Hong-Mu Date: Wed, 1 May 2024 04:12:44 +0900 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=EC=A4=91=EB=B3=B5=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../seoultech/sanEseo/global/config/DataSeoulAPI.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/com/seoultech/sanEseo/global/config/DataSeoulAPI.java b/src/main/java/com/seoultech/sanEseo/global/config/DataSeoulAPI.java index 7a0b6b7..2cebc80 100644 --- a/src/main/java/com/seoultech/sanEseo/global/config/DataSeoulAPI.java +++ b/src/main/java/com/seoultech/sanEseo/global/config/DataSeoulAPI.java @@ -7,15 +7,6 @@ public interface DataSeoulAPI { - @GetExchange("/1360000/VilageFcstInfoService_2.0/getVilageFcst?serviceKey={serviceKey}&pageNo=1&numOfRows=1000&dataType=json&base_date={base_date}&base_time={base_time}&nx={nx}&ny={ny}") - WeatherAPIResponse getWeather( - @PathVariable("serviceKey") String serviceKey, - @PathVariable("base_date") String baseDate, - @PathVariable("base_time") String baseTime, - @PathVariable("nx") int nx, - @PathVariable("ny") int ny - ); - @GetExchange("/{serviceKey}/json/ListAirQualityByDistrictService/1/1/{code}/") PollutionAPIResponse getPollution( @PathVariable("serviceKey") String serviceKey,