Drools Camel Integration

The Apache Camel integration allows us to interact with a Drools Stateless or Stateful session through a pipeline. It basically works by transforming XML commands into executable commands and executing each of them. The advantage of this integration is that Apache Camel makes possible the implementation of more advanced enterprise integration patterns, which is an improvement of Drools Pipeline.

This integration with Drools allows us to add any of the current Camel components. Using the Apache Components you can, receive commands from several entry points and send the execution result to another entry point. Just to name a few of the components: JMS queue/Atom Feed/Mina connection/a Mail/etc. As you can see, this brings a more powerful interoperability mechanism to integrate your application with Drools. Actually Drools is using Camel 2.4.0 that allows a more strong internal integration between them.

Introduction to Drools Grid
This integration is coupled with another drools module called: drools-grid. This module allow us to interact with Drools sessions independent of the JVM location. At the moment we can use two implementations:

  • Local: Used when the drools sessions and clients are in the same JVM
  • Remote: Used when you have drools sessions on a remote JVM. Currently, the only implementation is based on Apache Mina. HornetQ support is actually done, but don’t available in trunk

Drools Grid is embedded inside the Drools Camel component, so don’t worry about further implementation information because this is hidden by Drools. With this information we can start to configure our Camel Context.

Creating our Camel Context

We need to create our own CamelContext to start. The first thing that we should do is create and register the grid node that we are going to use inside the CamelContext. In the most common cases we need to implement the Local grid connection. The next step after the LocalConnection creation is to get a new ExecutionNode and register this inside the CamelContext.

LocalConnection connection = new LocalConnection();
ExecutionNode node = connection.getExecutionNode();
Context jndiContext = new JndiContext();
jndiContext.bind("node", node);
CamelContext camelContext = new DefaultCamelContext(jndiContext);

After this code, we have a properly configured CamelContext with all the configurations to use a Drools Component. However, before we are ready to execute Drools, we need to configure Camel entry points.

Using the previous configured CamelContext it’s necessary to register into the ExecutionNode all the KnowledgeSessions that you are going to use.

node.get(DirectoryLookupFactoryService.class).register("ksession1", ksession);

Creating Camel Routes

This is the most powerful feature of this integration, because of the very large library of Components provided by Camel to build pipelines. If you check the Camel Components you can see how many integration possibilities are added now to Camel, and if this component doesn’t exists you can create this by your own or request it to the Camel developers!.

The first step is know how create a Drools endpoint, which has the next syntax

{0} : Execution Node identifier that is registered in the CamelContext
{1} : Knowledge Session identifier that was registered in the Execution Node with identifier {0}

Now that you know how define a drools endpoint, you can create your routes and add them to the Camel Context

RouteBuilder rb = new RouteBuilder() {
public void configure() throws Exception {

from( "direct:test-with-session" ).policy( new DroolsPolicy() ).unmarshal( "xstream" ).to( "drools:node/ksession1" ).marshal( "xstream" );

But there are several ways to create Drools endpoints, and different configurations to support extra features. Let’s take a look:


This way we have several alternatives to define the routes:

  • Processing the commands objects directly
  • Processing the commands in a XML representation, using XStream or JSON as the marshaller/unmarshaller
from( "direct:test-with-session" ).policy( new DroolsPolicy() ).unmarshal( "xstream" ).to( "drools:node/ksession1" ).marshal( "xstream" );

Note: to use JSON just change the unmarshall() and marshall() parameter to “json”

  • Using a custom XStream Data Format with your own XStream Converters
org.apache.camel.model.dataformat.XStreamDataFormat xstreamDataFormat = new org.apache.camel.model.dataformat.XStreamDataFormat();
xstreamDataFormat.setConverters( Arrays.asList( new String[]{PersonConverter.class.getName()} ) );

Map dataFormats = new HashMap();
dataFormats.put( "custom-xstream",
xstreamDataFormat );
camelContext.setDataFormats( dataFormats );

from( "direct:test-no-session-custom" ).policy( new DroolsPolicy() ).unmarshal( "custom-xstream" ).to( "drools:node" ).marshal( "custom-xstream" );
  • Using JAXB as the marshaller/unmarshaller
JaxbDataFormat def = new JaxbDataFormat();
def.setPrettyPrint( true );
// Set the package names to be registered by JAXB
def.setContextPath( "org.drools.model:org.drools.pipeline.camel" );

from( "direct:test-with-session" ).policy( new DroolsPolicy() ).unmarshal( def ).to( "drools:node/ksession1" ).marshal( def );
  • Using Spring
<bean id="droolsPolicy" class="org.drools.camel.component.DroolsPolicy" />
 <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
      <from uri="cxfrs://bean://rsServer"/>
      <policy ref="droolsPolicy">
          <unmarshal ref="xstream" />
          <to uri="drools:node1/ksession1" />
          <marshal ref="xstream" />

What is the Camel Drools Policy? In a nutshell this class is used to add Drools support in Camel, what it does is to add interceptors into the camel route to create Camel Processors on the fly and modify the internal navigation route.

This post is a introduction to the new and powerful configuration, if you want to know how interact with it you can read the Drools Commands documentation here to see how create Command objects and marshall/unmarshall them.

In the next post I will show you how interact programmatically with this integration, how send messages and what else you can do.


3 thoughts on “Drools Camel Integration

    1. lucaz Post author

      This post is out-dated because the camel integration changed in the last release, i’ll create a new post about jbpm+spring+drools+camel in the next days. Regards.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s