From 5d02a82516a627327088c83bc089cd04369873bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=89=E1=85=B5=E1=86=AB=E1=84=92=E1=85=B4=E1=84=89?= =?UTF-8?q?=E1=85=A5=E1=86=BC?= Date: Mon, 14 Apr 2025 17:48:51 +0900 Subject: [PATCH 1/3] fix: batch item writer after step --- .../listener/BatchApplicationEventListener.kt | 9 ---- .../infra/batch/service/BettingWriter.kt | 50 ++++++++++++------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/gogo/gogobetting/domain/batch/root/application/listener/BatchApplicationEventListener.kt b/src/main/kotlin/gogo/gogobetting/domain/batch/root/application/listener/BatchApplicationEventListener.kt index 45f761d..7170d6e 100644 --- a/src/main/kotlin/gogo/gogobetting/domain/batch/root/application/listener/BatchApplicationEventListener.kt +++ b/src/main/kotlin/gogo/gogobetting/domain/batch/root/application/listener/BatchApplicationEventListener.kt @@ -1,7 +1,6 @@ package gogo.gogobetting.domain.batch.root.application.listener import gogo.gogobetting.domain.batch.root.event.BatchCancelEvent -import gogo.gogobetting.domain.batch.root.event.MatchBatchEvent import gogo.gogobetting.global.kafka.publisher.BatchPublisher import org.slf4j.LoggerFactory import org.springframework.stereotype.Component @@ -15,14 +14,6 @@ class BatchApplicationEventListener( private val log = LoggerFactory.getLogger(this::class.java.simpleName) - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) - fun bettingBatchEvent(event: MatchBatchEvent) { - with(event) { - log.info("published betting batch application event: {}", id) - batchPublisher.publishBettingBatchEvent(event) - } - } - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) fun batchCancelEvent(event: BatchCancelEvent) { with(event) { diff --git a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt index fbafe95..e094911 100644 --- a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt +++ b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt @@ -8,7 +8,10 @@ import gogo.gogobetting.domain.batch.root.persistence.BatchRepository import gogo.gogobetting.domain.betting.result.persistence.BettingResult import gogo.gogobetting.domain.betting.result.persistence.BettingResultRepository import gogo.gogobetting.domain.betting.root.persistence.BettingRepository +import gogo.gogobetting.global.kafka.publisher.BatchPublisher +import org.slf4j.LoggerFactory import org.springframework.batch.core.StepExecution +import org.springframework.batch.core.annotation.AfterStep import org.springframework.batch.core.annotation.BeforeStep import org.springframework.batch.core.configuration.annotation.StepScope import org.springframework.batch.item.Chunk @@ -27,6 +30,7 @@ class BettingWriter( private val batchRepository: BatchRepository, private val bettingRepository: BettingRepository, private val applicationEventPublisher: ApplicationEventPublisher, + private val batchPublisher: BatchPublisher, ) : ItemWriter { @Value("#{jobParameters['matchId']}") @@ -42,17 +46,22 @@ class BettingWriter( private val bTeamScore: Int = 0 private var batchId: Long = 0 + private val accumulatedItems = mutableListOf() + + private val log = LoggerFactory.getLogger(this::class.java.simpleName) @BeforeStep fun beforeStep(stepExecution: StepExecution) { val jobExecution = stepExecution.jobExecution + accumulatedItems.clear() batchId = jobExecution.executionContext["batchId"]!! as Long } - override fun write(items: Chunk) { - bettingResultRepository.saveAll(items) - + @AfterStep + fun afterStep(stepExecution: StepExecution) { + println(">>>>> afterStep called") val batch = batchRepository.findByIdOrNull(batchId)!! + batchDetailRepository.save( BatchDetail.of( batchId = batch.id, @@ -62,24 +71,29 @@ class BettingWriter( ) ) - val successList = items.filter { it.isPredicted } - .map { StudentBettingDto( - bettingRepository.findByIdOrNull(it.bettingId)!!.studentId, - it.earnedPoint - ) + val successList = accumulatedItems.filter { it.isPredicted } + .map { + val studentId = bettingRepository.findByIdOrNull(it.bettingId)?.studentId + ?: error("학생을 찾을 수 없습니다. bettingId=${it.bettingId}") + StudentBettingDto(studentId, it.earnedPoint) } - applicationEventPublisher.publishEvent( - MatchBatchEvent( - id = UUID.randomUUID().toString(), - batchId = batchId, - matchId = matchId, - victoryTeamId = winTeamId, - aTeamScore = aTeamScore, - bTeamScore = bTeamScore, - students = successList - ) + val event = MatchBatchEvent( + id = UUID.randomUUID().toString(), + batchId = batchId, + matchId = matchId, + victoryTeamId = winTeamId, + aTeamScore = aTeamScore, + bTeamScore = bTeamScore, + students = successList ) + log.info("published betting batch application event: {}", event.id) + batchPublisher.publishBettingBatchEvent(event) + } + + override fun write(items: Chunk) { + bettingResultRepository.saveAll(items) + accumulatedItems.addAll(items) } } From 9d03029f26151fcddbce275a6332fc6031fd9012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=89=E1=85=B5=E1=86=AB=E1=84=92=E1=85=B4=E1=84=89?= =?UTF-8?q?=E1=85=A5=E1=86=BC?= Date: Mon, 14 Apr 2025 17:50:27 +0900 Subject: [PATCH 2/3] fix: success list map --- .../gogo/gogobetting/infra/batch/service/BettingWriter.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt index e094911..62cb653 100644 --- a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt +++ b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt @@ -73,8 +73,7 @@ class BettingWriter( val successList = accumulatedItems.filter { it.isPredicted } .map { - val studentId = bettingRepository.findByIdOrNull(it.bettingId)?.studentId - ?: error("학생을 찾을 수 없습니다. bettingId=${it.bettingId}") + val studentId = bettingRepository.findByIdOrNull(it.bettingId)!!.studentId StudentBettingDto(studentId, it.earnedPoint) } From fa610f05a2d0362e37b9c2676a289e59446bd8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=89=E1=85=B5=E1=86=AB=E1=84=92=E1=85=B4=E1=84=89?= =?UTF-8?q?=E1=85=A5=E1=86=BC?= Date: Mon, 14 Apr 2025 17:53:46 +0900 Subject: [PATCH 3/3] fix: test log --- .../kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt index 62cb653..ef79474 100644 --- a/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt +++ b/src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt @@ -59,7 +59,6 @@ class BettingWriter( @AfterStep fun afterStep(stepExecution: StepExecution) { - println(">>>>> afterStep called") val batch = batchRepository.findByIdOrNull(batchId)!! batchDetailRepository.save(