ProcessorFactoryPage edited by Claus IbsenChanges (8)
Full ContentProcessorFactoryAvailable 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. ExampleHere is a custom factory which will alter the processor definitions. MyFactory 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 route is as follows Route public void testAlterDefinitionUsingProcessorFactory() throws Exception { getMockEndpoint("mock:foo").expectedBodiesReceived("body was altered"); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); } 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"); getMockEndpoint("mock:result").expectedBodiesReceived("Hello,World"); template.sendBody("direct:foo", "Hello,World"); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .setBody().constant("body not altered").to("mock:foo"); from("direct:foo") .split(body()) .setBody().constant("body not altered").to("mock:split") .end() .to("mock:result"); } Now the idea is that the setBody processors has been changed by the factory to set a different body. Java DSLIn Java DSL all you need to do is to configure the custom factory on the CamelContext as shown: Configuring ProcessorFactory in Java protected CamelContext createCamelContext() throws Exception { CamelContext context = super.createCamelContext(); // register our custom factory context.setProcessorFactory(new MyFactory()); return context; } public void testAlterDefinitionUsingProcessorFactory() throws Exception { getMockEndpoint("mock:foo").expectedBodiesReceived("body was altered"); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); } 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"); getMockEndpoint("mock:result").expectedBodiesReceived("Hello,World"); template.sendBody("direct:foo", "Hello,World"); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .setBody().constant("body not altered").to("mock:foo"); from("direct:foo") .split(body()) .setBody().constant("body not altered").to("mock:split") .end() .to("mock:result"); } }; } 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; } } } Spring XMLIn 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. Configuring ProcessorFactory in Spring
<!-- 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 id="camel" xmlns="http://camel.apache.org/schema/spring">
<!-- a simple route -->
<route>
<from uri="direct:start"/>
<setBody>
<constant>body not altered</constant>
</setBody>
<to uri="mock:foo"/>
</route>
<!-- a route which has a sub route (the split) -->
<route>
<from uri="direct:foo"/>
<split>
<simple>${body}</simple>
<setBody>
<constant>body not altered</constant>
</setBody>
<to uri="mock:split"/>
</split>
<to uri="mock:result"/>
</route>
</camelContext>
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > ProcessorFactory confluence
- [CONF] Apache Camel > ProcessorFactory confluence
- [CONF] Apache Camel > ProcessorFactory confluence
