diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java b/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java index 75eb756f..37b055a6 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java @@ -21,6 +21,7 @@ public class BatchScheduler { private final JobLauncher jobLauncher; private final Job dailyWeatherJob; private final Job expectedWeatherJob; + private final Job weatherAdvisoryTasklet; @Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간마다 public void runDailyWeatherJob() { @@ -49,4 +50,18 @@ public void runExpectedWeatherJob() { log.error("Error executing expectedWeatherJob: ", e); } } + + @Scheduled(cron = "0 0 * * * *") // 1시간마다 + public void runWeatherAdvisoryJob() { + log.info("[ Scheduler ] WeatherAdvisory Api"); + try { + JobParameters jobParameters = new JobParametersBuilder() + .addLong("executedTime", System.currentTimeMillis()) + .toJobParameters(); + + jobLauncher.run(weatherAdvisoryTasklet, jobParameters); + } catch (JobExecutionException e) { + log.error("Error executing expectedWeatherJob: ", e); + } + } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java b/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java new file mode 100644 index 00000000..5774cb9a --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java @@ -0,0 +1,39 @@ +package com.waither.weatherservice.batch; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import com.waither.weatherservice.entity.Region; +import com.waither.weatherservice.exception.WeatherExceptionHandler; +import com.waither.weatherservice.response.WeatherErrorCode; +import com.waither.weatherservice.service.WeatherService; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class WeatherAdvisoryTasklet implements Tasklet { + + private final WeatherService weatherService; + + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { + List regionList = weatherService.getRegionList(); + regionList.stream() + .forEach(region -> { + try { + weatherService.createWeatherAdvisory(region.getStartLat(), region.getStartLon()); + } catch (URISyntaxException e) { + throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); + } catch (IOException e) { + throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR); + } + }); + return RepeatStatus.FINISHED; + } +}