Kafka: How to Create a Simple Producer in Java

Updated: January 30, 2024 By: Guest Contributor Post a comment

Introduction

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Originally conceived by Linkedin and subsequently open-sourced, Kafka is fast becoming a core piece of infrastructure for many organizations, acting as a messaging backbone for event-driven architectures.

This tutorial covers the steps to create a simple Kafka producer in Java. We will begin with an explanation of what a Kafka producer is, followed by setting up a Kafka broker. Then, we’ll dive into creating a basic Kafka producer using Java, including writing to Kafka topics with standard and custom serializers, configuring producers for semantics like idempotence and transactions, and handling errors.

Assuming you have a basic understanding of Java and messaging systems, let’s get started!

Prerequisites

Before moving onward, make sure you have:

  • Java JDK 1.8 or higher
  • Maven, a dependency management tool
  • Access to a Kafka broker; you can set up your own or use a cloud service

Setting up a Kafka Broker

Before creating a producer, you need a Kafka broker running. Here’s a quick guide on setting up a single-node broker for development purposes:

1. Download the Kafka binaries from the official website.

2. Extract the tar file to a convenient location.

3. Start the ZooKeeper service included with Kafka, as it’s required for running a Kafka broker:

bin/zookeeper-server-start.sh config/zookeeper.properties

4. Open a new terminal window and start the Kafka server:

bin/kafka-server-start.sh config/server.properties

With the Kafka broker running, let’s create a simple Java producer.

Setting up a Java Project

With Maven, create a new Java project and add the following dependency to your pom.xml:

<dependencies>
  <dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>[specify-latest-version]</version>
</dependency>
</dependencies>

This dependency includes the necessary Kafka client libraries for Java.

A Basic Kafka Producer

Kafka producers send records to topics. The data sent to Kafka is a key-value pair with an optional partition key.

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class SimpleProducer {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    Producer<String, String> producer = new KafkaProducer<>(props);

    for(int i = 0; i < 10; i++) {
      producer.send(new ProducerRecord<String, String>("myTopic", Integer.toString(i), "Message" + i));
    }

    producer.close();
  }
}

The above program sends ten messages to `myTopic` on a local Kafka broker running at `localhost:9092`.

Advanced Kafka Producer

For more robust production code, consider settings such as:

  • Acks: Controls the number of acknowledgments the producer requires from brokers.
  • Retries: Setting for automatic retries if a message fails to send.
  • Batch size: Controls how many bytes of data to collect before sending messages.
  • Linger.ms: Controls the amount of time to wait for additional messages before sending a batch.
     props.put("acks", "all");
     props.put("retries", 2);
     props.put("batch.size", 16384);
     props.put("linger.ms", 1);
     props.put("buffer.memory", 33554432);

Besides the properties used for improved resilience and performance, handling the send callback allows you to process asynchronous confirmations of message delivery:

producer.send(new ProducerRecord<String, String>("myTopic", "key", "value"), new Callback() {
  @Override
  public void onCompletion(RecordMetadata metadata, Exception e) {
    if(e != null) {
      // Handle exception
    } else {
      // Perform action on successful delivery
    }
  }
});

This callback gives you insight into whether your message was sent successfully or if an exception occurred.

Conclusion

In this tutorial, we took a practical approach to creating a Kafka producer in Java. Starting with the basics, we progressed to more advanced configurations and error handling to ensure robust message delivery. Apache Kafka, combined with a solid understanding of its producer API, unlocks the potential for highly scalable and reliable data-driven applications.