Vai al contenuto

Kafka2Rest Kotlin Library

This library allow to consume kafka messages and handle them making HTTP REST calls.


To include the library as a dependency of your Maven/Gradle project use the following snippets.

install Kafka2Rest




implementation 'eu.mia-platform:kafka2rest:{VERSION}'

install Kafka2Rest dependencies

You need to add Fuel repository to your build.gradle or pom.xml.


repositories {
   maven {
        name 'spring-lib-release'
        url ''


Kafka2Rest is based on Filters and Processors. You can develop your custom Filters and Processor implementing the FilterInterface and ProcessorInterface or use existent filters and processors.


First of all you need to initialize Kafka2Rest with: * Generics <K, V> used in library ConsumerRecords. * Kafka Consumer. Remember to set false "" property if you need to handle processors failures. * A Config object.

Config Parameters

Parameter Meaning
maxIntervalBetweenPolls The maximum time (Duration) interval between two consecutive poll. When this timeout is exceeded the method isRunning() returns false.
topicList The list of topic to subscribe to.
pollTimeout The time (Duration) spent waiting in poll if data is not available in the buffer. If 0, returns immediately with any records that are available currently in the buffer, else returns empty. Must not be negative.

Code Sample

    val props = Properties()
    props["bootstrap.servers"] = "http://your-kafka-host:9092"
    props[""] = "consumer-group-id"
    props["key.deserializer"] =
    props["value.deserializer"] =
    props[""] = Int.MAX_VALUE
    props[""] = Int.MAX_VALUE
    props[""] = false

    val consumer = KafkaConsumer<String, String>(props)

    val config = ConfigBuilder
        .addTopicList(listOf("topic1", "topic2"))

    val kafka2rest = Kafka2Rest<String, String>(consumer, config)

Messages Handling

Then you need to set the Filter And Processor couples. Filter parse kafka message and return boolean . If true the linked processor run its process() method and do something.

    kafka2rest.set(Filter1(), HTTPPostProcessor())
    kafka2rest.set(Filter1(), MyCustomProcessor())
    kafka2rest.set(Filter2(), OtherProcessor())
    // ...


Just start Kafka2Rest. Invonking the start() method Kafka2Rest will run on new thread and listen kafka topics.


Health Check

Kafka2rest offers two method to verify service and connection healthiness.

  • isRunning() This method verifies the timeout between two consecutive poll. It returns true when the config maxIntervalBetweenPolls timeout is respected. This ensures that the consumer is polling and the processors aren't getting stuck.
  • isConnected() This method verifies that consumer can list broker topics and it must be connected to kafka server to do this. It returns true when topics are more than 0.

ProducerManager (beta)

You can also send messages to kafka host building ProducerManager and using the sendMessage() method.

    val producerManager = ProducerManager("localhost:9092")
    producerManager.sendMessage(myKafkaMessage, "topic1")


All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.



  • Removed all constructor except the primary.
  • ProducerManager has been detached from Kafka2Rest controller class.


  • CAOED-147 health methods isRunning() and isConnected().


  • TNPT1-41 Avoid to continue to process messages on commit error



  • update close method for graceful shut down


  • LUEE-41 Added a commit error handler callback function to notify application of commit failures
  • CC1B-442 Allow to pass headers in RESTController.get() and RESTController.put()
  • CC1B-441 Allow to pass key (not required) in KafkaMessage()
  • TNPT1-29 Retry if processor fails





  • publish script updated into the build.gradle file


  • offset value fixed on the commitSync function
  • MP4-932 Make http sync