-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ny consummer for import av virksomheter (fra Brreg)
Co-authored-by: Christian Klem <christian.klem@nav.no>
- Loading branch information
1 parent
50d5135
commit b1b5d93
Showing
9 changed files
with
404 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/kotlin/no/nav/lydia/sykefraversstatistikk/import/BrregAlleVirksomheterConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package no.nav.lydia.sykefraversstatistikk.import | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.serialization.json.Json | ||
import no.nav.lydia.Kafka | ||
import no.nav.lydia.exceptions.UgyldigAdresseException | ||
import no.nav.lydia.integrasjoner.brreg.BrregVirksomhetDto | ||
import no.nav.lydia.integrasjoner.brreg.tilVirksomhet | ||
import no.nav.lydia.virksomhet.VirksomhetRepository | ||
import no.nav.lydia.virksomhet.domene.VirksomhetStatus | ||
import org.apache.kafka.clients.consumer.KafkaConsumer | ||
import org.apache.kafka.common.errors.RetriableException | ||
import org.apache.kafka.common.errors.WakeupException | ||
import org.apache.kafka.common.serialization.StringDeserializer | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.time.Duration | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
object BrregAlleVirksomheterConsumer : CoroutineScope { | ||
private val logger: Logger = LoggerFactory.getLogger(this::class.java) | ||
lateinit var job: Job | ||
lateinit var kafka: Kafka | ||
|
||
lateinit var virksomhetRepository: VirksomhetRepository | ||
private lateinit var kafkaConsumer: KafkaConsumer<String, String> | ||
|
||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.IO + job | ||
|
||
init { | ||
Runtime.getRuntime().addShutdownHook(Thread(BrregAlleVirksomheterConsumer::cancel)) | ||
} | ||
|
||
fun create(kafka: Kafka, repository: VirksomhetRepository) { | ||
val topicNavn = kafka.brregAlleVirksomheterTopic | ||
val consumerGroupNavn = Kafka.brregConsumerGroupId | ||
|
||
logger.info("Creating kafka consumer job for $topicNavn i group $consumerGroupNavn") | ||
this.job = Job() | ||
this.kafka = kafka | ||
this.virksomhetRepository = repository | ||
kafkaConsumer = KafkaConsumer( | ||
BrregAlleVirksomheterConsumer.kafka.consumerProperties(consumerGroupId = Kafka.brregConsumerGroupId), | ||
StringDeserializer(), | ||
StringDeserializer() | ||
) | ||
logger.info("Created kafka consumer job for $topicNavn i group $consumerGroupNavn") | ||
} | ||
|
||
fun run() { | ||
val topicNavn = kafka.brregAlleVirksomheterTopic | ||
launch { | ||
kafkaConsumer.use { consumer -> | ||
try { | ||
consumer.subscribe(listOf(topicNavn)) | ||
logger.info("Kafka consumer subscribed to $topicNavn") | ||
while (job.isActive) { | ||
try { | ||
val records = consumer.poll(Duration.ofSeconds(1)) | ||
val antallMeldinger = records.count() | ||
if (antallMeldinger < 1) continue | ||
var antallIrrelevanteBedrifter = 0 | ||
logger.info("Fant $antallMeldinger nye meldinger for $topicNavn}") | ||
records.forEach { record -> | ||
val brregVirksomhet = Json.decodeFromString<BrregVirksomhetDto>(record.value()) | ||
|
||
try { | ||
val virksomhet = brregVirksomhet.tilVirksomhet( | ||
status = VirksomhetStatus.AKTIV, | ||
oppdateringsId = null | ||
) | ||
virksomhetRepository.insert(virksomhet) | ||
virksomhetRepository.insertNæringsundergrupper(virksomhet) | ||
} catch (e: UgyldigAdresseException) { | ||
antallIrrelevanteBedrifter += 1 | ||
} | ||
} | ||
logger.info("Lagret $antallMeldinger meldinger for $topicNavn") | ||
if (antallIrrelevanteBedrifter > 0) { | ||
logger.info("Fant $antallIrrelevanteBedrifter irrelevante bedrifter.") | ||
} | ||
consumer.commitSync() | ||
} catch (e: RetriableException) { | ||
logger.warn("Had a retriable exception for $topicNavn, retrying", e) | ||
} | ||
delay(kafka.consumerLoopDelay) | ||
} | ||
} catch (e: WakeupException) { | ||
logger.info("BrregAlleVirksomheterConsumer is shutting down...") | ||
} catch (e: Exception) { | ||
logger.error("Exception is shutting down kafka listner for $topicNavn", e) | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun cancel() = runBlocking { | ||
logger.info("Stopping kafka consumer job for ${kafka.brregAlleVirksomheterTopic}") | ||
kafkaConsumer.wakeup() | ||
job.cancelAndJoin() | ||
logger.info("Stopped kafka consumer job for ${kafka.brregAlleVirksomheterTopic}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.