-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BLOOM-107] 기상청 API 배치 시스템 구현 (#132)
* refactor: 날씨 메시지 타입 추가 * feat: WeatherCareMessageRepository 구현 * feat: WeatherCareMessage 배치 구현 * refactor: coroutines를 사용한 비동기 및 병렬 처리 * feat: Redis Repository 작성, Pipeline 사용 * refactor: WeatherCareMessage Serializable 추가 * refactor: 캐시 조회 후 메시지 생성 로직 구현 * refactor: coroutine map 방식에서 list add 방식으로 변경 * style: ktlint 적용 * refactor: batch 의존성 수정 * refactor: batch 의존성 수정
- Loading branch information
Showing
22 changed files
with
501 additions
and
55 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
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
8 changes: 0 additions & 8 deletions
8
api/src/main/kotlin/dnd11th/blooming/api/service/weather/WeatherMessage.kt
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
tasks.jar { enabled = true } | ||
tasks.bootJar { enabled = false } | ||
tasks.bootJar { enabled = true } | ||
|
||
dependencies { | ||
implementation(project(":common")) | ||
implementation(project(":client")) | ||
implementation(project(":domain:core")) | ||
implementation(project(":domain:redis")) | ||
|
||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
implementation("org.springframework:spring-tx") | ||
implementation("org.springframework.boot:spring-boot-starter-batch") | ||
testImplementation("org.springframework.batch:spring-batch-test") | ||
} |
13 changes: 13 additions & 0 deletions
13
batch/src/main/kotlin/dnd11th/blooming/Dnd11th8BackendBatchApplication.kt
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,13 @@ | ||
package dnd11th.blooming | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
@ConfigurationPropertiesScan | ||
class Dnd11th8BackendBatchApplication | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<Dnd11th8BackendBatchApplication>(*args) | ||
} |
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
17 changes: 17 additions & 0 deletions
17
batch/src/main/kotlin/dnd11th/blooming/batch/controller/DailyWeatherCallController.kt
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,17 @@ | ||
package dnd11th.blooming.batch.controller | ||
|
||
import dnd11th.blooming.batch.weather.WeatherCareMessageScheduler | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
class DailyWeatherCallController( | ||
private val weatherCareMessageScheduler: WeatherCareMessageScheduler, | ||
) { | ||
@GetMapping("/daily-weather-call") | ||
fun runWeatherCareMessageJob(): ResponseEntity<Void> { | ||
weatherCareMessageScheduler.run() | ||
return ResponseEntity.noContent().build() | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
batch/src/main/kotlin/dnd11th/blooming/batch/weather/WeatherCareMessageJobConfig.kt
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,48 @@ | ||
package dnd11th.blooming.batch.weather | ||
|
||
import dnd11th.blooming.core.entity.region.Region | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.Step | ||
import org.springframework.batch.core.configuration.annotation.JobScope | ||
import org.springframework.batch.core.job.builder.JobBuilder | ||
import org.springframework.batch.core.launch.support.RunIdIncrementer | ||
import org.springframework.batch.core.repository.JobRepository | ||
import org.springframework.batch.core.step.builder.StepBuilder | ||
import org.springframework.batch.item.ItemReader | ||
import org.springframework.batch.item.ItemWriter | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.transaction.PlatformTransactionManager | ||
|
||
@Configuration | ||
class WeatherCareMessageJobConfig { | ||
companion object { | ||
const val CHUNK_SIZE: Int = 1000 | ||
} | ||
|
||
@Bean | ||
fun weatherCareMessageBatchJob( | ||
jobRepository: JobRepository, | ||
weatherCareMessageBatchStep: Step, | ||
): Job { | ||
return JobBuilder("weatherBatchJob", jobRepository) | ||
.incrementer(RunIdIncrementer()) | ||
.start(weatherCareMessageBatchStep) | ||
.build() | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
fun weatherCareMessageBatchStep( | ||
jobRepository: JobRepository, | ||
transactionManager: PlatformTransactionManager, | ||
weatherCareMessageItemReader: ItemReader<Region>, | ||
weatherCareMessageItemWriter: ItemWriter<Region>, | ||
): Step { | ||
return StepBuilder("weatherBatchStep", jobRepository) | ||
.chunk<Region, Region>(CHUNK_SIZE, transactionManager) | ||
.reader(weatherCareMessageItemReader) | ||
.writer(weatherCareMessageItemWriter) | ||
.build() | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
batch/src/main/kotlin/dnd11th/blooming/batch/weather/WeatherCareMessageReader.kt
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,20 @@ | ||
package dnd11th.blooming.batch.weather | ||
|
||
import dnd11th.blooming.core.entity.region.Region | ||
import dnd11th.blooming.core.repository.region.RegionRepository | ||
import org.springframework.batch.core.configuration.annotation.StepScope | ||
import org.springframework.batch.item.support.ListItemReader | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class WeatherCareMessageReader( | ||
private val regionRepository: RegionRepository, | ||
) { | ||
@Bean | ||
@StepScope | ||
fun weatherCareMessageItemReader(): ListItemReader<Region> { | ||
val regions: List<Region> = regionRepository.findAll() | ||
return ListItemReader(regions) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
batch/src/main/kotlin/dnd11th/blooming/batch/weather/WeatherCareMessageScheduler.kt
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,30 @@ | ||
package dnd11th.blooming.batch.weather | ||
|
||
import dnd11th.blooming.common.util.Logger.Companion.log | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.JobParameters | ||
import org.springframework.batch.core.JobParametersBuilder | ||
import org.springframework.batch.core.launch.JobLauncher | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.stereotype.Component | ||
import org.springframework.util.StopWatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Component | ||
class WeatherCareMessageScheduler( | ||
private val jobLauncher: JobLauncher, | ||
@Qualifier("weatherCareMessageBatchJob") | ||
private val weatherCareMessageJob: Job, | ||
) { | ||
fun run() { | ||
val jobParameters: JobParameters = | ||
JobParametersBuilder() | ||
.addLong("time", System.currentTimeMillis()) | ||
.toJobParameters() | ||
val stopWatch = StopWatch() | ||
stopWatch.start() | ||
jobLauncher.run(weatherCareMessageJob, jobParameters) | ||
stopWatch.stop() | ||
log.info { stopWatch.getTotalTime(TimeUnit.MILLISECONDS) } | ||
} | ||
} |
Oops, something went wrong.