-
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.
* fix : 테스트 버그 수정 * feat : 배치알림 조회 구현 * feat : multi job 관련 설정 * feat : 알림 전송 step에 필요한 usecase * feat : 알림 전송 step * feat : 알림 전송 job * feat : Chunk size object 관리 * feat : 알림 가는 시점의 컨텐츠를 조회하는 걸로 수 * feat : AlertContent 삭제 hard delete로 변경 * feat : LocalDate Supplier 설정 * feat : 배치알림, 매핑 엔티티 저장 이벤트 로직 * feat : 배치알림, 매핑 엔티티 저장 포트, 서비 * edit : 메인 클래스 원상복구
- Loading branch information
1 parent
c03b808
commit da72900
Showing
38 changed files
with
515 additions
and
18 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
17 changes: 17 additions & 0 deletions
17
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertProcessor.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 com.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.ItemProcessor | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertProcessor( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemProcessor<AlertBatch, AlertBatch> { | ||
|
||
override fun process(alertBatch: AlertBatch): AlertBatch { | ||
alertUseCase.sendMessage(alertBatch) | ||
return alertBatch | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertReader.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,29 @@ | ||
package com.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.model.AlertBatchValue | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.ItemReader | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertReader( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemReader<AlertBatch> { | ||
|
||
private var currentPage: Int = 0 | ||
private var alertBatchList: MutableList<AlertBatch> = mutableListOf() | ||
|
||
override fun read(): AlertBatch? { | ||
if (alertBatchList.isEmpty()) { | ||
val alertBatches = alertUseCase.loadAllAlertBatch(currentPage++, AlertBatchValue.CHUNK_SIZE) | ||
alertBatchList.addAll(alertBatches) | ||
|
||
if (alertBatchList.isEmpty()) { | ||
return null | ||
} | ||
} | ||
|
||
return alertBatchList.removeFirst() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertWriter.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,19 @@ | ||
package com.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.Chunk | ||
import org.springframework.batch.item.ItemWriter | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertWriter( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemWriter<AlertBatch> { | ||
override fun write(alertBatches: Chunk<out AlertBatch>) { | ||
val batchIds = alertBatches.map { it.id } | ||
val alertContents = alertUseCase.fetchAllAlertContent(batchIds) | ||
|
||
alertUseCase.createAlerts(alertContents) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
adapters/in-batch/src/main/kotlin/com/pokit/alert/job/SendAlertConfig.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,43 @@ | ||
package com.pokit.alert.job | ||
|
||
import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl | ||
import com.navercorp.spring.batch.plus.kotlin.configuration.step.SimpleStepBuilderDsl | ||
import com.pokit.alert.component.AlertProcessor | ||
import com.pokit.alert.component.AlertReader | ||
import com.pokit.alert.component.AlertWriter | ||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.model.AlertBatchValue | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.transaction.PlatformTransactionManager | ||
import org.springframework.transaction.TransactionDefinition | ||
import org.springframework.transaction.interceptor.DefaultTransactionAttribute | ||
|
||
|
||
@Configuration | ||
class SendAlertConfig( | ||
private val transactionManager: PlatformTransactionManager, | ||
private val batch: BatchDsl, | ||
private val alertReader: AlertReader, | ||
private val alertProcessor: AlertProcessor, | ||
private val alertWriter: AlertWriter | ||
) { | ||
@Bean(name = ["sendAlertJob"]) | ||
fun sendAlertJob() = batch { | ||
job("sendAlertJob") { | ||
step(sendAlertStep()) | ||
} | ||
} | ||
|
||
@Bean | ||
fun sendAlertStep() = batch { | ||
step("sendAlertStep") { | ||
chunk(AlertBatchValue.CHUNK_SIZE, transactionManager, fun SimpleStepBuilderDsl<AlertBatch, AlertBatch>.() { | ||
reader(alertReader) | ||
processor(alertProcessor) | ||
writer(alertWriter) | ||
transactionAttribute(DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NOT_SUPPORTED)) | ||
}) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
adapters/in-batch/src/main/kotlin/com/pokit/alert/scheduler/SendAlertScheduler.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,32 @@ | ||
package com.pokit.alert.scheduler | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.JobParametersBuilder | ||
import org.springframework.batch.core.launch.JobLauncher | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SendAlertScheduler( | ||
private val jobLauncher: JobLauncher, | ||
@Qualifier("sendAlertJob") private val sendAlertJob: Job | ||
) { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
companion object { | ||
private const val 매일_오전_8시 = "0 0 8 * * *" | ||
} | ||
|
||
@Scheduled(cron = 매일_오전_8시) | ||
fun sendAlert() { | ||
val jobParameters = JobParametersBuilder() | ||
.addLong("run.id", System.currentTimeMillis()) | ||
.toJobParameters() | ||
|
||
logger.info { "[ALERT BATCH] start daily send alert job" } | ||
jobLauncher.run(sendAlertJob, jobParameters) | ||
logger.info { "[ALERT BATCH] end daily send alert job" } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
adapters/in-batch/src/main/kotlin/com/pokit/config/SchedulerConfig.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,24 @@ | ||
package com.pokit.config | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.scheduling.TaskScheduler | ||
import org.springframework.scheduling.annotation.EnableScheduling | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler | ||
|
||
@Configuration | ||
@EnableScheduling | ||
class SchedulerConfig { | ||
companion object { | ||
private const val POOL_SIZE = 3 | ||
} | ||
|
||
@Bean("schedulerTask") | ||
fun taskScheduler(): TaskScheduler { | ||
val executor = ThreadPoolTaskScheduler() | ||
executor.poolSize = POOL_SIZE | ||
executor.threadNamePrefix = "scheduler-thread-" | ||
executor.initialize() | ||
return executor | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...out-persistence/src/main/kotlin/com/pokit/out/persistence/alert/impl/AlertBatchAdapter.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,37 @@ | ||
package com.pokit.out.persistence.alert.impl | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.out.AlertBatchPort | ||
import com.pokit.out.persistence.alert.persist.AlertBatchEntity | ||
import com.pokit.out.persistence.alert.persist.AlertBatchRepository | ||
import com.pokit.out.persistence.alert.persist.toDomain | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Repository | ||
import java.time.LocalDate | ||
|
||
@Repository | ||
class AlertBatchAdapter( | ||
private val alertBatchRepository: AlertBatchRepository | ||
) : AlertBatchPort { | ||
override fun loadAllByShouldBeSentAt(now: LocalDate, pageable: Pageable): Page<AlertBatch> { | ||
return alertBatchRepository.findAllByShouldBeSentAtAfterAndSent(now, false, pageable) | ||
.map { it.toDomain() } | ||
} | ||
|
||
override fun send(alertBatch: AlertBatch) { | ||
alertBatchRepository.findByIdOrNull(alertBatch.id) | ||
?.sent() | ||
} | ||
|
||
override fun loadByUserIdAndDate(userId: Long, date: LocalDate): AlertBatch? { | ||
return alertBatchRepository.findByUserIdAndShouldBeSentAtAndSent(userId, date, false) | ||
?.run { toDomain() } | ||
} | ||
|
||
override fun persist(alertBatch: AlertBatch): AlertBatch { | ||
val alertBatchEntity = AlertBatchEntity.of(alertBatch) | ||
return alertBatchRepository.save(alertBatchEntity).toDomain() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...t-persistence/src/main/kotlin/com/pokit/out/persistence/alert/impl/AlertContentAdapter.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,27 @@ | ||
package com.pokit.out.persistence.alert.impl | ||
|
||
import com.pokit.alert.model.AlertContent | ||
import com.pokit.alert.port.out.AlertContentPort | ||
import com.pokit.out.persistence.alert.persist.AlertContentEntity | ||
import com.pokit.out.persistence.alert.persist.AlertContentRepository | ||
import com.pokit.out.persistence.alert.persist.toDomain | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class AlertContentAdapter( | ||
private val alertContentRepository: AlertContentRepository | ||
) : AlertContentPort { | ||
override fun loadAllInAlertBatchIds(ids: List<Long>): List<AlertContent> { | ||
return alertContentRepository.findAllByAlertBatchIdIn(ids) | ||
.map { it.toDomain() } | ||
} | ||
|
||
override fun deleteAll(ids: List<Long>) { | ||
alertContentRepository.deleteAllByIdInBatch(ids) | ||
} | ||
|
||
override fun persist(alertContent: AlertContent): AlertContent { | ||
val alertContentEntity = AlertContentEntity.of(alertContent) | ||
return alertContentRepository.save(alertContentEntity).toDomain() | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...rsistence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertBatchRepository.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,12 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDate | ||
|
||
interface AlertBatchRepository : JpaRepository<AlertBatchEntity, Long> { | ||
fun findAllByShouldBeSentAtAfterAndSent(now: LocalDate, send: Boolean, pageable: Pageable): Page<AlertBatchEntity> | ||
|
||
fun findByUserIdAndShouldBeSentAtAndSent(userId: Long, date: LocalDate, sent: Boolean): AlertBatchEntity? | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...istence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertContentRepository.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,7 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface AlertContentRepository : JpaRepository<AlertContentEntity, Long> { | ||
fun findAllByAlertBatchIdIn(ids: List<Long>): List<AlertContentEntity> | ||
} |
5 changes: 5 additions & 0 deletions
5
...ersistence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertJdbcRepository.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,5 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
interface AlertJdbcRepository { | ||
fun bulkInsert(alertEntities: List<AlertEntity>) | ||
} |
31 changes: 31 additions & 0 deletions
31
...stence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertJdbcRepositoryImpl.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,31 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class AlertJdbcRepositoryImpl( | ||
private val jdbcTemplate: JdbcTemplate | ||
) : AlertJdbcRepository { | ||
override fun bulkInsert(alertEntities: List<AlertEntity>) { | ||
val sql = """ | ||
INSERT INTO alert ( | ||
user_id, content_id, content_thumb_nail | ||
, title, is_deleted, created_at, updated_at | ||
) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) | ||
""".trimIndent() | ||
|
||
|
||
val batchArgs = alertEntities.map { alert -> | ||
arrayOf( | ||
alert.userId, | ||
alert.contentId, | ||
alert.contentThumbNail, | ||
alert.title, | ||
alert.deleted | ||
) | ||
} | ||
|
||
jdbcTemplate.batchUpdate(sql, batchArgs) | ||
} | ||
} |
Oops, something went wrong.