From 5ee0a306610c012c8b9fded405ee18e60f880bc5 Mon Sep 17 00:00:00 2001 From: geir-waagboe Date: Mon, 30 Oct 2023 16:52:46 +0100 Subject: [PATCH 1/2] IS-1748: Fix concurrency conflict --- .../no/nav/syfo/client/ArenaDialogNotat.kt | 2 +- .../kotlin/no/nav/syfo/client/SmgcpClient.kt | 4 ++-- .../no/nav/syfo/services/ApprecService.kt | 4 ++-- src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt | 19 ++++++++++--------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/no/nav/syfo/client/ArenaDialogNotat.kt b/src/main/kotlin/no/nav/syfo/client/ArenaDialogNotat.kt index 63e9281d..7f510cc9 100644 --- a/src/main/kotlin/no/nav/syfo/client/ArenaDialogNotat.kt +++ b/src/main/kotlin/no/nav/syfo/client/ArenaDialogNotat.kt @@ -138,7 +138,7 @@ fun sendArenaDialogNotat( loggingMeta: LoggingMeta ) { mqSender.sendArena( - payload = arenaDialogNotatMarshaller.toString(arenaDialogNotat) + payload = getArenaDialogNotatMarshaller().toString(arenaDialogNotat) ) logger.info("Message is sent to arena {}", fields(loggingMeta)) } diff --git a/src/main/kotlin/no/nav/syfo/client/SmgcpClient.kt b/src/main/kotlin/no/nav/syfo/client/SmgcpClient.kt index c5d174e2..08dcdc5e 100644 --- a/src/main/kotlin/no/nav/syfo/client/SmgcpClient.kt +++ b/src/main/kotlin/no/nav/syfo/client/SmgcpClient.kt @@ -9,7 +9,7 @@ import no.nav.helse.msgHead.XMLSender import no.nav.syfo.client.azuread.v2.AzureAdV2Client import no.nav.syfo.logger import no.nav.syfo.util.bearerHeader -import no.nav.syfo.util.senderMarshaller +import no.nav.syfo.util.getSenderMarshaller import java.io.ByteArrayOutputStream import java.io.IOException @@ -50,7 +50,7 @@ class SmgcpClient( private fun convertSenderToBase64(sender: XMLSender): ByteArray = ByteArrayOutputStream().use { - senderMarshaller.marshal(sender, it) + getSenderMarshaller().marshal(sender, it) it }.toByteArray() } diff --git a/src/main/kotlin/no/nav/syfo/services/ApprecService.kt b/src/main/kotlin/no/nav/syfo/services/ApprecService.kt index 96e71f2e..ea117f8e 100644 --- a/src/main/kotlin/no/nav/syfo/services/ApprecService.kt +++ b/src/main/kotlin/no/nav/syfo/services/ApprecService.kt @@ -9,8 +9,8 @@ import no.nav.syfo.apprec.ApprecStatus import no.nav.syfo.apprec.createApprec import no.nav.syfo.logger import no.nav.syfo.metrics.APPREC_COUNTER -import no.nav.syfo.util.apprecMarshaller import no.nav.syfo.util.get +import no.nav.syfo.util.getApprecMarshaller import no.nav.syfo.util.toString fun sendReceipt( @@ -27,7 +27,7 @@ fun sendReceipt( val apprec = createApprec(fellesformat, apprecStatus) apprec.get().error.addAll(apprecErrors) mqSender.sendReceipt( - payload = apprecMarshaller.toString(apprec) + payload = getApprecMarshaller().toString(apprec) ) APPREC_COUNTER.increment() } diff --git a/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt b/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt index 2ebd873f..1213e111 100644 --- a/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt +++ b/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt @@ -29,19 +29,14 @@ val fellesformatJaxBContext: JAXBContext = JAXBContext.newInstance( XMLAppRec::class.java, ) -val senderMarshaller: Marshaller = JAXBContext.newInstance(XMLSender::class.java).createMarshaller() +fun getSenderMarshaller(): Marshaller = JAXBContext.newInstance(XMLSender::class.java).createMarshaller() .apply { setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1") } val apprecJaxBContext: JAXBContext = JAXBContext.newInstance(XMLEIFellesformat::class.java, XMLAppRec::class.java) -val apprecMarshaller: Marshaller = apprecJaxBContext.createMarshaller() +fun getApprecMarshaller(): Marshaller = apprecJaxBContext.createMarshaller() val arenaDialogNotatJaxBContext: JAXBContext = JAXBContext.newInstance(ArenaDialogNotat::class.java) -val arenaDialogNotatMarshaller: Marshaller = arenaDialogNotatJaxBContext.createMarshaller() - -private val fellesformatUnmarshaller: Unmarshaller = fellesformatJaxBContext.createUnmarshaller().apply { - setAdapter(LocalDateTimeXmlAdapter::class.java, XMLDateTimeAdapter()) - setAdapter(LocalDateXmlAdapter::class.java, XMLDateAdapter()) -} +fun getArenaDialogNotatMarshaller(): Marshaller = arenaDialogNotatJaxBContext.createMarshaller() fun Marshaller.toString(input: Any): String = StringWriter().use { marshal(input, it) @@ -59,5 +54,11 @@ fun safeUnmarshal(inputMessageText: String): XMLEIFellesformat { spf.newSAXParser().xmlReader, InputSource(StringReader(inputMessageText)), ) - return fellesformatUnmarshaller.unmarshal(xmlSource) as XMLEIFellesformat + return getFellesformatUnmarshaller().unmarshal(xmlSource) as XMLEIFellesformat +} + +// Unmarshaller is not thread safe - do not "optimize" +private fun getFellesformatUnmarshaller(): Unmarshaller = fellesformatJaxBContext.createUnmarshaller().apply { + setAdapter(LocalDateTimeXmlAdapter::class.java, XMLDateTimeAdapter()) + setAdapter(LocalDateXmlAdapter::class.java, XMLDateAdapter()) } From 8e7abd6a6d7f1a43f2a5253629e2534809321f61 Mon Sep 17 00:00:00 2001 From: geir-waagboe Date: Tue, 31 Oct 2023 09:36:53 +0100 Subject: [PATCH 2/2] IS-1748: Fix concurrency conflict --- src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt b/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt index 1213e111..52d53591 100644 --- a/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt +++ b/src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt @@ -29,7 +29,8 @@ val fellesformatJaxBContext: JAXBContext = JAXBContext.newInstance( XMLAppRec::class.java, ) -fun getSenderMarshaller(): Marshaller = JAXBContext.newInstance(XMLSender::class.java).createMarshaller() +val xmlSenderJaxBContext = JAXBContext.newInstance(XMLSender::class.java) +fun getSenderMarshaller(): Marshaller = xmlSenderJaxBContext.createMarshaller() .apply { setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1") } val apprecJaxBContext: JAXBContext = JAXBContext.newInstance(XMLEIFellesformat::class.java, XMLAppRec::class.java)