Skip to content

Commit

Permalink
IS-1748: Fix concurrency conflict (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
geir-waagboe authored Oct 31, 2023
1 parent afc8e05 commit a25c3f7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/syfo/client/ArenaDialogNotat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nav/syfo/client/SmgcpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -50,7 +50,7 @@ class SmgcpClient(

private fun convertSenderToBase64(sender: XMLSender): ByteArray =
ByteArrayOutputStream().use {
senderMarshaller.marshal(sender, it)
getSenderMarshaller().marshal(sender, it)
it
}.toByteArray()
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nav/syfo/services/ApprecService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -27,7 +27,7 @@ fun sendReceipt(
val apprec = createApprec(fellesformat, apprecStatus)
apprec.get<XMLAppRec>().error.addAll(apprecErrors)
mqSender.sendReceipt(
payload = apprecMarshaller.toString(apprec)
payload = getApprecMarshaller().toString(apprec)
)
APPREC_COUNTER.increment()
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/kotlin/no/nav/syfo/util/JaxBUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ val fellesformatJaxBContext: JAXBContext = JAXBContext.newInstance(
XMLAppRec::class.java,
)

val senderMarshaller: 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)
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)
Expand All @@ -59,5 +55,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())
}

0 comments on commit a25c3f7

Please sign in to comment.