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..ef79474 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,21 @@ 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) { val batch = batchRepository.findByIdOrNull(batchId)!! + batchDetailRepository.save( BatchDetail.of( batchId = batch.id, @@ -62,24 +70,28 @@ 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 + 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) } }