Skip to content

Commit 5d02a82

Browse files
committed
fix: batch item writer after step
1 parent 61667f3 commit 5d02a82

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-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: 32 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,22 @@ 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) {
62+
println(">>>>> afterStep called")
5563
val batch = batchRepository.findByIdOrNull(batchId)!!
64+
5665
batchDetailRepository.save(
5766
BatchDetail.of(
5867
batchId = batch.id,
@@ -62,24 +71,29 @@ class BettingWriter(
6271
)
6372
)
6473

65-
val successList = items.filter { it.isPredicted }
66-
.map { StudentBettingDto(
67-
bettingRepository.findByIdOrNull(it.bettingId)!!.studentId,
68-
it.earnedPoint
69-
)
74+
val successList = accumulatedItems.filter { it.isPredicted }
75+
.map {
76+
val studentId = bettingRepository.findByIdOrNull(it.bettingId)?.studentId
77+
?: error("학생을 찾을 수 없습니다. bettingId=${it.bettingId}")
78+
StudentBettingDto(studentId, it.earnedPoint)
7079
}
7180

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-
)
81+
val event = MatchBatchEvent(
82+
id = UUID.randomUUID().toString(),
83+
batchId = batchId,
84+
matchId = matchId,
85+
victoryTeamId = winTeamId,
86+
aTeamScore = aTeamScore,
87+
bTeamScore = bTeamScore,
88+
students = successList
8289
)
8390

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

0 commit comments

Comments
 (0)