All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.kafka.message.KafkaProducerHolder.kt Maven / Gradle / Ivy

The newest version!
package org.enodeframework.kafka.message

import com.google.common.collect.Maps
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.future.await
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.header.Header
import org.apache.kafka.common.header.internals.RecordHeader
import org.enodeframework.common.exception.IORuntimeException
import org.enodeframework.common.extensions.SysProperties
import org.enodeframework.queue.QueueMessage
import org.enodeframework.queue.SendMessageResult
import org.slf4j.LoggerFactory
import org.springframework.kafka.core.KafkaTemplate
import org.springframework.kafka.support.SendResult
import java.util.concurrent.CompletableFuture

class KafkaProducerHolder(
    private val kafkaTemplate: KafkaTemplate,
    private val dispatcher: CoroutineDispatcher,
) {

    private val logger = LoggerFactory.getLogger(KafkaProducerHolder::class.java)

    fun send(queueMessage: QueueMessage): CompletableFuture {
        return CoroutineScope(dispatcher).async {
            sendAsync(queueMessage).await()
        }.asCompletableFuture()
    }

    private fun sendAsync(queueMessage: QueueMessage): CompletableFuture {
        val message: ProducerRecord = this.covertToProducerRecord(queueMessage)
        return kafkaTemplate.send(message)
            .handle { result: SendResult, throwable: Throwable? ->
                if (throwable != null) {
                    logger.error(
                        "Async send message has exception, message: {}",
                        queueMessage,
                        throwable
                    )
                    throw IORuntimeException(throwable)
                }
                if (logger.isDebugEnabled) {
                    logger.debug(
                        "Async send message success, sendResult: {}, message: {}",
                        result,
                        queueMessage
                    )
                }
                val items: MutableMap = Maps.newHashMap()
                items["result"] = result
                SendMessageResult("", items)
            }
    }

    private fun covertToProducerRecord(queueMessage: QueueMessage): ProducerRecord {
        val record = ProducerRecord(queueMessage.topic, queueMessage.routeKey, queueMessage.bodyAsStr())
        val mTypeHeader: Header = RecordHeader(SysProperties.MESSAGE_TYPE_KEY, queueMessage.type.toByteArray())
        val tagHeader: Header = RecordHeader(SysProperties.MESSAGE_TAG_KEY, queueMessage.tag.toByteArray())
        record.headers().add(mTypeHeader).add(tagHeader)
        return record
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy