Skip to content

Commit

Permalink
vanilla kafka
Browse files Browse the repository at this point in the history
  • Loading branch information
rtc11 committed Nov 16, 2023
1 parent 85db76a commit 5468a4c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions kafka/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
implementation("org.apache.kafka:kafka-clients:3.5.1")
testImplementation(kotlin("test"))
}
17 changes: 13 additions & 4 deletions kafka/main/no/nav/aap/kafka/KafkaFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package no.nav.aap.kafka

import org.apache.kafka.clients.CommonClientConfigs
import org.apache.kafka.clients.producer.KafkaProducer
import org.apache.kafka.clients.producer.Producer
import org.apache.kafka.clients.producer.ProducerConfig
import org.apache.kafka.common.config.SslConfigs
import org.apache.kafka.common.serialization.Serdes
import org.apache.kafka.common.serialization.Serdes.StringSerde
import org.apache.kafka.common.serialization.Serializer
import java.util.*

class KafkaConfig(
data class KafkaConfig(
val brokers: String,
val truststorePath: String,
val keystorePath: String,
Expand All @@ -16,7 +18,11 @@ class KafkaConfig(

class KafkaFactory private constructor() {
companion object {
fun createProducer(clientId: String, config: KafkaConfig): KafkaProducer<String, String> {
fun <T : Any> createProducer(
clientId: String,
config: KafkaConfig,
serializer: Serializer<T>,
): Producer<String, T> {
fun properties(): Properties = Properties().apply {
this[CommonClientConfigs.CLIENT_ID_CONFIG] = clientId
this[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = config.brokers
Expand All @@ -33,7 +39,10 @@ class KafkaFactory private constructor() {
this[SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG] = ""
}

return KafkaProducer(properties(), Serdes.StringSerde().serializer(), Serdes.StringSerde().serializer())
return KafkaProducer(properties(), StringSerde().serializer(), serializer)
}

fun createProducer(clientId: String, config: KafkaConfig): Producer<String, String> =
createProducer(clientId, config, StringSerde().serializer())
}
}
11 changes: 11 additions & 0 deletions kafka/test/no/nav/aap/kafka/KafkaFactoryTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.aap.kafka

import org.junit.jupiter.api.Test

class KafkaFactoryTest {

@Test
fun config() {

}
}

0 comments on commit 5468a4c

Please sign in to comment.