-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] 메시지 발행 및 소비를 위한 간단한 MessageProducer · Consumer 컴포넌트 개발
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/io/oeid/mogakgo/core/properties/kafka/MessageConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.oeid.mogakgo.core.properties.kafka; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.oeid.mogakgo.domain.event.Event; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class MessageConsumer { | ||
|
||
private final ObjectMapper objectMapper; | ||
private List<Event> eventRepo = new ArrayList<>(); | ||
|
||
@KafkaListener(topics = "my-topic", groupId = "my-group", containerFactory = "kafkaListenerContainerFactory") | ||
protected void consume(@Payload String payload) throws Exception { | ||
log.info("receive event: {}", payload); | ||
Event event = objectMapper.readValue(payload, Event.class); | ||
eventRepo.add(event); | ||
|
||
// Process | ||
// acknowledgment.acknowledge(); | ||
} | ||
|
||
public List<Event> getEventRepo() { return eventRepo; } | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/oeid/mogakgo/core/properties/kafka/MessageProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.oeid.mogakgo.core.properties.kafka; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class MessageProducer { | ||
|
||
private final KafkaTemplate<String, String> kafkaTemplate; | ||
|
||
public void sendMessage(String topic, String message) { | ||
kafkaTemplate.send(topic, message); | ||
} | ||
|
||
} |