0.4.0
This release is sees a big new feature Publishing and a lot of minor improvements and fixes towards 1.0 in the receiver.
PublisherScope
A PublishScope, that can offer message (doesn't await ack), or publish which is offer + ack.
The block however waits all the offer inside, similar to coroutineScope and re-throws any failed offer.
With transaction block, that wraps the block in the correct transaction semantics and has same behavior of await offer. A transaction blocks cannot be nested, thanks @PublisherDSL.
publisher.publishScope {
offer((1..10).map {
ProducerRecord(topic.name(), "$it", "msg-$it")
})
publish((11..20).map {
ProducerRecord(topic.name(), "$it", "msg-$it")
})
transaction {
// transaction { } illegal to be called here DslMarker magic
offer((21..30).map {
ProducerRecord(topic.name(), "$it", "msg-$it")
})
publish((31..40).map {
ProducerRecord(topic.name(), "$it", "msg-$it")
})
}// Waits until all offer finished in transaction, fails if any failed
// looping
(0..100).forEach {
delay(100.milliseconds)
val record = ProducerRecord(topic.name(), "$it", "msg-$it")
offer(record)
}
// streaming
flow(1..100)
.onEach { delay(100.milliseconds) }
.map { ProducerRecord(topic.name(), "$it", "msg-$it") }
.collect { offer(it) }
}
See KafkaPublisherSpec for more examples.
Flow publish records
Often we need to receiver/consume events from Kafka, and as a result we need to publish new events to Kafka. That typically requires a streaming solution to produce records into Kafka, and keeping track of all published records into Kafka and their lifecycle and wiring that back into a stream is tricky. So we kotlin-kafka
now offers Flow.produce
build in the same style as PublisherScope
!
produce
will send message to Kafka, and stream Result
of RecordMetadata
back to the user.
It will not stop sending messages if any error occurs, you can throw it in the collector if you want the stream to stop. Otherwise, use produceOrThrow
.
Any encountered errors will be sent to the collector as [Result.failure], and they will also be rethrown if the Flow completes without handling them (e.g. using Flow.catch
). Check Kafka's [Callback] documentation for more information on when and which errors are thrown, and which are recoverable.
suspend fun publish messages(bootStrapServers: String, topic: String) {
val publisherSettings = PublisherSettings(
bootstrapServers = bootStrapServers,
keySerializer = StringSerializer(),
valueSerializer = StringSerializer()
)
(0..10_000).asFlow()
.onEach { delay(10.milliseconds) }
.map { index ->
ProducerRecord<String, String>(topic.name(), index % 4, "$index", "Message $index")
}.produce(publisherSettings)
.collect { metadata: Result<RecordMetadata> ->
metadata
.onSuccess { println("partition: ${it.partition()}, offset: ${it.offset}") }
.onFailure { println("Failed to send: $it") }
}
All, and any feedback welcome!
What's Changed
- Update dependency org.testcontainers:kafka to v1.18.2 by @renovate in #128
- Update dependency org.testcontainers:kafka to v1.18.3 by @renovate in #129
- Update dependency org.jetbrains.kotlinx.kover to v0.7.1 by @renovate in #130
- Update all dependencies by @renovate in #131
- Update dependency org.jetbrains.dokka to v1.8.20 by @renovate in #133
- Update dependency org.jetbrains.dokka to v1.9.0 by @renovate in #134
- Update all dependencies to v4 (major) by @renovate in #136
- Update all dependencies by @renovate in #135
- Update all dependencies by @renovate in #138
- Update all dependencies to v0.12.0-rc.6 by @renovate in #139
- Update all dependencies by @renovate in #140
- Update all dependencies to v3.6.0 by @renovate in #141
- Update dependency gradle to v8.4 by @renovate in #142
- Clean-up by @nomisRev in #144
- Fix workflow by @nomisRev in #145
- Switch AutoOffsetReset definition by @nomisRev in #152
- Add publish plugin by @nomisRev in #153
- Add publisher settings by @nomisRev in #154
- Remove warnings from non-deprecated code by @nomisRev in #155
- Update dependency org.jetbrains.kotlinx.kover to v0.7.4 by @renovate in #157
- Publisher scope by @nomisRev in #156
- Fix PollLoop threading, add documentation, and split functionality in smaller methods by @nomisRev in #158
- Update dependency org.jetbrains.dokka to v1.9.10 by @renovate in #160
- Update all dependencies to v1.9.20 by @renovate in #161
- Update all dependencies to v5.8.0 by @renovate in #162
- Update dependency org.testcontainers:kafka to v1.19.2 by @renovate in #163
- Update dependency org.testcontainers:kafka to v1.19.3 by @renovate in #164
- Update all dependencies by @renovate in #165
- Update actions/setup-java action to v4 by @renovate in #166
- Update all dependencies by @renovate in #167
- Update actions/upload-artifact action to v4 by @renovate in #168
- Update all dependencies by @renovate in #169
- Update dependency com.diffplug.spotless to v6.25.0 by @renovate in #174
- Update dependency org.testcontainers:kafka to v1.19.4 by @renovate in #175
- Add power-assert, add utilities, clean-up by @nomisRev in #159
- ProduceFlow, deprecate Producer.kt, by @nomisRev in #177
- Update dependency gradle to v8.6 by @renovate in #176
- Flatten EventLoop into a single class, fix warnings. by @nomisRev in #178
- Explicit api, and flatten commit offset worker by @nomisRev in #179
- Update all dependencies to v2.0.12 by @renovate in #180
- Update dependency org.testcontainers:kafka to v1.19.5 by @renovate in #181
- Update all dependencies by @renovate in #182
- Update dependency org.testcontainers:kafka to v1.19.6 by @renovate in #183
- Update all dependencies by @renovate in #184
- Stress testing by @nomisRev in #187
Full Changelog: 0.3.1...0.4.0