diff options
| author | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-15 23:48:44 +0200 |
|---|---|---|
| committer | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-15 23:48:44 +0200 |
| commit | 0731bd58889df127ef87aba2590d505d79e6646f (patch) | |
| tree | 128aceeaf60ac5c098297f7cfda9fa47f974fc84 /opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt | |
| parent | f1ecbf0ce40d43685d8a6aeba0fe4cdebbd4536f (diff) | |
feat: migrated the past project to the sunfish repo
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt')
| -rw-r--r-- | opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt new file mode 100644 index 00000000..81eb6752 --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt @@ -0,0 +1,38 @@ +package org.opendc.common.utils + +import com.fasterxml.jackson.dataformat.toml.TomlMapper +import org.apache.kafka.clients.producer.KafkaProducer +import org.apache.kafka.clients.producer.ProducerRecord +import org.opendc.common.ProtobufMetrics +import java.util.* +/** + * Represents the Kafka interface. + * @constructor `topic` the Kafka topic + * @author Mateusz Kwiatkowski + * @see <a href=https://kafka.apache.org/23/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html> + * https://kafka.apache.org/23/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html</a> + * @see <a href=https://kafka.apache.org/23/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html> + * https://kafka.apache.org/23/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html</a> + */ + +@Suppress("DEPRECATION") +public class Kafka(private val topic: String) { + private var properties : Properties + + init { + check(!topic.contains("-")) + properties = TomlMapper().readerFor(Properties().javaClass) + .readValue(Kafka::class.java.getResource("/producer.toml")) + } + + public fun getSend() : (ProtobufMetrics.ProtoExport) -> Unit { + val producer = KafkaProducer<String, ProtobufMetrics.ProtoExport>(properties) + return fun (value : ProtobufMetrics.ProtoExport) { + try { + producer.send(ProducerRecord(this.topic, value)) + } catch (e: Exception) { + println("${e.message}") + } + } + } +}
\ No newline at end of file |
