Apache Kafka Integration with MuleSoft

What Is Apache Kafka?

Apache Kafka powers real-time data pipelines and stream processing applications as a powerful distributed streaming platform. It delivers fault tolerance, high throughput, and low latency to manage real-time data streams efficiently. Organizations use Kafka to handle large volumes of data, perform log aggregation, support event sourcing, and drive real-time analytics. It employs a publish-subscribe model to decouple systems and ensure seamless communication.

Kafka works on a pub-sub model and consists of core components such as:

  • Topics: It is a category or channel where messages are published. Each topic has a unique name across the entire kafka cluster.
  • Producers: Publish data to topics.
  • Consumers: Subscribe to topics and consume the data.
  • Broker: Kafka server that stores and serves messages.

Operations of Apache Kafka in MuleSoft Palette

  • Batch Message Listener: It allows to receive messages in a single batch,useful for improving performance when processing a large volume of records collectively.
  • Message Listener: This operation listens to a kafka topic and processes messages one at a time. It’s ideal for real-time data processing.
  • Bulk Publish: This operation enables sending multiple messages to a kafka topic in one go. It helps improve throughput by reducing the number of calls.
  • Commit: You manually acknowledge the successful processing of a message with this operation. This step becomes essential when auto-commit is turned off.
  • Consume: Unlike listeners, this operation fetches messages from Kafka only when the flow is triggered. Used for scenarios where polling is required.
  • Publish: This is used for sending messages to a specified kafka topic.
  • Seek: This allows repositioning the consumer to a specific offset in a topic. It is particularly helpful when you need to replay messages or skip certain ones based on offset values.

Prerequisites

  • Apache Kafka installed and running locally or on a server.
  • MuleSoft Anypoint Studio installed.
  • Kafka Connector for MuleSoft(available on Anypoint Exchange).
  • JDK installed (preferably 11 or above), and set java_home and path system variables.

Step 1: Set Up Kafka

  • Download the Apache Kafka from the link. (take the latest version)
Kafka Download
  • Extract the folder and keep it in C drive.
  • Click on the kafka config directory and edit server.properties, broker.properties, controller.properties.
Kafka config directory
  • Search for logs in all three above properties and edit the line from “log.dirs=/tmp/kraft-combined-logs” to “log.dirs=C:/kafka_2.13-4.0.0/kafka-logs”
Kafka logs
  • Also add controller.quorum.voters=1@localhost:9093 in server.properties.
add controller.quorum.voters=1@localhost:9093
  • Create a folder named kafka logs inside kafka.
Create kafka folder

Step 2: Create Random UUID and Assign.

  • Generate UUID with below command
Generate uuid
  • Random Id will be generated, which has to be used in next command

dxAK_j2qSkORomMwDeN6YQ – random uuid

Generate uuid using cmd

Step 3: Start the Kafka Server.

  • Open new command prompt in the folder C:\kafka_2.13-4.0.0 and type the below command
Start kafka using cmd
  • Kafka server is started.

Note: Keep the server running while testing

Step 4: Create Kafka Topic.

  • For creating topic, type the below command

sample – topic name
Kafka uses localhost:9092

Create topic with cmd
Create kafka topic with cmd

Step 5: Create Producer and Consumer

  • Type below command to create producer
Create kafka producer with cmd

Type any message

Type any message on kafka producer with cmd
  • To consume the message type below command
Consume any message on kafka with cmd

Note: If you want to view all the messages sent, then type below command

Step 6: Open Anypoint Studio and Create a Project

To create producer flow

  • Create new mule project
  • Drag and drop flow component.
  • Search and add for Apache Kafka module from Anypoint Exchange
MuleSoft Anypoint Studio Apache Kafka
  • Drag HTTP Listener from Palette and configure. Set path as /publish
MuleSoft Anypoint Studio HTTP listener
  • Drag and drop publish operation from Apache Kafka and configure it
MuleSoft Anypoint Studio Apache Kafka Producer configuration
  • Provide the topic name, and key as now() for current date and time
MuleSoft Anypoint Studio Kafka Publish Flow
  • Now run the project
Postman Kafka publish
  • To verify the message, check the command prompt by running the below command.
Verify Kafka message cmd

To create consumer flow

  • Drag and drop flow component and name it.
  • Drag Message listener and drop it in the flow.
  • Add config properties as below.
MuleSoft Anypoint Studio Kafka Message Listener
  • Add and configure the write operation of the file connector to check for the output.
MuleSoft Anypoint Studio Kafka Message Listener with write file operation
  • Run the application to test.
Run application for Kafka message cmd
Kafka message success
  • From postman testing
Postman Kafka message success
Kafka message cmd success
Kafka success postman

Conclusion:

Integrating Apache Kafka with MuleSoft unlocks powerful real-time data processing capabilities across distributed systems. With proper configuration and monitoring, this integration supports scalable, reliable, and event – driven enterprise architectures.

References: