Drools channels and Apache Camel integration.

The previous posts only showed how to build a route using the Drools endpoint as the target consumer of the message, but what happen if we need to start the route from a Drools endpoint (the source endpoint) and send a message from the rules consequence to the target endpoint? In this case we need to use Drools channels.

A Drools channel provides a mechanism to send objects from the working memory to some external process or functions.

These Drools channels can be used independently with the API but they also were integrated into the Camel integration module. To use them you only have to add the channels parameter in the endpoint definition, as you can see in the next route definition.

RouteBuilder rb = new RouteBuilder() {
    public void configure() throws Exception {
        from("drools://node/ksession?channel=logins").to("activemq:queue:security-queue");
    }
};

When the route is started the Drools component will register a custom channel implementation into the internal stateful knowledge session configured in the source endpoint, which will be available to use in your rules consequence.

Once the routes are configured you can use the registered channels in your rules consequences as shown in the next simple business rule:

rule "login verification"
when
    $login : Login()
    // something else
then
    channels["logins"].send("user " + $login.getUsername() + " logged at " + new Date());

Using Drools channels is really simple, you only have to obtain your defined channel and invoke the send method together with an object parameter. Internally, the custom channel implementation will create a Camel exchange message with your object parameter and will send it to the current Camel processor to continue the route. For example, in the previous defined route all the objects sent to the logins channel will be sent to the target endpoint, in this case to an ActiveMQ JMS security-queue queue.

As the routes will be started from your rules consequence there’s no need to use a Camel Producer object to send messages. Instead you only have to interact with the knowledge session as always do, inserting facts and firing rules.

For a complete example check the available project in the next github repository.
https://github.com/lucazamador/drools-camel-channels-example

Advertisements

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