Skip to content

Commit

Permalink
♻️ refactor: 기상특보 스케줄링 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn committed Jul 5, 2024
1 parent 080ce50 commit dcc54fc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Region> 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;
}
}

0 comments on commit dcc54fc

Please sign in to comment.