|
1 | 1 | package com.loopers.interfaces.api.listener; |
2 | 2 |
|
3 | 3 | 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; |
5 | 6 | import lombok.RequiredArgsConstructor; |
6 | 7 | import lombok.extern.slf4j.Slf4j; |
7 | | -import org.springframework.scheduling.annotation.Async; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
8 | 9 | import org.springframework.stereotype.Component; |
9 | 10 | import org.springframework.transaction.event.TransactionPhase; |
10 | 11 | import org.springframework.transaction.event.TransactionalEventListener; |
|
14 | 15 | @RequiredArgsConstructor |
15 | 16 | public class ProductLikeEventListener { |
16 | 17 |
|
17 | | - private final LikeChangedEventProducer likeChangedEventProducer; |
| 18 | + private final OutboxService outboxService; |
18 | 19 |
|
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={}", |
23 | 26 | event.productId(), event.action()); |
24 | 27 |
|
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 | + ); |
30 | 37 |
|
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 | + ); |
35 | 46 | } |
36 | 47 | } |
0 commit comments