Skip to content

Micronaut Kafka

DodaTech 1 min read

In this tutorial, you'll learn about Fix Micronaut Kafka Producer Not Sending. We cover key concepts, practical examples, and best practices.

The Problem

Kafka messages are not sent or received in a Micronaut application.

Quick Fix

Add Kafka dependency

Wrong:

<!-- No kafka -->

Output:

Cannot connect

Right:

<dependency>
  <groupId>io.micronaut.configuration</groupId>
  <artifactId>micronaut-kafka</artifactId>
</dependency>

Output:

Kafka enabled

Configure @KafkaClient

Wrong:

// No client interface

Output:

No producer

Right:

@KafkaClient
public interface ProductClient {
    @Topic("products")
    void sendProduct(@KafkaKey String key, Product product);
}

Output:

Producer created

Configure consumer

Wrong:

// No consumer

Output:

No messages received

Right:

@KafkaListener(offsetReset = OffsetReset.EARLIEST)
public class ProductListener {
    @Topic("products")
    public void receive(Product product) { }
}

Output:

Consumer configured

Prevention

  • Add micronaut-kafka dependency
  • Define @KafkaClient producer interface
  • Create @KafkaListener for message consumption

Common Mistakes with kafka

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world MICRONAUT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the @KafkaKey annotation for?

@KafkaKey specifies the Kafka record key. Messages without a key are sent with a null key.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro