Class KafkaClient
- java.lang.Object
-
- dev.lydtech.component.framework.client.kafka.KafkaClient
-
public final class KafkaClient extends Object
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<org.apache.kafka.clients.consumer.ConsumerRecord<String,String>>consumeAndAssert(String testName, org.apache.kafka.clients.consumer.Consumer consumer, int expectedEventCount, int furtherPolls)1.org.apache.kafka.clients.consumer.ConsumercreateConsumer(String groupId, String topic)org.apache.kafka.clients.producer.KafkaProducer<Long,String>createProducer()Create a standard Producer.org.apache.kafka.clients.producer.KafkaProducer<Long,String>createProducer(Properties additionalConfig)Create a Producer with additional config.static KafkaClientgetInstance()org.apache.kafka.clients.producer.RecordMetadatasendMessage(String topic, String key, String payload)Send a message synchronously without headers and with the default Producer.org.apache.kafka.clients.producer.RecordMetadatasendMessage(String topic, String key, String payload, Map<String,String> headers)Send a message synchronously with the provided headers and with the default Producer.org.apache.kafka.clients.producer.RecordMetadatasendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, String payload)Send a message synchronously without headers and with the provided Producer.org.apache.kafka.clients.producer.RecordMetadatasendMessage(org.apache.kafka.clients.producer.Producer producer, String topic, String key, String payload, Map<String,String> headers)Send a message synchronously.Future<org.apache.kafka.clients.producer.RecordMetadata>sendMessageAsync(String topic, String key, String payload)Send a message asynchronously without headers and with the default Producer.Future<org.apache.kafka.clients.producer.RecordMetadata>sendMessageAsync(String topic, String key, String 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, String 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, String payload, Map<String,String> headers)Send a message asynchronously.
-
-
-
Method Detail
-
getInstance
public static KafkaClient getInstance()
-
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, String 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, String payload) throws ExceptionSend 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, String 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, String payload, Map<String,String> headers) throws ExceptionSend 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, String 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, String 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, String 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, String payload, Map<String,String> headers)
Send a message asynchronously. Allows for producing batches of messages, by tuning the producer linger.ms config.
-
consumeAndAssert
public List<org.apache.kafka.clients.consumer.ConsumerRecord<String,String>> 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
-
-