|
| 1 | +package io.github.nomisRev.kafka |
| 2 | + |
| 3 | +import kotlinx.coroutines.Dispatchers |
| 4 | +import kotlinx.coroutines.coroutineScope |
| 5 | +import kotlinx.coroutines.flow.Flow |
| 6 | +import kotlinx.coroutines.flow.asFlow |
| 7 | +import kotlinx.coroutines.flow.collect |
| 8 | +import kotlinx.coroutines.flow.map |
| 9 | +import kotlinx.coroutines.flow.take |
| 10 | +import kotlinx.coroutines.launch |
| 11 | +import kotlinx.coroutines.runBlocking |
| 12 | +import org.apache.kafka.clients.admin.NewTopic |
| 13 | +import org.apache.kafka.clients.producer.ProducerRecord |
| 14 | +import org.apache.kafka.common.serialization.IntegerDeserializer |
| 15 | +import org.apache.kafka.common.serialization.IntegerSerializer |
| 16 | +import org.apache.kafka.common.serialization.StringDeserializer |
| 17 | +import org.apache.kafka.common.serialization.StringSerializer |
| 18 | +import java.util.UUID |
| 19 | +import kotlin.time.Duration.Companion.milliseconds |
| 20 | + |
| 21 | +@JvmInline |
| 22 | +value class Key(val index: Int) |
| 23 | + |
| 24 | +@JvmInline |
| 25 | +value class Message(val content: String) |
| 26 | + |
| 27 | +fun main(): Unit = runBlocking(Dispatchers.Default) { |
| 28 | + val topicName = "test-topic" |
| 29 | + val msgCount = 10 |
| 30 | + val kafka = Kafka.container |
| 31 | + |
| 32 | + Admin(AdminSettings(kafka.bootstrapServers)).use { client -> |
| 33 | + client.createTopic(NewTopic(topicName, 1, 1)) |
| 34 | + } |
| 35 | + |
| 36 | + coroutineScope { // Run produces and consumer in a single scope |
| 37 | + launch(Dispatchers.IO) { // Send 20 messages, and then close the producer |
| 38 | + val settings: ProducerSettings<Key, Message> = ProducerSettings( |
| 39 | + kafka.bootstrapServers, |
| 40 | + IntegerSerializer().imap { key: Key -> key.index }, |
| 41 | + StringSerializer().imap { msg: Message -> msg.content }, |
| 42 | + Acks.All |
| 43 | + ) |
| 44 | + (1..msgCount) |
| 45 | + .map { index -> ProducerRecord(topicName, Key(index), Message("msg: $index")) } |
| 46 | + .asFlow() |
| 47 | + .produce(settings) |
| 48 | + .collect(::println) |
| 49 | + } |
| 50 | + |
| 51 | + launch(Dispatchers.IO) { // Consume 20 messages as a stream, and then close the consumer |
| 52 | + val settings: ConsumerSettings<Key, Message> = ConsumerSettings( |
| 53 | + kafka.bootstrapServers, |
| 54 | + IntegerDeserializer().map(::Key), |
| 55 | + StringDeserializer().map(::Message), |
| 56 | + groupId = UUID.randomUUID().toString(), |
| 57 | + autoOffsetReset = AutoOffsetReset.Earliest, |
| 58 | + enableAutoCommit = false |
| 59 | + ) |
| 60 | + |
| 61 | + KafkaConsumer(settings).asFlow() |
| 62 | + .subscribeTo(topicName) |
| 63 | + .tap { (key, value) -> println("$key -> $value") } |
| 64 | + .commitBatchWithin(settings, 3, 10.milliseconds) |
| 65 | + .take(4) |
| 66 | + .collect() |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fun <A> Flow<A>.tap(also: suspend (A) -> Unit): Flow<A> = |
| 72 | + map { it.also { also(it) } } |
0 commit comments