JBoss.org Community Documentation

- Prev
- Next

## Chapter 3. Drools Camel Server

## 3.1. Introduction

The drools camel server (drools-camel-server) module is a war which you can deploy to execute KnowledgeBases remotely for any sort of client application. This is not limited to JVM application clients, but any technology that can use HTTP, through a REST interface. This version of the execution server supports stateless and stateful sessions in a native way.

## 3.2. Deployment

Drools Camel Server is a war file, which can be deployed in a application server (such as JBoss AS). As the service is stateless, it is possible to have have as many of these services deployed as you need to serve the client load. Deploy on JBoss AS 4.x / Tomcat 6.x works out-of-the-box, instead some external dependencies must be added and the configuration must be changed to be deployed in JBoss AS 5

## 3.3. Configuration

Inside the war file you will find a few XML configuration files.

- beans.xml

  - Skeleton xml that imports knowledge-services.xml and camel-server.xml

- camel-server.xml

  - Configures CXF endpoints with Camel Routes

  - Came Routes pipeline messages to various configured knowledge services

- knowledge-services.xml

  - Various Knowledge Bases and Sessions

- camel-client.xml

  - Sample camel client showing how to send and receive a message

  - Used by "out of the box" test.jsp

### 3.3.1. REST/Camel Services configuration

The next step is configure the services that are going to be exposed through drools-server. You can modify this configuration in camel-server.xml file.

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:cxf="http://camel.apache.org/schema/cxf"

      xmlns:jaxrs="http://cxf.apache.org/jaxrs"

      xsi:schemaLocation="

      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

      http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd

      http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd

      http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />

    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

      <!--

       !   If you are running on JBoss you will need to copy a camel-jboss.jar into the lib and set this ClassLoader configuration

       !  http://camel.apache.org/camel-jboss.html

       !   <bean id="jbossResolver" class="org.apache.camel.jboss.JBossPackageScanClassResolver"/>

       -->

      <!--

       !   Define the server end point.

       !   Copy and paste this element, changing id and the address, to expose services on different urls.

       !   Different Camel routes can handle different end point paths.

       -->

      <cxf:rsServer id="rsServer"

                    address="/kservice/rest"

                    serviceClass="org.drools.jax.rs.CommandExecutorImpl">

        <cxf:providers>

          <bean class="org.drools.jax.rs.CommandMessageBodyReader"/>

        </cxf:providers>

      </cxf:rsServer>

      <!-- Leave this, as it's needed to make Camel "drools" aware -->

      <bean id="droolsPolicy" class="org.drools.camel.component.DroolsPolicy" />

      <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

        <!--

         ! Routes incoming messages from end point id="rsServer".

         ! Example route unmarshals the messages with xstream and executes against ksession1.

         ! Copy and paste this element, changing marshallers and the 'to' uri, to target different sessions, as needed.

         !-->

        <route>

          <from uri="cxfrs://bean://rsServer"/>

            <policy ref="droolsPolicy">

              <unmarshal ref="xstream" />

              <to uri="drools:node1/ksession1" />

              <marshal ref="xstream" />

            </policy>

        </route>

      </camelContext>

    </beans>

#### 3.3.1.1. RESTful service endpoint creation

In the next xml snippet code we are creating a RESTful (JAX-RS) endpoint bound to /kservice/rest address and using org.drools.jax.rs.CommandExecutorImpl as the service implementer. This class is only used to instantiate the service endpoint because all the internal implementation is managed by Camel, and you can see in the source file that the exposed execute service must be never called.

Also a JAX-RS Provider is provided to determine if the message transported can be processed in this service endpoint.

    <cxf:rsServer id="rsServer"

                   address="/kservice/rest"

                   serviceClass="org.drools.jax.rs.CommandExecutorImpl">

      <cxf:providers>

        <bean class="org.drools.jax.rs.CommandMessageBodyReader"/>

      </cxf:providers>

    </cxf:rsServer>

Ideally this configuration doesn't need to be modified, at least the Service Class and the JAX-RS Provider, but you can add more endpoints associated to different addresses to use them in other Camel Routes.

After all this initial configuration, you can start config your own Knowledge Services.

#### 3.3.1.2. Camel Drools Policy & Context creation

DroolsPolicy is used to add Drools support in Camel, basically what it does is to add interceptors into the camel route to create Camel Processors on the fly and modify the internal navigation route. If you want to have SOAP support you need to create your custom Drools Policy, but it's going to be added in the next release.

But you don’t need to know more internal details, only instantiate this bean:

    <bean id="droolsPolicy" class="org.drools.camel.component.DroolsPolicy" />

The next is create the camel route that will have the responsibility to execute the commands sent through JAX-RS. Basically we create a route definition associated with the JAX-RS definition as the data input, the camel policy to be used and inside the “execution route” or ProcessorDefinitions. As you can see, we set XStream as the marshaller/unmarshaller and the drools execution route definition

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

      <route>

        <from uri="cxfrs://bean://rsServer"/>

        <policy ref="droolsPolicy">

          <unmarshal ref="xstream" />

          <to uri="drools:node1/ksession1" />

          <marshal ref="xstream" />

        </policy>

      </route>

    </camelContext>

The drools endpoint creation has the next arguments

    <!-- XML : generated by JHighlight v1.0 (http://jhighlight.dev.java.net) -->
    <span class="xml_tag_symbols">&lt;</span><span class="xml_tag_name">to</span><span class="xml_plain">&nbsp;</span><span class="xml_attribute_name">uri</span><span class="xml_tag_symbols">=</span><span class="xml_attribute_value">&quot;drools:{1}/{2}&quot;</span><span class="xml_plain">&nbsp;</span><span class="xml_tag_symbols">/&gt;</span><span class="xml_plain"></span><br />

1. Execution Node identifier that is registered in the CamelContext

2. Knowledge Session identifier that was registered in the Execution Node with identifier {1}

Both parameters are configured in knowledge-services.xml file.

#### 3.3.1.3. Knowledge Services configuration

The next step is create the Knowledge Sessions that you are going to use.

    <beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xmlns:drools="http://drools.org/schema/drools-spring"

           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

                               http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">

       <drools:execution-node id="node1" />

       <drools:kbase id="kbase1" node="node1">

          <drools:resources>

             <drools:resource  type="XSD" source="classpath:model.xsd"/>

             <drools:resource  type="DRL" source="classpath:test.drl"/>

          </drools:resources>

       </drools:kbase>

       <drools:ksession id="ksession1" type="stateless" kbase="kbase1" node="node1"/>

    </beans>

The execution-node is a context or registered kbases and ksessions, here kbase1 and ksession1 are planed in the node1 context. The kbase itself consists of 2 knowledge definitions, a drl and an xsd. The Spring documentation contains a lot more information on configuring these knowledge services.

#### 3.3.1.4. Test

With drools-server war unzipped you should be able to see a test.jsp and run it. This example just executes a simple "echo" type application. It sends a message to the rule server that pre-appends the word "echo" to the front and sends it back. By default the message is "Hello World", different messages can be passed using the url parameter msg - test.jsp?msg="My Custom Message".

Under the hood the jsp invokes the Test.java class, this then calls out to Camel which is where the meet happens. The camel-client.xml defines the client with just a few lines of xml:

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

       <route>

          <from uri="direct://kservice"/>

          <policy ref="droolsPolicy">

             <to uri="cxfrs://http://localhost:8080/drools-server-app/kservice/rest"/>

          </policy>

       </route>

     </camelContext>

"direct://kservice" is just a named hook, allowing java to grab a reference and push data into it. In this example the data is already in xml, so we don't need to add any DataFormat's to do the marshalling. The DroolsPolicy adds some smarts to the route and you'll see it used on the server side too. If JAXB or XStream were used, it would inject custom paths and converters, it can also set the ClassLoader too on the server side, on the client side it automatically unwraps the Response object.

The rule itself can be found here: test.drl. Notice the type Message is declared part of the drl and is thus not present on the Classpath.

    declare Message
       text : String
    end

    rule "echo" dialect "mvel"
    when
       $m : Message();
    then
       $m.text = "echo:" + $m.text;
    end

- PrevChapter 2. Spring Integration
- Top of page
- Front page
- NextChapter 4. Drools Commands