Skip to content

Commit

Permalink
[FEAT] 메시지 발행 및 소비를 위한 간단한 MessageProducer · Consumer 컴포넌트 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
JIN-076 committed Jun 5, 2024
1 parent 11e0609 commit 9d23a01
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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; }

}
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);
}

}

0 comments on commit 9d23a01

Please sign in to comment.