The Producer API
As discussed in Chapter 2, the Producer is an application that publishes records or messages to Kafka. In order to explore and try out this functionality, we are going to use the Kafka Producer API to publish some messages to the Kafka topics. We will primarily use the Kafka Client API library, which is a thread-safe module. So let's quickly start by implementing a basic Producer example.
Before we start with the basic implementation, as promised in our first chapter, we are going to help Steve and Jane convert their monolithic application to a message-driven architecture. So let's pick a basic microservice that we can use to start with.
Since we will be focusing on a simple Publisher module, we will start with the Notification Service, where some applications would notify a User with a message for a given Order.
Simple Kafka Producer Implementation
Let's create our simple Maven based Java application in one of our favorite IDEs. We will add the following libraries as part of the initial POM:
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>