Skip to content

Commit

Permalink
Vanilla kafka producer
Browse files Browse the repository at this point in the history
  • Loading branch information
rtc11 committed Nov 7, 2023
1 parent dbf10eb commit cdf136b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kafka/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Kafka
Library for setting up kafka producers and consumers
3 changes: 3 additions & 0 deletions kafka/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
implementation("org.apache.kafka:kafka-clients:3.5.1")
}
39 changes: 39 additions & 0 deletions kafka/main/no/nav/aap/kafka/KafkaFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package no.nav.aap.kafka

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

class KafkaConfig(
val brokers: String,
val truststorePath: String,
val keystorePath: String,
val credstorePsw: String,
)

class KafkaFactory private constructor() {
companion object {
fun createProducer(clientId: String, config: KafkaConfig): KafkaProducer<String, String> {
fun properties(): Properties = Properties().apply {
this[CommonClientConfigs.CLIENT_ID_CONFIG] = clientId
this[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = config.brokers
this[ProducerConfig.ACKS_CONFIG] = "all"
this[ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION] = "5"
this[CommonClientConfigs.SECURITY_PROTOCOL_CONFIG] = "SSL"
this[SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG] = "JKS"
this[SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG] = config.truststorePath
this[SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG] = config.credstorePsw
this[SslConfigs.SSL_KEYSTORE_TYPE_CONFIG] = "PKCS12"
this[SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG] = config.keystorePath
this[SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG] = config.credstorePsw
this[SslConfigs.SSL_KEY_PASSWORD_CONFIG] = config.credstorePsw
this[SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG] = ""
}

return KafkaProducer(properties(), Serdes.StringSerde().serializer(), Serdes.StringSerde().serializer())
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rootProject.name = "aap-libs"

include(
"cache",
"kafka",
"kafka-2",
"kafka-interfaces",
"kafka-avroserde",
Expand Down

0 comments on commit cdf136b

Please sign in to comment.