Package org.springframework.batch.item.kafka

# Class KafkaItemWriter<K,T>

java.lang.Object
org.springframework.batch.item.KeyValueItemWriter<K,T>
org.springframework.batch.item.kafka.KafkaItemWriter<K,T>

All Implemented Interfaces:
  `ItemWriter<T>`, `org.springframework.beans.factory.InitializingBean`

public class KafkaItemWriter<K,T> extends KeyValueItemWriter<K,T>

An `ItemWriter` implementation for Apache Kafka using a `KafkaTemplate` with default topic configured.

Since:
  4.2
Author:
  Mathieu Ouellet, Mahmoud Ben Hassine

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected org.springframework.kafka.core.KafkaTemplate<K,T>`
  `kafkaTemplate`

  ### Fields inherited from class org.springframework.batch.item.KeyValueItemWriter

  `delete, itemKeyMapper`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `KafkaItemWriter()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `flush()`
  Flush items to the key/value store.
  `protected void`
  `init()`
  afterPropertiesSet() hook
  `void`
  `setKafkaTemplate(org.springframework.kafka.core.KafkaTemplate<K,T> kafkaTemplate)`
  Set the `KafkaTemplate` to use.
  `void`
  `setTimeout(long timeout)`
  The time limit to wait when flushing items to Kafka.
  `protected void`
  `writeKeyValue(K key, T value)`
  Subclasses implement this method to write each item to key value store

  ### Methods inherited from class org.springframework.batch.item.KeyValueItemWriter

  `afterPropertiesSet, setDelete, setItemKeyMapper, write`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### kafkaTemplate

    protected org.springframework.kafka.core.KafkaTemplate<K,T> kafkaTemplate

- ## Constructor Details

  - ### KafkaItemWriter

    public KafkaItemWriter()

- ## Method Details

  - ### writeKeyValue

    protected void writeKeyValue(K key, T value)
    Description copied from class: `KeyValueItemWriter`
    Subclasses implement this method to write each item to key value store
    Specified by:
      `writeKeyValue` in class `KeyValueItemWriter<K,T>`
    Parameters:
      `key` - the key
      `value` - the item

  - ### flush

    protected void flush() throws Exception
    Description copied from class: `KeyValueItemWriter`
    Flush items to the key/value store.
    Overrides:
      `flush` in class `KeyValueItemWriter<K,T>`
    Throws:
      `Exception` - if unable to flush items

  - ### init

    protected void init()
    Description copied from class: `KeyValueItemWriter`
    afterPropertiesSet() hook
    Specified by:
      `init` in class `KeyValueItemWriter<K,T>`

  - ### setKafkaTemplate

    public void setKafkaTemplate(org.springframework.kafka.core.KafkaTemplate<K,T> kafkaTemplate)
    Set the `KafkaTemplate` to use.
    Parameters:
      `kafkaTemplate` - to use

  - ### setTimeout

    public void setTimeout(long timeout)
    The time limit to wait when flushing items to Kafka.
    Parameters:
      `timeout` - milliseconds to wait, defaults to -1 (no timeout).
    Since:
      4.3.2