- Spring Boot
- Tutorial
Build Consumer
Next, create the Java consumer application by pasting the following code into a file src/main/java/examples/Consumer.java
.
package examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import java.io.IOException;
@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_KEY) String key) {
logger.info(String.format("Consumed event from topic %s: key = %-10s value = %s", topic, key, value));
}
}
Once again, you can compile the code before proceeding by running the following command:
gradle build
And you should see the following output:
BUILD SUCCESSFUL in 1s