Skip to content

Commit 9838f76

Browse files
committed
feat: Outbox 이벤트 저장을 위한 Kafka 이벤트 리스너 수정
1 parent a333fbf commit 9838f76

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.loopers.interfaces.api.listener;
22

33
import com.loopers.domain.like.LikeEvent;
4-
import com.loopers.infrastructure.kafka.producer.LikeChangedEventProducer;
4+
import com.loopers.domain.outbox.OutboxService;
5+
import com.loopers.infrastructure.kafka.dto.LikeChangedDto;
56
import lombok.RequiredArgsConstructor;
67
import lombok.extern.slf4j.Slf4j;
7-
import org.springframework.scheduling.annotation.Async;
8+
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.stereotype.Component;
910
import org.springframework.transaction.event.TransactionPhase;
1011
import org.springframework.transaction.event.TransactionalEventListener;
@@ -14,23 +15,33 @@
1415
@RequiredArgsConstructor
1516
public class ProductLikeEventListener {
1617

17-
private final LikeChangedEventProducer likeChangedEventProducer;
18+
private final OutboxService outboxService;
1819

19-
@Async
20-
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
21-
public void handleLikeChangedKafkaEvent(LikeEvent event) {
22-
log.debug("좋아요 Kafka 이벤트 발행 시작: productId={}, action={}",
20+
@Value("${kafka.topic.product-like-name}")
21+
private String productLikeTopic;
22+
23+
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
24+
public void handleLikeChangedOutboxEvent(LikeEvent event) {
25+
log.debug("좋아요 Outbox 이벤트 저장: productId={}, action={}",
2326
event.productId(), event.action());
2427

25-
try {
26-
String likeType = switch (event.action()) {
27-
case ADDED -> "LIKED";
28-
case REMOVED -> "UNLIKED";
29-
};
28+
String likeType = switch (event.action()) {
29+
case ADDED -> "LIKED";
30+
case REMOVED -> "UNLIKED";
31+
};
32+
33+
LikeChangedDto payload = LikeChangedDto.of(
34+
event.productId(),
35+
likeType
36+
);
3037

31-
likeChangedEventProducer.sendLikeChangedEvent(event.productId(), likeType);
32-
} catch (Exception e) {
33-
log.error("좋아요 Kafka 이벤트 발행 실패: productId={}", event.productId(), e);
34-
}
38+
outboxService.saveEvent(
39+
"PRODUCT",
40+
event.productId().toString(),
41+
"LIKE_CHANGED",
42+
productLikeTopic,
43+
event.productId().toString(),
44+
payload
45+
);
3546
}
3647
}

0 commit comments

Comments
 (0)