Class KafkaSender<K extends String,V extends Serializable>
- java.lang.Object
-
- org.birchframework.framework.kafka.KafkaSender<K,V>
-
- Type Parameters:
K- type of keyV- type of value
@Component @ConditionalOnClass(org.springframework.kafka.core.KafkaTemplate.class) @EnableConfigurationProperties(org.birchframework.configuration.BirchProperties.class) public class KafkaSender<K extends String,V extends Serializable> extends Object
Customized Kafka producer. Supports:- Synchronous
- Synchronous transactional
- Asynchronous
- Asynchronous transactional
birch: kafka: sender: wait-time: 2s # duration of wait time to wait for response when sending synchronously; default is 2 seconds allow-non-transactional: true # whether to configure the Kafka template to allows sending outside of a transaction context; default is true- Author:
- Keivan Khalichi
-
-
Constructor Summary
Constructors Constructor Description KafkaSender(org.springframework.kafka.core.KafkaTemplate<K,V> theTemplate, BirchProperties theProperties)Construct this sender using the providedKafkaTemplateandBirchProperties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<KafkaSendResult<K,V>>send(String topic, Integer partition, K key, V data)Send message, synchronously.Optional<KafkaSendResult<K,V>>send(String topic, K key, V data)Send message, synchronously.Optional<KafkaSendResult<K,V>>send(String topic, V data)Send message, synchronously.voidsendAsync(String topic, Integer partition, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously, not transactional, and with provided callbacks.voidsendAsync(String topic, K key, V data)Method to send message, asynchronously.voidsendAsync(String topic, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously with provided callbacks.voidsendAsync(String topic, V data)Sends message to topic asynchronously.voidsendAsync(String topic, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously with provided callbacks.voidsendAsyncTransactional(String topic, Integer partition, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously and within a Kafka transaction, with provided callbacks.Optional<KafkaSendResult<K,V>>sendTransactional(String topic, Integer partition, K key, V data)Send message synchronously and within a transaction.
-
-
-
Constructor Detail
-
KafkaSender
public KafkaSender(org.springframework.kafka.core.KafkaTemplate<K,V> theTemplate, BirchProperties theProperties)
Construct this sender using the providedKafkaTemplateandBirchProperties. By default, the instance is not transactional.
-
-
Method Detail
-
send
public Optional<KafkaSendResult<K,V>> send(String topic, K key, V data) throws InterruptedException
Send message, synchronously.- Parameters:
topic- topic namekey- message keydata- a message payload implementingSerializable- Returns:
- result, if there are no exceptions
- Throws:
InterruptedException- rethrow when waiting to get results throws the exception
-
send
public Optional<KafkaSendResult<K,V>> send(String topic, V data) throws InterruptedException
Send message, synchronously.- Parameters:
topic- topic namedata- a message payload implementingSerializable- Returns:
- result, if there are no exceptions
- Throws:
InterruptedException- rethrow when waiting to get results throws the exception
-
send
public Optional<KafkaSendResult<K,V>> send(@Nonnull String topic, @Nullable Integer partition, @Nullable K key, @Nonnull V data) throws InterruptedException
Send message, synchronously.- Parameters:
topic- topic namekey- message keydata- a message payload implementingSerializable- Returns:
- result, if there are no exceptions
- Throws:
InterruptedException- rethrow when waiting to get results throws the exception
-
sendTransactional
public Optional<KafkaSendResult<K,V>> sendTransactional(@Nonnull String topic, @Nullable Integer partition, @Nullable K key, @Nonnull V data)
Send message synchronously and within a transaction.- Parameters:
topic- topic namekey- message keydata- a message payload implementingSerializable- Returns:
- result, if there are no exceptions
- Throws:
InterruptedException- rethrow when waiting to get results throws the exception
-
sendAsync
public void sendAsync(String topic, V data)
Sends message to topic asynchronously.- Parameters:
topic- the topicdata- a message payload implementingSerializable
-
sendAsync
public void sendAsync(String topic, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
Sends message to topic asynchronously with provided callbacks.- Parameters:
topic- the topicdata- a message payload implementingSerializablesuccessCallback- the callback to call upon success
-
sendAsync
public void sendAsync(String topic, K key, V data)
Method to send message, asynchronously.- Parameters:
topic- topic namekey- message keydata- a message payload implementingSerializable
-
sendAsync
public void sendAsync(String topic, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
Sends message to topic asynchronously with provided callbacks.- Parameters:
topic- the topickey- the message keydata- a message payload implementingSerializablesuccessCallback- the callback to call upon success
-
sendAsync
public void sendAsync(@Nonnull String topic, @Nullable Integer partition, @Nullable K key, @Nonnull V data, @Nonnull org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously, not transactional, and with provided callbacks.- Parameters:
topic- the topicpartition- the topic partitionkey- the keydata- a message payload implementingSerializablesuccessCallback- the callback to call upon success
-
sendAsyncTransactional
public void sendAsyncTransactional(@Nonnull String topic, @Nullable Integer partition, @Nullable K key, @Nonnull V data, @Nonnull org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)Sends message to topic asynchronously and within a Kafka transaction, with provided callbacks.- Parameters:
topic- the topicpartition- the topic partitionkey- the keydata- a message payload implementingSerializablesuccessCallback- the callback to call upon success
-
-