|
| 1 | +package no.nav.syfo.personstatus.infrastructure.kafka.behandlendeenhet |
| 2 | + |
| 3 | +import no.nav.syfo.ApplicationState |
| 4 | +import no.nav.syfo.personstatus.application.PersonBehandlendeEnhetService |
| 5 | +import no.nav.syfo.personstatus.domain.PersonIdent |
| 6 | +import no.nav.syfo.personstatus.infrastructure.kafka.KafkaConsumerService |
| 7 | +import no.nav.syfo.personstatus.infrastructure.kafka.KafkaEnvironment |
| 8 | +import no.nav.syfo.personstatus.infrastructure.kafka.kafkaAivenConsumerConfig |
| 9 | +import no.nav.syfo.personstatus.infrastructure.kafka.launchKafkaTask |
| 10 | +import no.nav.syfo.util.configuredJacksonMapper |
| 11 | +import org.apache.kafka.clients.consumer.ConsumerConfig |
| 12 | +import org.apache.kafka.clients.consumer.ConsumerRecords |
| 13 | +import org.apache.kafka.clients.consumer.KafkaConsumer |
| 14 | +import org.apache.kafka.common.serialization.Deserializer |
| 15 | +import org.slf4j.LoggerFactory |
| 16 | +import java.time.Duration |
| 17 | +import java.time.OffsetDateTime |
| 18 | +import java.util.Properties |
| 19 | + |
| 20 | +class BehandlendeEnhetConsumer( |
| 21 | + private val personBehandlendeEnhetService: PersonBehandlendeEnhetService, |
| 22 | +) : KafkaConsumerService<BehandlendeEnhetUpdateRecord> { |
| 23 | + |
| 24 | + override val pollDurationInMillis: Long = 1000 |
| 25 | + |
| 26 | + override suspend fun pollAndProcessRecords(consumer: KafkaConsumer<String, BehandlendeEnhetUpdateRecord>) { |
| 27 | + val records = consumer.poll(Duration.ofMillis(pollDurationInMillis)) |
| 28 | + if (records.count() > 0) { |
| 29 | + log.info("BehandlendeEnhetConsumer trace: Received ${records.count()} records") |
| 30 | + processRecords(records = records) |
| 31 | + consumer.commitSync() |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private suspend fun processRecords(records: ConsumerRecords<String, BehandlendeEnhetUpdateRecord>) = |
| 36 | + records.requireNoNulls().map { record -> |
| 37 | + personBehandlendeEnhetService.updateBehandlendeEnhet( |
| 38 | + PersonIdent(record.value().personident) |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + fun start(applicationState: ApplicationState, kafkaEnvironment: KafkaEnvironment) { |
| 43 | + val consumerProperties = Properties().apply { |
| 44 | + putAll(kafkaAivenConsumerConfig(kafkaEnvironment = kafkaEnvironment)) |
| 45 | + this[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = BehandlendeEnhetUpdateRecordDeserializer::class.java.canonicalName |
| 46 | + } |
| 47 | + launchKafkaTask( |
| 48 | + applicationState = applicationState, |
| 49 | + kafkaConsumerService = this, |
| 50 | + consumerProperties = consumerProperties, |
| 51 | + topic = TOPIC, |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + companion object { |
| 56 | + private val log = LoggerFactory.getLogger(this::class.java) |
| 57 | + private const val TOPIC = "teamsykefravr.behandlendeenhet" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +data class BehandlendeEnhetUpdateRecord( |
| 62 | + val personident: String, |
| 63 | + val updatedAt: OffsetDateTime, |
| 64 | +) |
| 65 | + |
| 66 | +class BehandlendeEnhetUpdateRecordDeserializer : Deserializer<BehandlendeEnhetUpdateRecord> { |
| 67 | + private val mapper = configuredJacksonMapper() |
| 68 | + override fun deserialize(topic: String, data: ByteArray): BehandlendeEnhetUpdateRecord = |
| 69 | + mapper.readValue(data, BehandlendeEnhetUpdateRecord::class.java) |
| 70 | +} |
0 commit comments