Using Mule, I need to loop a collection of records in a batch fashion (don't want to use the Batch scope). In the foreach element you have the way to specify the batch size to partition your collection.
Being said that, if you specify a number it works just fine. For instance
<foreach doc:name="For Each" batchSize="100">
<logger message="#[flowVars.counter]" level="INFO" doc:name="Logger"/>
</foreach>
It will print batches of 100 elements as I want. But if I use MEL it throws a NumberFormatException. Here the xml
<foreach doc:name="For Each" batchSize="#[flowVars.counter]">
<logger message="#[flowVars.counter]" level="INFO" doc:name="Logger"/>
</foreach>
The exception
ERROR 2017-03-01 09:47:06,121 [main] org.mule.module.launcher.application.DefaultMuleApplication: null
java.lang.NumberFormatException: For input string: "[flowVars.batchSize]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_121]
at java.lang.Integer.parseInt(Integer.java:580) ~[?:1.8.0_121]
at java.lang.Integer.valueOf(Integer.java:740) ~[?:1.8.0_121]
at java.lang.Integer.decode(Integer.java:1197) ~[?:1.8.0_121]
I printed the class type of #[flowVars.batchSize] and it is an Integer, so that shouldn't be the problem. Instead, I think that the foreach scope doesn't allow you to use MEL at least for this property.
My question, is it or is it not possible to use MEL to determine the batch size value of a foreach scope?
Thanks in advance.
Notice that this is not a runtime error. This error appears in the initialise() stage of the class ForEach,in other words, according to the default configuration you are not allowed to set dynamically the batchSize of the components. The issue is that he is trying to parse the xml to get the value inside the batchSize="" xml tag and he finds a string (#[flowVars.counter]) and not an integer ("5").
ForEach class below:
public class Foreach extends AbstractMessageProcessorOwner implements Initialisable, MessageProcessor, NonBlockingSupported{
@Override
public void initialise() throws InitialisationException
{....
splitter.setBatchSize(batchSize); .... }
As a work around, you can just set one property as batchSizeForEach and refer to this property using ${batchSizeForEach}
Regards!