KafkaTopicProducer

class KafkaTopicProducer(producer: Producer<String, ByteArray>, dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, closeTimeout: Duration = KafkaOptions.CLOSE_TIMEOUT) : TopicProducer, AutoCloseable

The TopicProducer over a Kafka producer: made by create from KafkaOptions, an idempotent producer with acks=all.

send hands each key's messages to the broker one at a time (#6782): a key's next message goes only once its previous one was acknowledged, so a message Kafka refuses for good is never overtaken by its key's later ones, which neither the idempotent producer nor max.in.flight keeps from being written after a refusal. The first message of every key is handed at once, in the call's order, on a virtual thread since a send may block while the producer fetches metadata or its buffer is full; each key then goes on as its own acknowledgements come, whatever the others wait for. A failure stops its key: its later messages of the call are not sent, while the other keys' go on. send returns once every key is done, and throws the failure of the earliest message of the call that failed, Kafka being unreachable or refusing a record for good as the SPI's typed failure (KafkaFailures). Messages of distinct keys go at once, as one batch did before; a key costs one acknowledged step per message it has after its first, the producer's linger.ms (5 ms by default) plus the round trip. Only a saga's commands derived to one aggregate of another context (CommandSender.sendAll) put several messages of one key in a call: a published stream's sink sends one record a call, and a receipt, a timer's command or a routed command one message. A message's partition is the one it names (TopicMessage.partition), else its key's (the producer's default partitioner), so the messages of an aggregate keep their order in one partition; the idempotent producer keeps that order through its own retries.

close flushes what is in flight and closes the producer, waiting at most closeTimeout (KafkaOptions.CLOSE_TIMEOUT); past it, the producer fails the records it has not delivered, and a warning says so. Their senders had not had them acknowledged, so they send them again.

Constructors

Link copied to clipboard
constructor(producer: Producer<String, ByteArray>, dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, closeTimeout: Duration = KafkaOptions.CLOSE_TIMEOUT)

Types

Link copied to clipboard

Functions

Link copied to clipboard
open override fun close()
Link copied to clipboard
open suspend override fun send(topic: String, messages: List<TopicMessage>)