The Challenge
Here is the task. I have a file like the following:AAAA BBBB CCCC DDDDI need to convert this content to an ArrayList, so that I can look through them. Actually each line is an object name. I need to query those objects from web services.
Solution
Here are the steps:- Load file and payload as string. Mule provide a construct called "parse-template", which can be used to load file. This construct will load the file and convert the content to a string payload.
- Use Groovy or MEL to convert the string to an ArrayList
1 2 3 4 5 6 7 8 9 | < parse-template location = "veeva_objects.txt" doc:name = "Parse Template" > </ parse-template > < set-payload value = "#[Arrays.asList(payload.trim().replaceAll("[\\t\\n\\r]", ",").split(","))]" doc:name = "Set Payload" > </ set-payload > < scripting:component doc:name = "Groovy" > < scripting:script engine = "Groovy" > <!--[CDATA[return payload.trim().replaceAll('[\\t\\n\\r]', ',').split(',').collect{ it.trim()}]]--> </ scripting:script > </ scripting:component > |
Key Learning Points
Few points we learn from this:- MEL support java directly. This is really very powerful.
- Groovy is very powerful tool in Mule application development. collect {it.trim()} is similar as java stream function.
In Mule, the way you have described reading the file,, could be very memory consuming depending on the size of my file(I might not be knowing the scenario you were trying to touch), but can we read a file, and parse it the using streams ?
ReplyDeleteScenario could be, My file has header and footer (plain text file), I want to read data part only after specific pattern match in the file.
Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDelete