-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(recipes): kotlin ktor with kafka implemented
- Loading branch information
Showing
24 changed files
with
382 additions
and
37 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
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
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
13 changes: 13 additions & 0 deletions
13
.../kotlin/com/trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/ConsumerEngine.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
class ConsumerEngine( | ||
private val supervisors: List<ConsumerSupervisor<*, *>> | ||
) { | ||
fun start() { | ||
supervisors.forEach { it.start() } | ||
} | ||
|
||
fun stop() { | ||
supervisors.forEach { it.cancel() } | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...lin/com/trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/ConsumerSupervisor.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
import io.github.nomisRev.kafka.receiver.KafkaReceiver | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.Channel | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import java.time.Duration | ||
|
||
abstract class ConsumerSupervisor<K, V>( | ||
private val kafkaReceiver: KafkaReceiver<K, V>, | ||
private val maxConcurrency: Int | ||
) { | ||
private val logger = KotlinLogging.logger("ConsumerSupervisor[${javaClass.simpleName}]") | ||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
private val recordChannel = Channel<ConsumerRecord<K, V>>(maxConcurrency) | ||
|
||
abstract val topics: List<String> | ||
|
||
fun start() { | ||
scope.launch { | ||
logger.info { "Receiving records from topics: $topics" } | ||
subscribe() | ||
} | ||
logger.info { "Consuming records with concurrency: $maxConcurrency" } | ||
} | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
private suspend fun subscribe() { | ||
kafkaReceiver.withConsumer { consumer -> | ||
consumer.subscribe(topics) | ||
while (scope.isActive) { | ||
val records = consumer.poll(Duration.ofMillis(100)) | ||
records.forEach { record -> | ||
logger.debug { "Received record: $record" } | ||
try { | ||
consume(record) | ||
consumer.commitAsync() | ||
} catch (e: Exception) { | ||
handleError(e, record) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
abstract suspend fun consume(record: ConsumerRecord<K, V>) | ||
|
||
protected open fun handleError(e: Exception, record: ConsumerRecord<K, V>) { | ||
logger.error(e) { "Error while processing record: $record" } | ||
} | ||
|
||
fun cancel() { | ||
logger.info { "Cancelling consumer supervisor" } | ||
scope.cancel() | ||
} | ||
|
||
/** | ||
* Offers the record to the channel. If the channel is full, it suspends until a space becomes available. | ||
*/ | ||
@Suppress("TooGenericExceptionCaught") | ||
private fun consume() = repeat(maxConcurrency) { | ||
scope.launch { | ||
for (record in recordChannel) { | ||
try { | ||
consume(record) | ||
} catch (e: Exception) { | ||
handleError(e, record) | ||
} | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/KafkaDomainEventPublisher.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.trendyol.stove.examples.domain.ddd.* | ||
import io.github.nomisRev.kafka.publisher.KafkaPublisher | ||
import kotlinx.coroutines.runBlocking | ||
import org.apache.kafka.clients.producer.ProducerRecord | ||
import org.slf4j.* | ||
|
||
class KafkaDomainEventPublisher( | ||
private val publisher: KafkaPublisher<String, Any>, | ||
private val topicResolver: TopicResolver, | ||
private val objectMapper: ObjectMapper | ||
) : EventPublisher { | ||
private val logger: Logger = LoggerFactory.getLogger(KafkaDomainEventPublisher::class.java) | ||
|
||
override fun <TId> publishFor(aggregateRoot: AggregateRoot<TId>) = runBlocking { | ||
mapEventsToProducerRecords(aggregateRoot) | ||
.forEach { record -> publisher.publishScope { offer(record) } } | ||
} | ||
|
||
private fun <TId> mapEventsToProducerRecords( | ||
aggregateRoot: AggregateRoot<TId> | ||
): List<ProducerRecord<String, Any>> = aggregateRoot.domainEvents() | ||
.map { event -> | ||
val topic: Topic = topicResolver(aggregateRoot.aggregateName) | ||
logger.info("Publishing event {} to topic {}", event, topic.name) | ||
ProducerRecord<String, Any>( | ||
topic.name, | ||
aggregateRoot.idAsString, | ||
objectMapper.writeValueAsString(event) | ||
) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../src/main/kotlin/com/trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/SerDe.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.serialization.JacksonConfiguration | ||
import org.apache.kafka.common.serialization.* | ||
|
||
private val kafkaObjectMapperRef = JacksonConfiguration.default | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
class StoveKafkaValueDeserializer<T : Any> : Deserializer<T> { | ||
override fun deserialize( | ||
topic: String, | ||
data: ByteArray | ||
): T = kafkaObjectMapperRef.readValue<Any>(data) as T | ||
} | ||
|
||
class StoveKafkaValueSerializer<T : Any> : Serializer<T> { | ||
override fun serialize( | ||
topic: String, | ||
data: T | ||
): ByteArray = kafkaObjectMapperRef.writeValueAsBytes(data) | ||
} |
9 changes: 9 additions & 0 deletions
9
.../src/main/kotlin/com/trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/Topic.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
data class Topic( | ||
val name: String, | ||
val retry: String, | ||
val deadLetter: String, | ||
val maxRetry: Int = 1, | ||
val concurrency: Int = 1 | ||
) |
7 changes: 7 additions & 0 deletions
7
...n/kotlin/com/trendyol/stove/examples/kotlin/ktor/infra/boilerplate/kafka/TopicResolver.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.trendyol.stove.examples.kotlin.ktor.infra.boilerplate.kafka | ||
|
||
import com.trendyol.stove.examples.kotlin.ktor.application.* | ||
|
||
class TopicResolver(private val kafkaConfiguration: KafkaConfiguration) { | ||
operator fun invoke(aggregateName: String): Topic = kafkaConfiguration.topics.getValue(aggregateName) | ||
} |
Oops, something went wrong.