PrepZone Logo
PrepZone

Spring Kafka: Template, Listeners, and Config

KafkaTemplate and @KafkaListener for VaultCommerce — Spring Boot 3 auto-configuration and JSON serialization.

Why this matters

  • KafkaTemplate provides sync/async send with built-in serializer configuration from application.yml.
  • @KafkaListener handles container lifecycle, concurrency, and ack modes.
  • Spring Boot 3.x auto-configures bootstrap servers from spring.kafka.bootstrap-servers.
  • Production VaultCommerce services are 100% Spring Kafka, not raw clients.
OrderServiceVaultCommerce
vaultcommerce.orders.placed.v1
InventoryConsumer
KafkaTemplate publishes to a topic. @KafkaListener consumes from a consumer group.

KafkaTemplate and producers

kafkaTemplate.send(topic, key, value) uses configured ProducerFactory. DefaultKafkaProducerFactory shares one producer per config. VaultCommerce defines multiple templates only when serialization differs (Avro vs JSON).

Key points

  • KafkaTemplate — Spring wrapper for producer send operations
  • @KafkaListener — declarative consumer endpoint
  • ConcurrentKafkaListenerContainerFactory — thread pool per listener
  • AckMode — RECORD, BATCH, MANUAL, MANUAL_IMMEDIATE
  • *spring.kafka. ** — Boot centralized configuration properties

@KafkaListener configuration

concurrency=4 spawns 4 consumer threads (≤ partitions). ackMode=MANUAL_IMMEDIATE commits after listener returns. groupId from property placeholder per environment. VaultCommerce uses @RetryableTopic on critical listeners.

VaultCommerce rollout checklist

Before promoting changes that touch the VaultCommerce order and payment event backbone, run the staging KRaft cluster (Kafka 3.7+, Schema Registry 7.x) through a 10k events/min soak test. Compare producer request latency p99 and consumer lag per group against the pre-deploy baseline. KafkaTemplate for send; @KafkaListener for consume. Document the change in the internal topic registry, attach Grafana screenshots to the change ticket, and keep an engineer on lag dashboards for 30 minutes after production rollout — roll back the service release before altering broker-level settings if lag or under-replicated partitions spike.

Java
// VaultCommerce order-service — Spring Kafka 3.x
@Service
@RequiredArgsConstructor
public class OrderEventPublisher {
    private final KafkaTemplate<String, OrderPlaced> kafkaTemplate;

    public void publish(OrderPlaced event) {
        kafkaTemplate.send("vaultcommerce.orders.placed.v1", event.orderId(), event)
            .whenComplete((result, ex) -> {
                if (ex != null) throw new EventPublishException(event.orderId(), ex);
            });
    }
}

@KafkaListener(topics = "vaultcommerce.orders.placed.v1", groupId = "inventory-service", concurrency = "4")
public void handle(ConsumerRecord<String, OrderPlaced> record) {
    inventoryService.reserve(record.value());
}

Quick recall

Everything you need if you only revisit this box.

  1. KafkaTemplate for send; @KafkaListener for consume.
  2. Set concurrency ≤ partition count.
  3. Use MANUAL_IMMEDIATE ack for critical consumers.

Test yourself

Answer these before moving on — recall is what makes it stick.