Skip to content

Commit 39148f9

Browse files
authored
Merge pull request #26 from team-gogo/hotfix/batch-item-writer
[hotfix] 매치 정산 일괄 처리 수정
2 parents 61667f3 + fa610f0 commit 39148f9

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/main/kotlin/gogo/gogobetting/domain/batch/root/application/listener/BatchApplicationEventListener.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gogo.gogobetting.domain.batch.root.application.listener
22

33
import gogo.gogobetting.domain.batch.root.event.BatchCancelEvent
4-
import gogo.gogobetting.domain.batch.root.event.MatchBatchEvent
54
import gogo.gogobetting.global.kafka.publisher.BatchPublisher
65
import org.slf4j.LoggerFactory
76
import org.springframework.stereotype.Component
@@ -15,14 +14,6 @@ class BatchApplicationEventListener(
1514

1615
private val log = LoggerFactory.getLogger(this::class.java.simpleName)
1716

18-
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
19-
fun bettingBatchEvent(event: MatchBatchEvent) {
20-
with(event) {
21-
log.info("published betting batch application event: {}", id)
22-
batchPublisher.publishBettingBatchEvent(event)
23-
}
24-
}
25-
2617
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
2718
fun batchCancelEvent(event: BatchCancelEvent) {
2819
with(event) {

src/main/kotlin/gogo/gogobetting/infra/batch/service/BettingWriter.kt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import gogo.gogobetting.domain.batch.root.persistence.BatchRepository
88
import gogo.gogobetting.domain.betting.result.persistence.BettingResult
99
import gogo.gogobetting.domain.betting.result.persistence.BettingResultRepository
1010
import gogo.gogobetting.domain.betting.root.persistence.BettingRepository
11+
import gogo.gogobetting.global.kafka.publisher.BatchPublisher
12+
import org.slf4j.LoggerFactory
1113
import org.springframework.batch.core.StepExecution
14+
import org.springframework.batch.core.annotation.AfterStep
1215
import org.springframework.batch.core.annotation.BeforeStep
1316
import org.springframework.batch.core.configuration.annotation.StepScope
1417
import org.springframework.batch.item.Chunk
@@ -27,6 +30,7 @@ class BettingWriter(
2730
private val batchRepository: BatchRepository,
2831
private val bettingRepository: BettingRepository,
2932
private val applicationEventPublisher: ApplicationEventPublisher,
33+
private val batchPublisher: BatchPublisher,
3034
) : ItemWriter<BettingResult> {
3135

3236
@Value("#{jobParameters['matchId']}")
@@ -42,17 +46,21 @@ class BettingWriter(
4246
private val bTeamScore: Int = 0
4347

4448
private var batchId: Long = 0
49+
private val accumulatedItems = mutableListOf<BettingResult>()
50+
51+
private val log = LoggerFactory.getLogger(this::class.java.simpleName)
4552

4653
@BeforeStep
4754
fun beforeStep(stepExecution: StepExecution) {
4855
val jobExecution = stepExecution.jobExecution
56+
accumulatedItems.clear()
4957
batchId = jobExecution.executionContext["batchId"]!! as Long
5058
}
5159

52-
override fun write(items: Chunk<out BettingResult>) {
53-
bettingResultRepository.saveAll(items)
54-
60+
@AfterStep
61+
fun afterStep(stepExecution: StepExecution) {
5562
val batch = batchRepository.findByIdOrNull(batchId)!!
63+
5664
batchDetailRepository.save(
5765
BatchDetail.of(
5866
batchId = batch.id,
@@ -62,24 +70,28 @@ class BettingWriter(
6270
)
6371
)
6472

65-
val successList = items.filter { it.isPredicted }
66-
.map { StudentBettingDto(
67-
bettingRepository.findByIdOrNull(it.bettingId)!!.studentId,
68-
it.earnedPoint
69-
)
73+
val successList = accumulatedItems.filter { it.isPredicted }
74+
.map {
75+
val studentId = bettingRepository.findByIdOrNull(it.bettingId)!!.studentId
76+
StudentBettingDto(studentId, it.earnedPoint)
7077
}
7178

72-
applicationEventPublisher.publishEvent(
73-
MatchBatchEvent(
74-
id = UUID.randomUUID().toString(),
75-
batchId = batchId,
76-
matchId = matchId,
77-
victoryTeamId = winTeamId,
78-
aTeamScore = aTeamScore,
79-
bTeamScore = bTeamScore,
80-
students = successList
81-
)
79+
val event = MatchBatchEvent(
80+
id = UUID.randomUUID().toString(),
81+
batchId = batchId,
82+
matchId = matchId,
83+
victoryTeamId = winTeamId,
84+
aTeamScore = aTeamScore,
85+
bTeamScore = bTeamScore,
86+
students = successList
8287
)
8388

89+
log.info("published betting batch application event: {}", event.id)
90+
batchPublisher.publishBettingBatchEvent(event)
91+
}
92+
93+
override fun write(items: Chunk<out BettingResult>) {
94+
bettingResultRepository.saveAll(items)
95+
accumulatedItems.addAll(items)
8496
}
8597
}

0 commit comments

Comments
 (0)