java.lang.Object
dev.lydtech.component.framework.client.kafka.KafkaClient

public final class KafkaClient extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> List<org.apache.kafka.clients.consumer.ConsumerRecord<String,T>>
    consumeAndAssert(String testName, org.apache.kafka.clients.consumer.Consumer consumer, int expectedEventCount, int furtherPolls)
     
    <T> List<org.apache.kafka.clients.consumer.ConsumerRecord<String,T>>
    consumeAndAssert(String testName, org.apache.kafka.clients.consumer.Consumer consumer, int expectedEventCount, int furtherPolls, int awaitAtMostSeconds)
    1.
    org.apache.kafka.clients.consumer.Consumer
    createConsumer(String groupId, String topic)
    Create a standard Consumer.
    org.apache.kafka.clients.consumer.Consumer
    createConsumer(String groupId, String topic, Properties additionalConfig)
    Create a Consumer with additional config.
    org.apache.kafka.clients.producer.KafkaProducer<String,String>
    Create a standard Producer.
    org.apache.kafka.clients.producer.KafkaProducer<String,String>
    createProducer(Properties additionalConfig)
    Create a Producer with additional config.
    protected String
     
     
    org.apache.kafka.clients.consumer.Consumer
    initConsumer(String groupId, String topic, Long initialPollSeconds)
    Create a consumer and perform an initial long poll for the given number of seconds in order to clear the topic.
    org.apache.kafka.clients.consumer.Consumer
    initConsumer(String groupId, String topic, Properties additionalConfig, Long initialPollSeconds)
    Create a Consumer with additional config, and perform an initial long poll for the given number of seconds in order to clear the topic.
    org.apache.kafka.clients.producer.RecordMetadata
    sendMessage(String topic, String key, Object payload)
    Send a message synchronously without headers and with the default Producer.
    org.apache.kafka.clients.producer.RecordMetadata
    sendMessage(String topic, String key, Object payload, Map<String,String> headers)
    Send a message synchronously with the provided headers and with the default Producer.
    org.apache.kafka.clients.producer.RecordMetadata
    sendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload)
    Send a message synchronously without headers and with the provided Producer.
    org.apache.kafka.clients.producer.RecordMetadata
    sendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload, Map<String,String> headers)
    Send a message synchronously.
    Future<org.apache.kafka.clients.producer.RecordMetadata>
    sendMessageAsync(String topic, String key, Object payload)
    Send a message asynchronously without headers and with the default Producer.
    Future<org.apache.kafka.clients.producer.RecordMetadata>
    sendMessageAsync(String topic, String key, Object payload, Map<String,String> headers)
    Send a message asynchronously with the provided headers and with the default Producer.
    Future<org.apache.kafka.clients.producer.RecordMetadata>
    sendMessageAsync(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload)
    Send a message asynchronously without headers and with the provided Producer.
    Future<org.apache.kafka.clients.producer.RecordMetadata>
    sendMessageAsync(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload, Map<String,String> headers)
    Send a message asynchronously.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • brokerUrl

      protected String brokerUrl
  • Method Details

    • getInstance

      public static KafkaClient getInstance()
    • getBrokerUrl

      protected String getBrokerUrl()
    • createConsumer

      public org.apache.kafka.clients.consumer.Consumer createConsumer(String groupId, String topic)
      Create a standard Consumer.
    • createConsumer

      public org.apache.kafka.clients.consumer.Consumer createConsumer(String groupId, String topic, Properties additionalConfig)
      Create a Consumer with additional config.
    • initConsumer

      public org.apache.kafka.clients.consumer.Consumer initConsumer(String groupId, String topic, Long initialPollSeconds)
      Create a consumer and perform an initial long poll for the given number of seconds in order to clear the topic.
    • initConsumer

      public org.apache.kafka.clients.consumer.Consumer initConsumer(String groupId, String topic, Properties additionalConfig, Long initialPollSeconds)
      Create a Consumer with additional config, and perform an initial long poll for the given number of seconds in order to clear the topic.
    • createProducer

      public org.apache.kafka.clients.producer.KafkaProducer<String,String> createProducer()
      Create a standard Producer.
    • createProducer

      public org.apache.kafka.clients.producer.KafkaProducer<String,String> createProducer(Properties additionalConfig)
      Create a Producer with additional config.
    • sendMessage

      public org.apache.kafka.clients.producer.RecordMetadata sendMessage(String topic, String key, Object payload) throws Exception
      Send a message synchronously without headers and with the default Producer.
      Throws:
      Exception
    • sendMessage

      public org.apache.kafka.clients.producer.RecordMetadata sendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload) throws Exception
      Send a message synchronously without headers and with the provided Producer.
      Throws:
      Exception
    • sendMessage

      public org.apache.kafka.clients.producer.RecordMetadata sendMessage(String topic, String key, Object payload, Map<String,String> headers) throws Exception
      Send a message synchronously with the provided headers and with the default Producer.
      Throws:
      Exception
    • sendMessage

      public org.apache.kafka.clients.producer.RecordMetadata sendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload, Map<String,String> headers) throws Exception
      Send a message synchronously. Awaits for the result of the send before returning.
      Throws:
      Exception
    • sendMessageAsync

      public Future<org.apache.kafka.clients.producer.RecordMetadata> sendMessageAsync(String topic, String key, Object payload)
      Send a message asynchronously without headers and with the default Producer.
    • sendMessageAsync

      public Future<org.apache.kafka.clients.producer.RecordMetadata> sendMessageAsync(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload)
      Send a message asynchronously without headers and with the provided Producer.
    • sendMessageAsync

      public Future<org.apache.kafka.clients.producer.RecordMetadata> sendMessageAsync(String topic, String key, Object payload, Map<String,String> headers)
      Send a message asynchronously with the provided headers and with the default Producer.
    • sendMessageAsync

      public Future<org.apache.kafka.clients.producer.RecordMetadata> sendMessageAsync(org.apache.kafka.clients.producer.Producer producer, String topic, String key, Object payload, Map<String,String> headers)
      Send a message asynchronously. Allows for producing batches of messages, by tuning the producer linger.ms config.
    • consumeAndAssert

      public <T> List<org.apache.kafka.clients.consumer.ConsumerRecord<String,T>> consumeAndAssert(String testName, org.apache.kafka.clients.consumer.Consumer consumer, int expectedEventCount, int furtherPolls) throws Exception
      Throws:
      Exception
    • consumeAndAssert

      public <T> List<org.apache.kafka.clients.consumer.ConsumerRecord<String,T>> consumeAndAssert(String testName, org.apache.kafka.clients.consumer.Consumer consumer, int expectedEventCount, int furtherPolls, int awaitAtMostSeconds) throws Exception
      1. Poll for messages on the application’s outbound topic. 2. Assert the expected number are received within the specified number of seconds. 3. Performs the specified number of extra polls after the expected number received to ensure no further events. 4. Returns the consumed events.
      Throws:
      Exception