Kafka — Transaction Producer

Prateek
2 min readFeb 18, 2022

--

In this example, we’ll use Confluent Kafka to send messages to Kafka message hub.

Create Topic —

`kafka-topics — create — bootstrap-server localhost:9092 — replication-factor 1 — partitions 1 — topic hello-producer-1`

`kafka-topics — create — bootstrap-server localhost:9092 — replication-factor 1 — partitions 1 — topic hello-producer-2`

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kafka.examples</groupId>
<artifactId>transactional-producer</artifactId>
<version>2.3.0</version>

<properties>
<java.version>1.8</java.version>
<kafka.version>2.3.0</kafka.version>
</properties>

<build>
<plugins>
<!-- Maven Compiler Plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Apache Kafka Clients-->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.version}</version>
</dependency>

<!-- Apache Log4J2 binding for SLF4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>

Appconfig.java

class AppConfigs {
final static String applicationID = "HelloProducer";
final static String bootstrapServers = "localhost:9092,localhost:9093";
final static String topicName1 = "hello-producer-1";
final static String topicName2 = "hello-producer-2";
final static int numEvents = 2;
final static String transaction_id = "Hello-Producer-Trans";
}

HelloProducer.java

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Properties;

public class HelloProducer {
private static final Logger logger = LogManager.getLogger();

public static void main(String[] args) {

logger.info("Creating Kafka Producer...");
Properties props = new Properties();
props.put(ProducerConfig.CLIENT_ID_CONFIG, AppConfigs.applicationID);
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, AppConfigs.transaction_id);

KafkaProducer<Integer, String> producer = new KafkaProducer<>(props);
producer.initTransactions();

logger.info("Starting First Transaction...");
producer.beginTransaction();
try {
for (int i = 1; i <= AppConfigs.numEvents; i++) {
producer.send(new ProducerRecord<>(AppConfigs.topicName1, i, "Simple Message-T1-" + i));
producer.send(new ProducerRecord<>(AppConfigs.topicName2, i, "Simple Message-T1-" + i));
}
logger.info("Committing First Transaction.");
producer.commitTransaction();
}catch (Exception e){
logger.error("Exception in First Transaction. Aborting...");
producer.abortTransaction();
producer.close();
throw new RuntimeException(e);
}

logger.info("Starting Second Transaction...");
producer.beginTransaction();
try {
for (int i = 1; i <= AppConfigs.numEvents; i++) {
producer.send(new ProducerRecord<>(AppConfigs.topicName1, i, "Simple Message-T2-" + i));
producer.send(new ProducerRecord<>(AppConfigs.topicName2, i, "Simple Message-T2-" + i));
}
logger.info("Aborting Second Transaction.");
producer.abortTransaction();
}catch (Exception e){
logger.error("Exception in Second Transaction. Aborting...");
producer.abortTransaction();
producer.close();
throw new RuntimeException(e);
}

logger.info("Finished - Closing Kafka Producer.");
producer.close();

}
}

Check Data

kafka-console-consumer — bootstrap-server localhost:9092 — topic hello-producer-1 — from-beginning

Simple Message-T1–1

Simple Message-T1–2

kafka-console-consumer — bootstrap-server localhost:9092 — topic hello-producer-2 — from-beginning

Simple Message-T1–1

Simple Message-T1–2

--

--

Prateek
Prateek

Written by Prateek

Java Developer and enthusiast

No responses yet