K
- the key type.V
- the value type.public class KafkaTemplate<K,V> extends java.lang.Object implements KafkaOperations<K,V>
Modifier and Type | Field and Description |
---|---|
protected org.apache.commons.logging.Log |
logger |
Constructor and Description |
---|
KafkaTemplate(ProducerFactory<K,V> producerFactory)
Create an instance using the supplied producer factory.
|
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(int partition,
K key,
V data)
Send the data to the default topic with the provided key and partition.
|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(K key,
V data)
Send the data to the default topic with the provided key and no partition.
|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(java.lang.String topic,
int partition,
K key,
V data)
Send the data to the provided topic with the provided key and partition.
|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(java.lang.String topic,
K key,
V data)
Send the data to the provided topic with the provided key and no partition.
|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(java.lang.String topic,
V data)
Send the data to the provided topic with no key or partition.
|
java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
convertAndSend(V data)
Send the data to the default topic with no key or partition.
|
protected java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> |
doSend(org.apache.kafka.clients.producer.ProducerRecord<K,V> producerRecord)
Send the producer record.
|
void |
flush()
Flush the producer.
|
java.lang.String |
getDefaultTopic()
The default topic for send methods where a topic is not
providing.
|
void |
setDefaultTopic(java.lang.String defaultTopic)
Set the default topic for send methods where a topic is not
providing.
|
void |
setProducerListener(ProducerListener<K,V> producerListener)
Set a
ProducerListener which will be invoked when Kafka acknowledges
a send operation. |
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(int partition,
K key,
V data)
Send the data to the default topic with the provided key and partition.
|
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(K key,
V data)
Send the data to the default topic with the provided key and no partition;
wait for result.
|
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(java.lang.String topic,
int partition,
K key,
V data)
Send the data to the provided topic with the provided key and partition;
wait for result.
|
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(java.lang.String topic,
K key,
V data)
Send the data to the provided topic with the provided key and no partition;
wait for result.
|
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(java.lang.String topic,
V data)
Send the data to the provided topic with no key or partition.;
wait for result.
|
org.apache.kafka.clients.producer.RecordMetadata |
syncConvertAndSend(V data)
Send the data to the default topic with no key or partition;
wait for result.
|
public KafkaTemplate(ProducerFactory<K,V> producerFactory)
producerFactory
- the producer factory.public java.lang.String getDefaultTopic()
public void setDefaultTopic(java.lang.String defaultTopic)
defaultTopic
- the topic.public void setProducerListener(ProducerListener<K,V> producerListener)
ProducerListener
which will be invoked when Kafka acknowledges
a send operation.producerListener
- the listener.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
data
- The data.RecordMetadata
.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(K key, V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
key
- the key.data
- The data.RecordMetadata
.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(int partition, K key, V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
partition
- the partition.key
- the key.data
- the data.RecordMetadata
.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(java.lang.String topic, V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.data
- The data.RecordMetadata
.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(java.lang.String topic, K key, V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.key
- the key.data
- The data.RecordMetadata
.public java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> convertAndSend(java.lang.String topic, int partition, K key, V data)
KafkaOperations
convertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.partition
- the partition.key
- the key.data
- the data.RecordMetadata
.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
data
- The data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(K key, V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
key
- the key.data
- The data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(int partition, K key, V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
partition
- the partition.key
- the key.data
- the data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(java.lang.String topic, V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.data
- The data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(java.lang.String topic, K key, V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.key
- the key.data
- The data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.public org.apache.kafka.clients.producer.RecordMetadata syncConvertAndSend(java.lang.String topic, int partition, K key, V data) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
KafkaOperations
syncConvertAndSend
in interface KafkaOperations<K,V>
topic
- the topic.partition
- the partition.key
- the key.data
- the data.RecordMetadata
.java.lang.InterruptedException
- thread interrupted while awaiting result.java.util.concurrent.ExecutionException
- execution exception while awaiting result.protected java.util.concurrent.Future<org.apache.kafka.clients.producer.RecordMetadata> doSend(org.apache.kafka.clients.producer.ProducerRecord<K,V> producerRecord)
producerRecord
- the producer record.RecordMetadata
.public void flush()
KafkaOperations
flush
in interface KafkaOperations<K,V>