Stream the XMLSlurper input in Groovy Scripts
In addition to modeling the desired features in integration scenarios, it is important to also consider non-functional aspects like resource consumption, performance, and reliability, just to name a few.
This blog is part of a series that shall assist Integration Developers to properly address these qualities when modeling Integration Flows.
This contribution focuses on Groovy scripts for XML processing, as they are often used in the Script step.
Integration Developers often use XmlSlurper
in Groovy scripts to process XML messages. There are aspects that need to be considered regarding memory consumption. Consider the following code snippet:
def body = message.getBody(java.lang.String)
def xml = new XmlSlurper().parseText(body)
In the code above the body
variable is just used to provide input to the XmlSlurper
. This is fine as long as the body is of type java.lang.String
. In all other cases conversion to java.lang.String
will be applied, which requires allocation of additional memory for the String
object. Note that when the message body is large, or when many messages are processed in parallel, the additional memory footprint might even cause a java.lang.OutOfMemoryError
. Ultimately the OutOfMemoryError
would interrupt the message processing.
The additional memory allocation can be avoided if the XmlSlurper
would accept the body as it is, or if the body could be streamed. Then, a better approach is to stream the message body to the XmlSlurper
by using message.getBody(java.io.Reader)
, as shown in the following snippet:
def body = message.getBody(java.io.Reader)
def xml = new XmlSlurper().parse(body)
This will do the magic of streaming the message body – the body
variable is now a java.io.Reader
that is just a reference to the message payload object, thus reducing memory consumption and contributing to reliability of your integration scenario.
The next contribution in this series is Avoid Binding Variables in Groovy Scripts.
Thanks Markus for good blog and waiting for other blogs of the series... !
Regards,
Sriprasad Shivaram Bhat
I would like to award points for the best blog title I have seen in a long while. I love the names people give to new technology, when someone outside the IT space sees a sentence like "Stream the XML Slurper in the Groovy Script" it really helps dispel the idea we are all nerds.
As I recall the XML Slurper was some sort of monster from Star Wars, and when it got killed by Luke Skywalker it;s keeper burst into tears.
Hi Markus
Thanks for this blog on performance consideration. If I have a CSV to XML conversion step, the output of it would be a byte array. Would the same consideration apply there, i.e. do we need to stream it in the subsequent Groovy script, or is it even possible?
Regards
Eng Swee