Skip to content

Instantly share code, notes, and snippets.

@mmafrar
Created February 1, 2022 13:29
Show Gist options
  • Save mmafrar/0f1d9013369875ec0bba34286a2ffe1c to your computer and use it in GitHub Desktop.
Save mmafrar/0f1d9013369875ec0bba34286a2ffe1c to your computer and use it in GitHub Desktop.
Getting Started with Apache Kafka and Spring Boot
package com.example.kafka;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
@Service
public class Consumer {
private final Logger logger = LoggerFactory.getLogger(Consumer.class);
@KafkaListener(id = "myConsumer", topics = "purchases", groupId = "spring-boot", autoStartup = "false")
public void listen(String value,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key) {
logger.info(String.format("Consumed event from topic %s: key = %-10s value = %s", topic, key, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment