Package org.springframework.kafka.core
Interface KafkaOperations<K,V>
-
- Type Parameters:
K
- the key type.V
- the value type. If the Kafka topic is set withCreateTime
all send operations will use the user provided time if provided, elseKafkaProducer
will generate one If the topic is set withLogAppendTime
then the user provided timestamp will be ignored and instead will be the Kafka broker local time when the message is appended
- All Known Implementing Classes:
AggregatingReplyingKafkaTemplate
,KafkaTemplate
,ReplyingKafkaTemplate
,RoutingKafkaTemplate
public interface KafkaOperations<K,V>
The basic Kafka operations contract returningListenableFuture
s.- Author:
- Marius Bogoevici, Gary Russell, Biju Kunjummen
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
KafkaOperations.OperationsCallback<K,V,T>
A callback for executing arbitrary operations on theKafkaOperations
.static interface
KafkaOperations.ProducerCallback<K,V,T>
A callback for executing arbitrary operations on theProducer
.
-
Field Summary
Fields Modifier and Type Field Description static java.time.Duration
DEFAULT_POLL_TIMEOUT
Default timeout forreceive(String, int, long)
.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description <T> T
execute(KafkaOperations.ProducerCallback<K,V,T> callback)
Execute some arbitrary operation(s) on the producer and return the result.<T> T
executeInTransaction(KafkaOperations.OperationsCallback<K,V,T> callback)
Execute some arbitrary operation(s) on the operations and return the result.void
flush()
Flush the producer.default ProducerFactory<K,V>
getProducerFactory()
Return the producer factory used by this template.default boolean
inTransaction()
Return true if the template is currently running in a transaction on the calling thread.default boolean
isAllowNonTransactional()
Return true if this template, when transactional, allows non-transactional operations.boolean
isTransactional()
Return true if the implementation supports transactions (has a transaction-capable producer factory).java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric>
metrics()
SeeProducer.metrics()
.java.util.List<org.apache.kafka.common.PartitionInfo>
partitionsFor(java.lang.String topic)
SeeProducer.partitionsFor(String)
.default org.apache.kafka.clients.consumer.ConsumerRecord<K,V>
receive(java.lang.String topic, int partition, long offset)
Receive a single record with the default poll timeout (5 seconds).org.apache.kafka.clients.consumer.ConsumerRecord<K,V>
receive(java.lang.String topic, int partition, long offset, java.time.Duration pollTimeout)
Receive a single record.default org.apache.kafka.clients.consumer.ConsumerRecords<K,V>
receive(java.util.Collection<TopicPartitionOffset> requested)
Receive a multiple records with the default poll timeout (5 seconds).org.apache.kafka.clients.consumer.ConsumerRecords<K,V>
receive(java.util.Collection<TopicPartitionOffset> requested, java.time.Duration pollTimeout)
Receive multiple records.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(java.lang.String topic, java.lang.Integer partition, K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(java.lang.String topic, K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(java.lang.String topic, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(org.apache.kafka.clients.producer.ProducerRecord<K,V> record)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
send(org.springframework.messaging.Message<?> message)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
sendDefault(java.lang.Integer partition, java.lang.Long timestamp, K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
sendDefault(java.lang.Integer partition, K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
sendDefault(K key, V data)
Deprecated.org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>>
sendDefault(V data)
Deprecated.void
sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets)
Deprecated.in the 3.0.0 KafkaProducer.void
sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)
Deprecated.in the 3.0.0 KafkaProducer.default void
sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)
When running in a transaction, send the consumer offset(s) to the transaction.default KafkaOperations2<K,V>
usingCompletableFuture()
Return an implementation that returnsCompletableFuture
instead ofListenableFuture
.
-
-
-
Field Detail
-
DEFAULT_POLL_TIMEOUT
static final java.time.Duration DEFAULT_POLL_TIMEOUT
Default timeout forreceive(String, int, long)
.
-
-
Method Detail
-
sendDefault
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> sendDefault(V data)
Deprecated.Send the data to the default topic with no key or partition.- Parameters:
data
- The data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
sendDefault
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> sendDefault(K key, V data)
Deprecated.Send the data to the default topic with the provided key and no partition.- Parameters:
key
- the key.data
- The data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
sendDefault
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> sendDefault(java.lang.Integer partition, K key, V data)
Deprecated.Send the data to the default topic with the provided key and partition.- Parameters:
partition
- the partition.key
- the key.data
- the data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
sendDefault
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> sendDefault(java.lang.Integer partition, java.lang.Long timestamp, K key, V data)
Deprecated.Send the data to the default topic with the provided key and partition.- Parameters:
partition
- the partition.timestamp
- the timestamp of the record.key
- the key.data
- the data.- Returns:
- a Future for the
SendResult
. - Since:
- 1.3
- See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(java.lang.String topic, V data)
Deprecated.Send the data to the provided topic with no key or partition.- Parameters:
topic
- the topic.data
- The data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(java.lang.String topic, K key, V data)
Deprecated.Send the data to the provided topic with the provided key and no partition.- Parameters:
topic
- the topic.key
- the key.data
- The data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(java.lang.String topic, java.lang.Integer partition, K key, V data)
Deprecated.Send the data to the provided topic with the provided key and partition.- Parameters:
topic
- the topic.partition
- the partition.key
- the key.data
- the data.- Returns:
- a Future for the
SendResult
. - See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V data)
Deprecated.Send the data to the provided topic with the provided key and partition.- Parameters:
topic
- the topic.partition
- the partition.timestamp
- the timestamp of the record.key
- the key.data
- the data.- Returns:
- a Future for the
SendResult
. - Since:
- 1.3
- See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(org.apache.kafka.clients.producer.ProducerRecord<K,V> record)
Deprecated.Send the providedProducerRecord
.- Parameters:
record
- the record.- Returns:
- a Future for the
SendResult
. - Since:
- 1.3
- See Also:
usingCompletableFuture()
-
send
@Deprecated org.springframework.util.concurrent.ListenableFuture<SendResult<K,V>> send(org.springframework.messaging.Message<?> message)
Deprecated.Send a message with routing information in message headers. The message payload may be converted before sending.- Parameters:
message
- the message to send.- Returns:
- a Future for the
SendResult
. - See Also:
KafkaHeaders.TOPIC
,KafkaHeaders.PARTITION
,KafkaHeaders.KEY
,usingCompletableFuture()
-
partitionsFor
java.util.List<org.apache.kafka.common.PartitionInfo> partitionsFor(java.lang.String topic)
SeeProducer.partitionsFor(String)
.- Parameters:
topic
- the topic.- Returns:
- the partition info.
- Since:
- 1.1
-
metrics
java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric> metrics()
SeeProducer.metrics()
.- Returns:
- the metrics.
- Since:
- 1.1
-
execute
@Nullable <T> T execute(KafkaOperations.ProducerCallback<K,V,T> callback)
Execute some arbitrary operation(s) on the producer and return the result.- Type Parameters:
T
- the result type.- Parameters:
callback
- the callback.- Returns:
- the result.
- Since:
- 1.1
-
executeInTransaction
@Nullable <T> T executeInTransaction(KafkaOperations.OperationsCallback<K,V,T> callback)
Execute some arbitrary operation(s) on the operations and return the result. The operations are invoked within a local transaction and do not participate in a global transaction (if present).- Type Parameters:
T
- the result type.- Parameters:
callback
- the callback.- Returns:
- the result.
- Since:
- 1.1
-
flush
void flush()
Flush the producer.
-
sendOffsetsToTransaction
@Deprecated void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets)
Deprecated.in the 3.0.0 KafkaProducer.When running in a transaction, send the consumer offset(s) to the transaction. The group id is obtained fromKafkaUtils.getConsumerGroupId()
. It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager
) since the container will take care of sending the offsets to the transaction.- Parameters:
offsets
- The offsets.- Since:
- 1.3
-
sendOffsetsToTransaction
@Deprecated void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)
Deprecated.in the 3.0.0 KafkaProducer.When running in a transaction, send the consumer offset(s) to the transaction. It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager
) since the container will take care of sending the offsets to the transaction.- Parameters:
offsets
- The offsets.consumerGroupId
- the consumer's group.id.- Since:
- 1.3
-
sendOffsetsToTransaction
default void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)
When running in a transaction, send the consumer offset(s) to the transaction. It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager
) since the container will take care of sending the offsets to the transaction. Use with 2.5 brokers or later.- Parameters:
offsets
- The offsets.groupMetadata
- the consumer group metadata.- Since:
- 2.5
- See Also:
Producer.sendOffsetsToTransaction(Map, ConsumerGroupMetadata)
-
isTransactional
boolean isTransactional()
Return true if the implementation supports transactions (has a transaction-capable producer factory).- Returns:
- true or false.
- Since:
- 2.3
-
isAllowNonTransactional
default boolean isAllowNonTransactional()
Return true if this template, when transactional, allows non-transactional operations.- Returns:
- true to allow.
- Since:
- 2.4.3
-
inTransaction
default boolean inTransaction()
Return true if the template is currently running in a transaction on the calling thread.- Returns:
- true if a transaction is running.
- Since:
- 2.5
-
getProducerFactory
default ProducerFactory<K,V> getProducerFactory()
Return the producer factory used by this template.- Returns:
- the factory.
- Since:
- 2.5
-
receive
@Nullable default org.apache.kafka.clients.consumer.ConsumerRecord<K,V> receive(java.lang.String topic, int partition, long offset)
Receive a single record with the default poll timeout (5 seconds).- Parameters:
topic
- the topic.partition
- the partition.offset
- the offset.- Returns:
- the record or null.
- Since:
- 2.8
- See Also:
DEFAULT_POLL_TIMEOUT
-
receive
@Nullable org.apache.kafka.clients.consumer.ConsumerRecord<K,V> receive(java.lang.String topic, int partition, long offset, java.time.Duration pollTimeout)
Receive a single record.- Parameters:
topic
- the topic.partition
- the partition.offset
- the offset.pollTimeout
- the timeout.- Returns:
- the record or null.
- Since:
- 2.8
-
receive
default org.apache.kafka.clients.consumer.ConsumerRecords<K,V> receive(java.util.Collection<TopicPartitionOffset> requested)
Receive a multiple records with the default poll timeout (5 seconds). Only absolute, positive offsets are supported.- Parameters:
requested
- a collection of record requests (topic/partition/offset).- Returns:
- the records
- Since:
- 2.8
- See Also:
DEFAULT_POLL_TIMEOUT
-
receive
org.apache.kafka.clients.consumer.ConsumerRecords<K,V> receive(java.util.Collection<TopicPartitionOffset> requested, java.time.Duration pollTimeout)
Receive multiple records. Only absolute, positive offsets are supported.- Parameters:
requested
- a collection of record requests (topic/partition/offset).pollTimeout
- the timeout.- Returns:
- the record or null.
- Since:
- 2.8
-
usingCompletableFuture
default KafkaOperations2<K,V> usingCompletableFuture()
Return an implementation that returnsCompletableFuture
instead ofListenableFuture
. The methods returningListenableFuture
will be removed in 3.0- Returns:
- the implementation.
- Since:
- 2.9.
-
-