-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: 서비스 간 통신 api 수정 및 카프카 수정
- Loading branch information
Showing
10 changed files
with
270 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
weather-service/src/main/java/com/waither/weatherservice/batch/BatchConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.waither.weatherservice.batch; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.core.step.tasklet.Tasklet; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import com.waither.weatherservice.service.WeatherService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class BatchConfig { | ||
|
||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager transactionManager; | ||
private final WeatherService weatherService; | ||
|
||
@Bean | ||
public Job dailyWeatherJob() { | ||
return new JobBuilder("dailyWeatherJob", jobRepository) | ||
.start(dailyWeatherStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step dailyWeatherStep() { | ||
return new StepBuilder("dailyWeatherStep", jobRepository) | ||
.tasklet(dailyWeatherTasklet(), transactionManager) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Tasklet dailyWeatherTasklet() { | ||
return new DailyWeatherTasklet(weatherService); | ||
} | ||
|
||
@Bean | ||
public Job expectedWeatherJob() { | ||
return new JobBuilder("expectedWeatherJob", jobRepository) | ||
.start(expectedWeatherStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step expectedWeatherStep() { | ||
return new StepBuilder("expectedWeatherStep", jobRepository) | ||
.tasklet(expectedWeatherTasklet(), transactionManager) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Tasklet expectedWeatherTasklet() { | ||
return new ExpectedWeatherTasklet(weatherService); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.waither.weatherservice.batch; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecutionException; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@EnableScheduling | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BatchScheduler { | ||
|
||
private final JobLauncher jobLauncher; | ||
private final Job dailyWeatherJob; | ||
private final Job expectedWeatherJob; | ||
|
||
@Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간마다 | ||
public void runDailyWeatherJob() { | ||
try { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addLong("executedTime", System.currentTimeMillis()) | ||
.toJobParameters(); | ||
|
||
jobLauncher.run(dailyWeatherJob, jobParameters); | ||
} catch (JobExecutionException e) { | ||
log.error("Error executing dailyWeatherJob: ", e); | ||
} | ||
} | ||
|
||
@Scheduled(cron = "0 0 * * * *") // 1시간마다 | ||
public void runExpectedWeatherJob() { | ||
try { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addLong("executedTime", System.currentTimeMillis()) | ||
.toJobParameters(); | ||
|
||
jobLauncher.run(expectedWeatherJob, jobParameters); | ||
} catch (JobExecutionException e) { | ||
log.error("Error executing expectedWeatherJob: ", e); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
weather-service/src/main/java/com/waither/weatherservice/batch/DailyWeatherTasklet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.waither.weatherservice.batch; | ||
|
||
import java.net.URISyntaxException; | ||
import java.time.LocalDateTime; | ||
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 DailyWeatherTasklet implements Tasklet { | ||
|
||
private final WeatherService weatherService; | ||
|
||
@Override | ||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | ||
LocalDateTime now = LocalDateTime.now(); | ||
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
List<Region> regionList = weatherService.getRegionList(); | ||
regionList.stream() | ||
.forEach(region -> { | ||
try { | ||
weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
dateTime[1]); | ||
} catch (URISyntaxException e) { | ||
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
} | ||
}); | ||
return RepeatStatus.FINISHED; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
weather-service/src/main/java/com/waither/weatherservice/batch/ExpectedWeatherTasklet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.waither.weatherservice.batch; | ||
|
||
import java.net.URISyntaxException; | ||
import java.time.LocalDateTime; | ||
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 ExpectedWeatherTasklet implements Tasklet { | ||
|
||
private final WeatherService weatherService; | ||
|
||
@Override | ||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | ||
LocalDateTime now = LocalDateTime.now(); | ||
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
List<Region> regionList = weatherService.getRegionList(); | ||
regionList.stream() | ||
.forEach(region -> { | ||
try { | ||
weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
dateTime[1]); | ||
} catch (URISyntaxException e) { | ||
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
} | ||
}); | ||
return RepeatStatus.FINISHED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 58 additions & 58 deletions
116
weather-service/src/main/java/com/waither/weatherservice/scheduler/SchedulerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
package com.waither.weatherservice.scheduler; | ||
|
||
import java.net.URISyntaxException; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
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; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties | ||
public class SchedulerConfig { | ||
|
||
private final WeatherService weatherService; | ||
|
||
@Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간 마다 | ||
public void createDailyWeather() { | ||
|
||
LocalDateTime now = LocalDateTime.now(); | ||
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
List<Region> regionList = weatherService.getRegionList(); | ||
regionList.stream() | ||
.forEach(region -> { | ||
try { | ||
weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
dateTime[1]); | ||
} catch (URISyntaxException e) { | ||
new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
} | ||
}); | ||
} | ||
|
||
@Scheduled(cron = "0 0 * * * *") // 1시간 마다 | ||
public void createExpectedWeather() { | ||
|
||
LocalDateTime now = LocalDateTime.now(); | ||
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
List<Region> regionList = weatherService.getRegionList(); | ||
regionList.stream() | ||
.forEach(region -> { | ||
try { | ||
weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
dateTime[1]); | ||
} catch (URISyntaxException e) { | ||
new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
} | ||
}); | ||
} | ||
} | ||
// package com.waither.weatherservice.scheduler; | ||
// | ||
// import java.net.URISyntaxException; | ||
// import java.time.LocalDateTime; | ||
// import java.util.List; | ||
// | ||
// import org.springframework.boot.context.properties.ConfigurationProperties; | ||
// import org.springframework.scheduling.annotation.Scheduled; | ||
// | ||
// 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; | ||
// import lombok.extern.slf4j.Slf4j; | ||
// | ||
// @Slf4j | ||
// @RequiredArgsConstructor | ||
// @ConfigurationProperties | ||
// public class SchedulerConfig { | ||
// | ||
// private final WeatherService weatherService; | ||
// | ||
// @Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간 마다 | ||
// public void createDailyWeather() { | ||
// | ||
// LocalDateTime now = LocalDateTime.now(); | ||
// String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
// List<Region> regionList = weatherService.getRegionList(); | ||
// regionList.stream() | ||
// .forEach(region -> { | ||
// try { | ||
// weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
// dateTime[1]); | ||
// } catch (URISyntaxException e) { | ||
// throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
// } | ||
// }); | ||
// } | ||
// | ||
// @Scheduled(cron = "0 0 * * * *") // 1시간 마다 | ||
// public void createExpectedWeather() { | ||
// | ||
// LocalDateTime now = LocalDateTime.now(); | ||
// String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_"); | ||
// List<Region> regionList = weatherService.getRegionList(); | ||
// regionList.stream() | ||
// .forEach(region -> { | ||
// try { | ||
// weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0], | ||
// dateTime[1]); | ||
// } catch (URISyntaxException e) { | ||
// throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); | ||
// } | ||
// }); | ||
// } | ||
// } |
Oops, something went wrong.