Spring / Spring Boot 4 Basics Interview Questions
What is Spring Boot's messaging support with Kafka in Boot 4?
Spring Boot 4 ships with Spring for Apache Kafka 4.0, which introduces Share Consumer support for Kafka Queues -- allowing multiple consumers in a group to share records from the same partition rather than partitions being locked to single consumers.
// Producer: @Service public class OrderEventProducer { private final KafkaTemplate<String, OrderEvent> kafkaTemplate; public void publishOrderCreated(Order order) { OrderEvent event = new OrderEvent(order.id(), "ORDER_CREATED", Instant.now()); kafkaTemplate.send("orders-topic", order.id(), event); } } // Consumer: @Service public class OrderEventConsumer { @KafkaListener( topics = "orders-topic", groupId = "order-processing-group", concurrency = "3" // 3 consumer threads ) public void handleOrderEvent( @Payload OrderEvent event, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic, Acknowledgment ack) { try { processEvent(event); ack.acknowledge(); // manual acknowledgement } catch (Exception e) { log.error("Failed to process event: {}", event, e); // send to DLT (dead letter topic) } } } # application.yml: Kafka configuration spring: kafka: bootstrap-servers: localhost:9092 consumer: group-id: order-service auto-offset-reset: earliest key-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer properties: spring.json.trusted.packages: "com.example.events" producer: key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
