diff --git a/kafka/README.md b/kafka/README.md new file mode 100644 index 0000000..5df58e7 --- /dev/null +++ b/kafka/README.md @@ -0,0 +1,2 @@ +## Kafka +Library for setting up kafka producers and consumers diff --git a/kafka/build.gradle.kts b/kafka/build.gradle.kts new file mode 100644 index 0000000..c3280c6 --- /dev/null +++ b/kafka/build.gradle.kts @@ -0,0 +1,3 @@ +dependencies { + implementation("org.apache.kafka:kafka-clients:3.5.1") +} diff --git a/kafka/main/no/nav/aap/kafka/KafkaFactory.kt b/kafka/main/no/nav/aap/kafka/KafkaFactory.kt new file mode 100644 index 0000000..65a2bad --- /dev/null +++ b/kafka/main/no/nav/aap/kafka/KafkaFactory.kt @@ -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 { + 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()) + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 2467a9a..a5be23b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,6 +2,7 @@ rootProject.name = "aap-libs" include( "cache", + "kafka", "kafka-2", "kafka-interfaces", "kafka-avroserde",