KafkaItemReader and KafkaItemWriter

Apache Kafka is a distributed publish-subscribe messaging system. jberet-support module includes kafkaItemReader and kafkaItemWriter to read from or write to Kafka topics. kafkaItemReader reads and binds data to instances of custom POJO bean provided by the batch application.

kafkaItemReader keeps track of the current read position, including current topic name, topic partition number, and topic partition offset. Therefore, it is recommended to disable Kafka auto commit in Kafka consumer properties, e.g., enable.auto.commit=false. Kafka consumer properties are specified in batch property configFile.

This reader class supports retry and restart, using the tracked read position as checkpoint info. It is also recommended to turn off Kafka consumer automatic group management; instead manually assign topics and partitions for the consumer. See batch property topicPartitions.

The following dependencies are required for kafkaItemReader and kafkaItemWriter:


Batch Configuration Properties in Job XML

kafkaItemReader and kafkaItemWriter are configured through <reader> or <writer> batch properties in job xml. All properties are of type String, unless noted otherwise. The following is an example job xml that references kafkaItemReader and kafkaItemWriter:

<chunk item-count="5">
  <reader ref="kafkaItemReader">
      <property name="configFile" value="kafka-consumer.properties"/>
      <property name="topicPartitions" value="#{jobParameters['topicPartitions']}"/>
      <property name="pollTimeout" value="#{jobParameters['pollTimeout']}"/>
<chunk item-count="5">
  <writer ref="kafkaItemWriter">
      <property name="configFile" value="kafka-producer.properties"/>
      <property name="topicPartition" value="#{jobParameters['topicPartition']}"/>
      <property name="recordKey" value="#{jobParameters['recordKey']}"/>

Batch Properties for Both kafkaItemReader and kafkaItemWriter


The file path or URL to the Kafka configuration properties file. See Kafka docs for valid property keys and values.

Batch Properties for kafkaItemReader



A list of topic-and-partition in the form of "topicName1:partitionNumber1, topicName2:partitionNumber2, ". For example, "orders:0, orders:1, returns:0, returns:1".



The time, in milliseconds, spent waiting in poll if data is not available. If 0, returns immediately with any records that are available now. Must not be negative.

Batch Properties for kafkaItemWriter


A topic partition in the form of <topicName>:<partitionNumber>. For example, "orders:0". Unlike KafkaItemReader, which accepts multiple TopicPartition as source, this writer class only accepts 1 TopicPartition as destination.


The key used when sending ProducerRecord.