prowide / prowide-iso20022

Comprehensive business model and parser for all ISO 20022 messages
https://www.prowidesoftware.com
Apache License 2.0
147 stars 68 forks source link

MX Xml Parsing taking too much time #23

Closed BSethi-Fintech closed 3 years ago

BSethi-Fintech commented 3 years ago

Hi, We are working to parse swift MX message of type seev.031.002.09 When we are calling the below function. It is taking 300ms to 600 ms for parsing different XML messages. MxSeev03100209 mx = MxSeev03100209.parse(xmlMessage);

[INFO ] 2021-04-21 21:46:01,612 Parser.mxParse() -Parsing started [INFO ] 2021-04-21 21:46:02,010 Parser.mxParse() -Parsing end

I attached sample XML messages for which it is taking this much time. Please check why it's taking so much time as we have to process thousands of messages in a single day and it's taking hours to process. MX_Sample_Xml_2.txt MX_Sample_xml_1.txt

zubri commented 3 years ago

The parse methods available in the specific classes such as MxCamt00300104 or in the AbstractMX class uses JAXB unmarshaller. By default implementation will create a new JAXBContext for each parse invocation.

To deal with it the implementation supports injecting a cache for the JaxbContext. This is managed by a singleton class JaxbContextLoader like this:

JaxbContextLoader.INSTANCE.setCacheImpl(new JaxbContextCacheImpl());

When a cache implementation is set in the loader, the created JAXBContext will be cached and re-used between parse and write calls improving simnifically the parsing performance.

The JaxbContextCacheImpl is a default out-of-the-box cache implementation based on a simple ConcurrentHashMap, with no eviction. This implementation is aimed to avoid additional third party dependencies.

This is an example using Guava:

https://gist.github.com/zubri/2ac9c8b0ffb3433e79efab515f9c4242

You then inject this as: JaxbContextLoader.INSTANCE.setCacheImpl(new GuavaJaxbContextCache());

zubri commented 3 years ago

I've just added an example of this in the public examples

https://github.com/prowide/prowide-iso20022-examples/blob/main/src/main/java/com/prowidesoftware/swift/samples/MxParseWithCache.java