I'm having difficulty returning a collection of MuleMessage instances from a component.
Mule 3.3.1.
The following code works (i.e., after the component, a foreach component with a logger dumps out "abc" and "def" as I expect).
public Object onCall( MuleEventContext eventContext ) throws Exception
{
MuleMessage message = eventContext.getMessage();
MuleMessageCollection collection = new DefaultMessageCollection( message.getMuleContext() );
String s1 = "abc";
String s2 = "def";
DefaultMuleMessage m1 = new DefaultMuleMessage( s1, message.getMuleContext() );
DefaultMuleMessage m2 = new DefaultMuleMessage( s2, message.getMuleContext() );
List<MuleMessage> list = new ArrayList<MuleMessage>();
list.add( m1 );
list.add( m2 );
collection.addMessages( list );
return collection;
}
If, however, I substitute a class of my own in place of the Strings, like so:
public Object onCall( MuleEventContext eventContext ) throws Exception
{
MuleMessage message = eventContext.getMessage();
MuleMessageCollection collection = new DefaultMessageCollection( message.getMuleContext() );
LicenseRequest s1 = new LicenseRequest();
LicenseRequest s2 = new LicenseRequest();
DefaultMuleMessage m1 = new DefaultMuleMessage( s1, message.getMuleContext() );
DefaultMuleMessage m2 = new DefaultMuleMessage( s2, message.getMuleContext() );
List<MuleMessage> list = new ArrayList<MuleMessage>();
list.add( m1 );
list.add( m2 );
collection.addMessages( list );
return collection;
}
I get an exception:
org.mule.transport.http.HttpsConnector Work caused exception on 'workCompleted'. Work being executed was: org.mule.transport.http.HttpsMessageReceiver$HttpsWorker@7b921c57
org.mule.exception.DefaultSystemExceptionStrategy Caught exception in Exception Strategy: Payload was invalidated calling setPayload and the message is not collection anymore.
java.lang.IllegalStateException: Payload was invalidated calling setPayload and the message is not collection anymore.
at org.mule.DefaultMessageCollection.checkValidPayload(DefaultMessageCollection.java:107)
at org.mule.DefaultMessageCollection.newThreadCopy(DefaultMessageCollection.java:312)
at org.mule.DefaultMuleEvent.newThreadCopy(DefaultMuleEvent.java:779)
at org.mule.RequestContext.newEvent(RequestContext.java:140)
at org.mule.RequestContext.setExceptionPayload(RequestContext.java:121)
at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:54)
at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:77)
at org.mule.transport.http.HttpMessageReceiver$HttpWorker.run(HttpMessageReceiver.java:220)
at org.mule.work.WorkerContext.run(WorkerContext.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "[license-generation].HTTPSConnector.receiver.02" java.lang.IllegalStateException: Payload was invalidated calling setPayload and the message is not collection anymore.
at org.mule.DefaultMessageCollection.checkValidPayload(DefaultMessageCollection.java:107)
at org.mule.DefaultMessageCollection.newThreadCopy(DefaultMessageCollection.java:312)
at org.mule.DefaultMuleEvent.newThreadCopy(DefaultMuleEvent.java:779)
at org.mule.RequestContext.newEvent(RequestContext.java:140)
at org.mule.RequestContext.setExceptionPayload(RequestContext.java:121)
at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:54)
at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:77)
at org.mule.transport.AbstractConnector.handleWorkException(AbstractConnector.java:2099)
at org.mule.transport.AbstractConnector.workCompleted(AbstractConnector.java:2067)
at org.mule.work.WorkerContext.run(WorkerContext.java:338)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
At this point in my testing, LicenseRequest is just an empty class whose toString() method returns "ghi".
What am I doing wrong?
I should add that my goal is to return a collection of objects which are created from the incoming payload. I can then iterate over those objects in the calling flow and take appropriate action for each.
EDIT: It appears I can do what I want in a transformer, just not a component. Why is this?
I guess it is not required to wrap each of LicenseRequest object into a mule message and the adding it to the collection. Make a collection directly and return it from the onCall method.
Hope this helps.