Thursday, February 9, 2017

Mule Dev Trick: Load A Text File And Convert To ArrayList

The Challenge

Here is the task. I have a file like the following:
AAAA
BBBB
CCCC
DDDD
I 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
Here are the source code:

        
        

        
        

        
            
        

Key Learning Points

Few points we learn from this:
  1. MEL support java directly. This is really very powerful.
  2. Groovy is very powerful tool in Mule application development. collect {it.trim()} is similar as java stream function.

2 comments:

  1. 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 ?
    Scenario 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.

    ReplyDelete
  2. Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...