Spring and CDI are great frameworks for integrating Drools inside our own applications. In cases where we want to expose Drools functionality as a service to other applications, we need to start looking at frameworks that expose service endpoints. For this purpose, there is an integration component that allows us to expose Drools components through Apache Camel (http://camel.apache.o) endpoints, called Kie Camel.
Apache Camel is an integration framework that lets us define routes that merge together different types of services and components, using a series of predefined and market accepted patterns called Enterprise Integration Patterns (EIP). Similarly to a design pattern, an EIP allows us to define reusable, easy to understand and extensible components. The main focus of EIP is to provide simple, reusable structures to define service endpoints.
In order to use Kie Camel, the first thing we need to do is add a dependency to our POM file:
<dependency> <groupId>org.jboss.integration.fuse</groupId> <artifactId>kie-camel</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency>
Note: the kie-camel component is managed as part of the JBoss integration tooling, outside the Drools projects, and that is why it uses a different versioning scheme from the rest of the Kie components. Version 1.3.0-SNAPSHOT is the current version, which uses Drools 6.3.0.Final dependencies internally.
This component will have all the dependencies as Camel core libraries, which will make it easier to define our endpoints. These endpoints will be defined using a Camel concept called routes; routes are specific Enterprise Integration Pattern implementations, where we configure how different endpoints interact with each other. We can define them using a Spring context file, and here's a short example of how to create a route that uses Drools:
<bean id="kPolicy" class="org.kie.camel.component.KiePolicy" /> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct://someOriginalRoute"/> <policy ref="kPolicy"> <unmarshal ref="xstream" /> <to uri="kie:ksession1" /> <marshal ref="xstream" /> </policy> </route> </camelContext>
This simple piece of Spring context does a lot of things:
As we can imagine, there is a lot more that we can do with Apache Camel to expose different parts of this Kie Session to other beans. We could feed information from its listeners to other endpoints, perform previous validations to the data being introduced into the Kie Session, or anything else we can imagine.
When we expose the endpoint through a REST
server (you can find the rest of this configuration in the chapter-11-camel/src/test/resources/cxf-rs-spring.xml
file), we can invoke it by passing XML representations of a Drools Kie Session command, such as the following:
<batch-execution lookup="ksession1"> <insert out-identifier="myItem"> <org.drools.devguide.eshop.model.Item> <cost>119.0</cost> <category>NA</category> </org.drools.devguide.eshop.model.Item> </insert> <fire-all-rules/> </batch-execution>
This will cause the command to be fed into the Kie Session, which will then insert the specified Item object into the working memory and fire all the rules. A full list of the XML commands which can be fed into a Kie Session in this manner can be found at http://docs.jboss.org/drools/release/6.3.0.Final/drools-docs/html/ch11.html.
We can run this example by running the KieCamelTest
JUnit test in the chapter-11-camel
project in the code bundle. Apache Camel is a very large topic to be fully covered in this book, but for those who still find it useful and want to start looking into Camel, you can get started at http://camel.apache.org/manual.html.
3.131.38.210