33package io.github.nomisRev.kafka
44
55import io.github.nomisRev.kafka.internal.chunked
6+ import io.github.nomisRev.kafka.receiver.ReceiverSettings
67import kotlinx.coroutines.CancellationException
78import kotlinx.coroutines.CoroutineDispatcher
89import java.time.Duration
@@ -73,6 +74,13 @@ import kotlin.coroutines.suspendCoroutine
7374import kotlin.time.Duration.Companion.milliseconds
7475import kotlin.time.toJavaDuration
7576
77+ @Deprecated(
78+ " Use KafkaReceiver instead. This function will be removed in 0.4.0" ,
79+ ReplaceWith (
80+ " KafkaReceiver(settings.toReceiverSettings())" ,
81+ " io.github.nomisRev.kafka.receiver.KafkaReceiver"
82+ )
83+ )
7684public fun <K , V > KafkaConsumer (settings : ConsumerSettings <K , V >): KafkaConsumer <K , V > =
7785 KafkaConsumer (settings.properties(), settings.keyDeserializer, settings.valueDeserializer)
7886
@@ -102,6 +110,13 @@ public fun <K, V> KafkaConsumer(settings: ConsumerSettings<K, V>): KafkaConsumer
102110 * ```
103111 * <!--- KNIT example-consumer-01.kt -->
104112 */
113+ @Deprecated(
114+ " Use KafkaReceiver#receive instead. This function will be removed in 0.4.0" ,
115+ ReplaceWith (
116+ " KafkaReceiver(settings.toReceiverSettings()).receive()" ,
117+ " io.github.nomisRev.kafka.receiver.KafkaReceiver"
118+ )
119+ )
105120public fun <K , V > kafkaConsumer (settings : ConsumerSettings <K , V >): Flow <KafkaConsumer <K , V >> =
106121 flow {
107122 KafkaConsumer (settings).use {
@@ -115,6 +130,7 @@ public fun <K, V> kafkaConsumer(settings: ConsumerSettings<K, V>): Flow<KafkaCon
115130@OptIn(FlowPreview ::class )
116131@ExperimentalCoroutinesApi
117132@JvmName(" commitBatchesWithin" )
133+ @Deprecated(" Use KafkaReceiver instead. It comes with strong guarantees about commits" )
118134public fun <K , V > Flow <ConsumerRecords <K , V >>.commitBatchWithin (
119135 settings : ConsumerSettings <K , V >,
120136 count : Int ,
@@ -129,6 +145,7 @@ public fun <K, V> Flow<ConsumerRecords<K, V>>.commitBatchWithin(
129145
130146@OptIn(FlowPreview ::class )
131147@ExperimentalCoroutinesApi
148+ @Deprecated(" Use KafkaReceiver instead. It comes with strong guarantees about commits" )
132149public fun <K , V > Flow <ConsumerRecord <K , V >>.commitBatchWithin (
133150 settings : ConsumerSettings <K , V >,
134151 count : Int ,
@@ -143,6 +160,10 @@ public fun <K, V> Flow<ConsumerRecord<K, V>>.commitBatchWithin(
143160 }
144161}
145162
163+ @Deprecated(
164+ " Use KafkaReceiver instead. It comes with strong guarantees about commits." +
165+ " You can only commit while polling, which is done automatically for you with KafkaReceiver"
166+ )
146167public suspend fun <K , V > KafkaConsumer <K , V >.commitAwait (
147168 offsets : Map <TopicPartition , OffsetAndMetadata >,
148169): Map <TopicPartition , OffsetAndMetadata > =
@@ -158,15 +179,16 @@ public operator fun <K, V> ConsumerRecord<K, V>.component2(): V = value()
158179
159180public fun <K , V > Iterable <ConsumerRecord <K , V >>.offsets (
160181 metadata : ((record: ConsumerRecord <K , V >) -> String )? = null,
161- ): Map <TopicPartition , OffsetAndMetadata > = mutableMapOf<TopicPartition , OffsetAndMetadata >().apply {
162- this @offsets.forEach { record ->
163- val key = TopicPartition (record.topic(), record.partition())
164- val value = metadata?.let {
165- OffsetAndMetadata (record.offset() + 1 , record.leaderEpoch(), metadata(record))
166- } ? : OffsetAndMetadata (record.offset() + 1 )
167- put(key, value)
182+ ): Map <TopicPartition , OffsetAndMetadata > =
183+ mutableMapOf<TopicPartition , OffsetAndMetadata >().apply {
184+ this @offsets.forEach { record ->
185+ val key = TopicPartition (record.topic(), record.partition())
186+ val value = metadata?.let {
187+ OffsetAndMetadata (record.offset() + 1 , record.leaderEpoch(), metadata(record))
188+ } ? : OffsetAndMetadata (record.offset() + 1 )
189+ put(key, value)
190+ }
168191 }
169- }
170192
171193public fun <K , V > ConsumerRecord <K , V >.offsets (
172194 metadata : ((record: ConsumerRecord <K , V >) -> String )? = null,
@@ -187,6 +209,13 @@ public fun <K, V> List<ConsumerRecords<K, V>>.offsets(
187209 }
188210}
189211
212+ @Deprecated(
213+ " Use KafkaReceiver#receive instead. This function will be removed in 0.4.0" ,
214+ ReplaceWith (
215+ " KafkaReceiver(settings.toReceiverSettings()).receive()" ,
216+ " io.github.nomisRev.kafka.receiver.KafkaReceiver"
217+ )
218+ )
190219@OptIn(FlowPreview ::class )
191220public fun <K , V > Flow <KafkaConsumer <K , V >>.subscribeTo (
192221 name : String ,
@@ -197,6 +226,13 @@ public fun <K, V> Flow<KafkaConsumer<K, V>>.subscribeTo(
197226 consumer.subscribeTo(name, dispatcher, listener, timeout)
198227}
199228
229+ @Deprecated(
230+ " Use KafkaReceiver#receive instead. This function will be removed in 0.4.0" ,
231+ ReplaceWith (
232+ " KafkaReceiver(settings.toReceiverSettings()).receive()" ,
233+ " io.github.nomisRev.kafka.receiver.KafkaReceiver"
234+ )
235+ )
200236/* * Subscribes to the [KafkaConsumer] and polls for events in an interruptible way. */
201237public fun <K , V > KafkaConsumer <K , V >.subscribeTo (
202238 name : String ,
@@ -220,13 +256,22 @@ public fun <K, V> KafkaConsumer<K, V>.subscribeTo(
220256 }
221257}
222258
259+ @Deprecated(
260+ " Use io.github.nomisRev.kafka.receiver.AutoOffsetReset instead" ,
261+ ReplaceWith (
262+ " this" ,
263+ " io.github.nomisRev.kafka.receiver.AutoOffsetReset"
264+ )
265+ )
223266public enum class AutoOffsetReset (public val value : String ) {
224267 Earliest (" earliest" ), Latest (" latest" ), None (" none" )
225268}
226269
227- // TODO Compare with reactor-kafka
228- // TODO should be easier to use `null`/`Nothing` Key
229270/* * Default values taken from [org.apache.kafka.clients.consumer.ConsumerConfig] */
271+ @Deprecated(
272+ " Use ReceiverSettings with KafkaReceiver instead." ,
273+ ReplaceWith (" toReceiverSettings()" )
274+ )
230275public data class ConsumerSettings <K , V >(
231276 // BOOTSTRAP_SERVERS_CONFIG
232277 val bootstrapServers : String ,
@@ -299,6 +344,15 @@ public data class ConsumerSettings<K, V>(
299344 // Optional parameter that allows for setting properties not defined here
300345 private val properties : Properties ? = null ,
301346) {
347+ public fun toReceiverSettings (): ReceiverSettings <K , V > =
348+ ReceiverSettings (
349+ bootstrapServers,
350+ keyDeserializer,
351+ valueDeserializer,
352+ groupId,
353+ properties = properties()
354+ )
355+
302356 public fun properties (): Properties = Properties ().apply {
303357 properties?.let { putAll(it) }
304358 put(BOOTSTRAP_SERVERS_CONFIG , bootstrapServers)
0 commit comments