Class KafkaClient


  • public final class KafkaClient
    extends Object
    • Field Detail

      • brokerUrl

        protected String brokerUrl
    • Method Detail

      • getInstance

        public static KafkaClient getInstance()
      • getBrokerUrl

        protected String getBrokerUrl()
      • createConsumer

        public org.apache.kafka.clients.consumer.Consumer createConsumer​(String groupId,
                                                                         String topic)
      • createProducer

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

        public org.apache.kafka.clients.producer.KafkaProducer<Long,​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
        1. Poll for messages on the application’s outbound topic. 2. Assert the expected number are received. 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