Child pages
  • ProcessorFactory
Skip to end of metadata
Go to start of metadata


Available as of Camel 2.4

There is a org.apache.camel.spi.ProcessorFactory which allows you to use a custom factory for creating Processor based on the Camel routes.
The factory can also be used for manipulating the definitions before the Processors is created. For example you can use it to alter options, or even add new outputs to the routes, before the routes is actually created.


Here is a custom factory which will alter the processor definitions.

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
public static class MyFactory implements ProcessorFactory {

    public Processor createChildProcessor(RouteContext routeContext, ProcessorDefinition<?> definition, boolean mandatory) throws Exception {
        return null;

    public Processor createProcessor(RouteContext routeContext, ProcessorDefinition<?> definition) throws Exception {
        if (definition instanceof SplitDefinition) {
            // add additional output to the splitter
            SplitDefinition split = (SplitDefinition) definition;
            split.addOutput(new ToDefinition("mock:extra"));

        if (definition instanceof SetBodyDefinition) {
            SetBodyDefinition set = (SetBodyDefinition) definition;
            set.setExpression(new ConstantExpression("body was altered"));

        // return null to let the default implementation create the processor, we just wanted to alter the definition
        // before the processor was created
        return null;

The unit test and route is as follows:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
public void testAlterDefinitionUsingProcessorFactory() throws Exception {
    getMockEndpoint("mock:foo").expectedBodiesReceived("body was altered");

    template.sendBody("direct:start", "Hello World");


public void testAlterDefinitionUsingProcessorFactoryWithChild() throws Exception {
    getMockEndpoint("mock:split").expectedBodiesReceived("body was altered", "body was altered");
    getMockEndpoint("mock:extra").expectedBodiesReceived("body was altered", "body was altered");

    template.sendBody("direct:foo", "Hello,World");


protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        public void configure() throws Exception {
                .setBody().constant("body not altered").to("mock:foo");

                    .setBody().constant("body not altered").to("mock:split")

Now the idea is that the setBody processors has been changed by the factory to set a different body.
Also the Splitter has one additional output where we send the splitted message to mock:extra endpoint.

Notice we just return null which instructs Camel to use its default implementation to create the Processor. After all we just wanted to manipulate the definition. However if you do return a Processor then Camel use the returned processor.

Java DSL

In Java DSL all you need to do is to configure the custom factory on the CamelContext using the setProcessorFactory method:

Configuring ProcessorFactory in Java
  CamelContext context ...
  context.setProcessorFactory(new MyFactory());

Spring XML

In Spring XML all you have to do is just to declare a <bean> tag with the custom factory, and Camel will automatic lookup it up and use it.

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'

<!-- use a custom processor factory as a hook which can manipulate the model before the processors
     is created, which allows us to do stuff to the route before its created -->
<bean id="customFactory" class="org.apache.camel.processor.CustomProcessorFactoryTest$MyFactory"/>

<camelContext xmlns="">
    <!-- a simple route -->
        <from uri="direct:start"/>
            <constant>body not altered</constant>
        <to uri="mock:foo"/>

    <!-- a route which has a sub route (the split) -->
        <from uri="direct:foo"/>
                <constant>body not altered</constant>
            <to uri="mock:split"/>
        <to uri="mock:result"/>


  • No labels