Over a million developers have joined DZone.

Replacing Standard Mule Transformer w/ Custom Implementation

DZone's Guide to

Replacing Standard Mule Transformer w/ Custom Implementation

· Integration Zone
Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

Although there is a lot of documentation about Mule ESB the issue in this post still took me some time to get it right. In this post I give a complete example of how I replaced standard Mule functionality (a transformer in this case) with my own implementation.

My issue started with this question which I posted at the Mule Forum without any response so far. This made me look for a solution myself, which is one of the benefits of open source; it offers you the opportunity to do so.

I soon realized that the behavior I want could be quite easily be implemented by a small change in the MessagePropertiesTransformer. The original code (I am using Mule CE version 3.2.1) was like this:
protected void addProperties(MuleMessage message)
   final String key = entry.getKey();

   Object value= entry.getValue();
   Object realValue = value;

   //Enable expression support for property values
   if (muleContext.getExpressionManager().isExpression(value.toString()))
     realValue = muleContext.getExpressionManager().evaluate(value.toString(), message);
What we see here is a piece of the addProperties method of the MessagePropertiesTransformer. The code checks if the supplied value of an added property is an expression and if so it resolves the expression to the ‘realValue’.
The way I modified it:
protected void addProperties(MuleMessage message)
  final String key = entry.getKey();

  Object value= entry.getValue();
  Object realValue = value;

  // Modified to be able to execute expression in expressions :-)
  while (muleContext.getExpressionManager().isExpression(realValue.toString())){
     realValue = muleContext.getExpressionManager().evaluate(realValue.toString(), message);
As you can see I am iterating over the realValue till it is no longer an expression anymore. With this line of code I can get my wanted behavior: Supplying an expression that resolves to an expression that resolves to a real value. So far for the easy part. Now I wanted to use my modified transformer instead of the standard one if I use a construction like:
<message-properties-transformer scope="invocation">
  <add-message-property key="field1" value="#[xpath:#[header:INVOCATION:my.attribute.1]]"/>
The way I solved it was by adding my own namespace in the Mule configuration. Although it is described here I think it is handy to supply a complete example here:

  • Define the schema for your element
  • In my case it was simple since I didn’t change any at this level, so I just copied the existing element in my own schema file which I saved as ‘my-schema.xsd’ in the META-INF directory of my project:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.pascalalma.net/schema/core" 
            elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xsd:import namespace="http://www.mulesoft.org/schema/mule/core" schemaLocation="http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd"/>
    <xsd:documentation>My Mule Extension.</xsd:documentation>
  <!-- My Transformer definition -->
  <xsd:element name="message-properties-transformer" type="mule:messagePropertiesTransformerType"
        A transformer that can add, delete or rename message properties.
Just make sure you prefix the type and substitutionGroup you refer to in your XSD because you will get errors about unknown XML elements otherwise.

  • Write the Namespace Handler
  • In this case it is almost a copy of the existing NamespaceHandler for the transformer:

package net.pascalalma.mule.config;

import net.pascalalma.mule.config.spring.parsers.specific.MessagePropertiesTransformerDefinitionParser;
import org.mule.config.spring.handlers.AbstractMuleNamespaceHandler;

 * @author pascal
public class MyNamespaceHandler extends AbstractMuleNamespaceHandler {

    public void init() {
        registerBeanDefinitionParser("message-properties-transformer", new MessagePropertiesTransformerDefinitionParser());
    • Write the Definition Parser
    • It becomes boring but this is also largely a copy of the existing MessagePropertiesTransformerDefinitionParser:

package net.pascalalma.mule.config.spring.parsers.specific;

import net.pascalalma.mule.transformer.MessagePropertiesTransformer;
import org.mule.config.spring.parsers.specific.MessageProcessorDefinitionParser;

 * @author pascal
public class MessagePropertiesTransformerDefinitionParser extends MessageProcessorDefinitionParser
    public MessagePropertiesTransformerDefinitionParser()
        addAlias("scope", "scopeName");

      Just make sure you import the correct MessagePropertiesTransformer here!

      • Set the Spring handler mapping
      • Add the file ‘spring.handlers’ to the project in the META-INF directory if it doesn’t exist already. In the file put the following:

        • Set the local schema mapping
        • Add the file ‘spring.schemas’ to the project in the META-INF directory if it doesn’t exist already. In the file put the following:

        • Modify the Mule config to include the custom namespace
        • I added the following parts to my Mule config. First add the schema declaration :

      xsi:schemaLocation="http://www.pascalalma.net/schema/core http://www.pascalalma.net/schema/core/1.0/my-schema.xsd 	

          Next I use this namespace prefix with my transformer like this:

<pan:message-properties-transformer scope="invocation">
  <add-message-property key="field1" value="#[header:INVOCATION:id.attribute.1]"/>

        As you can see I changed the content of field1 so it matches my needs. What is happening now is that the expression ‘#[header:INVOCATION:id.attribute.1]‘ resolves to a XPath expression like ‘#[xpath:/msg/header/ID]‘ and this resolves to a real value that I can use in the remainder of the flow.

Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.


Published at DZone with permission of Pascal Alma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}