Warning: A non-numeric value encountered in /home/customer/www/archive.ricston.com/public_html/wp-content/themes/Divi/functions.php on line 5766

In this weeks’ blog post we look at an issue regarding the MuleMessageCollection in Mule 3. While using the collection aggregator, this message processor creates a MuleMessageCollection (as expected) which is a collection of the Mule messages that have been aggregated according to correlation criteria.

The problem with having MuleMessageCollection as the current payload, is that setting the payload from a component or transformer is ignored. This means that the payload will remain as the same MuleMessageCollection.

As of Mule 3.1.2, one can use the combine-collections-transformer in order to merge a collection of Mule messages into a new MuleMessage. Note however, that if the payload is a collection of collections, this transformer changes the payload into a single collection (hence merging the collections into one collection). This transformer may be used in the following way:

<flow name="message-aggregator-transformer"> 
   <inbound-endpoint ... /> 
   <collection-aggregator ... /> 
   <combine-collections-transformer />
   <outbound-endpoint ... />
</flow>

For versions of Mule prior to 3.1.2, the same effect may be achieved. Consider the following Mule configuration:

 <flow name="message-aggregator-transformer">
    <inbound-endpoint ... />
    <collection-aggregator ... />
    <component class="com.ricston.ListToString" />
    <outbound-endpoint ... /> 
</flow>

The focus here is creating a component class that replaces the current payload by changing a MuleMessageCollection into a DefaultMuleMessage. Note that using a transformer instead of a component does not work.

public class ListToString implements Callable {
@Override
	public Object onCall(MuleEventContext eventContext) throws Exception {
		String ans = "test";
		MuleMessage m = eventContext.getMessage();
		MuleMessage msg = new DefaultMuleMessage(ans, m, eventContext.getMuleContext());
		return msg;
	}
}
  1. Create a component class implementing the Callable interface and place it after the collection aggregator
  2. Create a MuleMessage with the current message
    MuleMessage m = eventContext.getMessage();
  3. Create an object that you would like to be the result of the transformation (in this example the String “test”)
  4. Create a new DefaultMuleMessage with its payload set as the newly created object in (3)
    MuleMessage msg = new DefaultMuleMessage(ans, m,
    eventContext.getMuleContext());
  5.  Return the new DefaultMuleMessage as the payload

The result of this is that we have replaced the current payload in Mule, from MuleMessageCollection to DefaultMuleMessage. The issue explained above has already been reported and documented.