Contact Us 1-800-596-4880

Creating Message Processors

One of the key concepts in Mule is Message Processor. In order to make available Java methods in Mule a special annotations has to be used. The @Processor annotation is method annotation that will hint the Annotation Processor that a particular method can be consumed as a Message Processor.

Quick example:

@Module(name = "myextension")
public class MyExtension {

   @Processor
   public void myMethod() {
       // code here
   }

The method myMethod can be invoked from Mule in the following way:

<myextension:myMethod />

The @Processor Annotation

Any public, instance method can be annotated with the @Processor and then be invoked from Mule. These methods can receive any number and type of arguments and have any return type.

Parameter Description

name

Optional. The xml name of the element that will invoke this processor. If not specified,
the name will be derived from the name of the method.

intercepting

Optional. Setting this value to true will trigger the generation of a Intercepting Message Processor rather than a Message Processor.

@Optional

Similar to an @Optional annotated instance variable, using the @Optional on a method parameter means that it will not be required to provide a value for this parameter when invoking this method from Mule.

Example:

@Module(name = "optional")
public class OptionalModule {

    @Processor
    public double division(int sum1, int sum2, @Optional int divisor) {
        return (sum1 + sum2) / divisor;
    }

Because the divisor parameter is not mandatory, executing the following will result in a exception because it will attempt to divide by zero (zero is default value for ints).

<optional:division sum1="40" sum2="10"/>

@Default

A parameter annotated with @Default will take this value if it not explicitly set. Using the previous example:

@Module(name = "optional")
public class OptionalModule {

    @Processor
    public double division(int sum1, int sum2, @Optional @Default("1") int divisor) {
        return (sum1 + sum2) / divisor;
    }

Because the divisor parameter has now a default value of 1, the following executes normally and sets the payload to 50.

<optional:division sum1="40" sum2="10"/>

Intercepting Message Processors

This feature has been reworked in 3.0.3. Prior to 3.0.3, it needed an InterceptCallback. The InterceptCallback interface has been replaced with SourceCallback. The SourceCallback gives more flexibility and supports a broader set of scenarios like splitters.

An intercepting message processor handles Mule events intercepting another listener Message Processor. It is the intercepting message processor’s responsibility to decide whether the processing should continue or not, that is, if the next message processor in the chain should be invoked.

When setting the intercepting parameter to true, the method must have a parameter of type org.mule.api.callback.SourceCallback.

Example:

@Processor(intercepting = true)
    public Object shouldContinue(SourceCallback afterChain, ...) throws Exception {
        if (...) {
            return afterChain.process();
        }

        ...
    }

Restrictions

Some restrictions apply to the methods that can be annotated with @Processor:

  • Cannot be static

  • Cannot be non-public

  • Cannot have parameters named with the word name

  • Overloading is not supported

More information