-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Archive older versions * Fix link * Improve text * Remove legacy doc * Improve * Fix links * Add badge * Improve link * Fix links * Indentation * Add link * Improve format * Add 💙 * Rename kcat
- Loading branch information
1 parent
07a40b1
commit efadcc4
Showing
12 changed files
with
329 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/rogervinas/stream/domain/MyEventConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.rogervinas.stream.domain | ||
|
||
interface MyEventConsumer { | ||
fun consume(event: MyEvent) | ||
} | ||
fun consume(event: MyEvent) | ||
} |
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/rogervinas/stream/domain/MyEventProducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.rogervinas.stream.domain | ||
|
||
interface MyEventProducer { | ||
fun produce(event: MyEvent) | ||
} | ||
fun produce(event: MyEvent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 42 additions & 38 deletions
80
src/test/kotlin/com/rogervinas/stream/MyKafkaConsumerHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,49 @@ | ||
package com.rogervinas.stream | ||
|
||
import org.apache.kafka.clients.consumer.* | ||
import org.apache.kafka.clients.consumer.Consumer | ||
import org.apache.kafka.clients.consumer.ConsumerConfig | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.apache.kafka.clients.consumer.KafkaConsumer | ||
import org.apache.kafka.clients.consumer.OffsetResetStrategy | ||
import org.apache.kafka.common.serialization.StringDeserializer | ||
import java.time.Duration | ||
import java.util.* | ||
import kotlin.collections.ArrayList | ||
import java.util.Properties | ||
import java.util.UUID | ||
|
||
class MyKafkaConsumerHelper(bootstrapServers: String, topic: String) { | ||
|
||
private val consumer: Consumer<String, String> | ||
|
||
companion object { | ||
private const val MILLIS_POLL: Long = 250 | ||
} | ||
|
||
init { | ||
consumer = KafkaConsumer(consumerConfig(bootstrapServers)) | ||
consumer.subscribe(arrayListOf(topic)) | ||
} | ||
|
||
fun consumeAll(): List<ConsumerRecord<String, String>> { | ||
return consumeAtLeast(1, Duration.ofSeconds(1)) | ||
} | ||
|
||
fun consumeAtLeast(numberOfRecords: Int, timeout: Duration): List<ConsumerRecord<String, String>> { | ||
val records: MutableList<ConsumerRecord<String, String>> = ArrayList() | ||
var millisLeft = timeout.toMillis() | ||
do { | ||
consumer.poll(Duration.ofMillis(MILLIS_POLL)).iterator().forEachRemaining { records.add(it) } | ||
millisLeft -= MILLIS_POLL | ||
} while (millisLeft > 0 && records.size < numberOfRecords) | ||
return records | ||
} | ||
|
||
private fun consumerConfig(bootstrapServers: String): Properties { | ||
val config = Properties() | ||
config.setProperty(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString()) | ||
config.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers) | ||
config.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.java.name) | ||
config.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.java.name) | ||
config.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.EARLIEST.name.toLowerCase()) | ||
return config | ||
} | ||
} | ||
private val consumer: Consumer<String, String> | ||
|
||
companion object { | ||
private const val MILLIS_POLL: Long = 250 | ||
} | ||
|
||
init { | ||
consumer = KafkaConsumer(consumerConfig(bootstrapServers)) | ||
consumer.subscribe(arrayListOf(topic)) | ||
} | ||
|
||
fun consumeAll(): List<ConsumerRecord<String, String>> { | ||
return consumeAtLeast(1, Duration.ofSeconds(1)) | ||
} | ||
|
||
fun consumeAtLeast(numberOfRecords: Int, timeout: Duration): List<ConsumerRecord<String, String>> { | ||
val records: MutableList<ConsumerRecord<String, String>> = ArrayList() | ||
var millisLeft = timeout.toMillis() | ||
do { | ||
consumer.poll(Duration.ofMillis(MILLIS_POLL)).iterator().forEachRemaining { records.add(it) } | ||
millisLeft -= MILLIS_POLL | ||
} while (millisLeft > 0 && records.size < numberOfRecords) | ||
return records | ||
} | ||
|
||
private fun consumerConfig(bootstrapServers: String): Properties { | ||
val config = Properties() | ||
config.setProperty(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString()) | ||
config.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers) | ||
config.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.java.name) | ||
config.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.java.name) | ||
config.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.EARLIEST.name.toLowerCase()) | ||
return config | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters