Sobes.tech
Middle

Did you work with Kafka through Spring or directly?

sobes.tech AI

Answer from AI

I have worked both with Spring Kafka and directly with Kafka API. Using Spring Kafka simplifies integration by providing convenient annotations (@KafkaListener, @KafkaTemplate) and configuration management through Spring Boot. Direct interaction with Kafka requires working with KafkaProducer and KafkaConsumer, which offers more control but requires more code and setup.

Example of sending a message via Spring Kafka:

@Autowired
private KafkaTemplate<String, String> kafkaTemplate;

public void sendMessage(String topic, String message) {
    kafkaTemplate.send(topic, message);
}

Example of consuming messages:

@KafkaListener(topics = "myTopic", groupId = "group_id")
public void listen(String message) {
    System.out.println("Received: " + message);
}