Contact Us 1-800-596-4880

CEP Example

This is an adaptation of the Stock Tick example which comes bundled with Drools to run inside and integrate with Mule. It illustrates the following topics:

  • Using Mule messages as input to the Drools working memory

  • Using a Drools ruleset to generate Mule messages

  • Using Quartz to generate a periodic input signal

  • Using the AJAX transport to update a web page from Mule

Description

The application is an alert mechanism for sudden changes in a stock ticker. It is implemented using the CEP functionality of Drools. A group of companies to monitor is defined, then the current stock price of each company is kept as a fact in Drools' working memory. A periodic stock ticker is used as an event stream and Drools' ruleset is defined such that a percentage delta greater than a pre-defined threshold triggers an alert to be sent as a Mule message. Both the event stream and the alerts are routed via AJAX to a web page so that the user can monitor the processing.

Running the example

  1. Make sure you have met the prerequisites and installed Mule according to the [instructions].

  2. At the command line, navigate to the examples/cep directory under your Mule home directory.

  3. Copy the pre-built example zip to the applications folder ($MULE_HOME/apps) and start Mule if it isn’t already started.

  4. Open the following URI in your browser: http://localhost:8087/services/cepExample

  5. You should see the page start to update periodically via AJAX as the example runs.

Building the example

Run mvn from the example source folder. This will compile the example classes, produce an application zip file and copy it to $MULE_HOME/apps.

Walking through the Mule Configuration

Initial Facts

<spring:bean name="companies" class="org.mule.example.cep.CompanyRegistry" factory-method="getCompanies" />

A Spring Factory Bean provides a Collection object which will be used as initial facts to assert into Drools' working memory at startup.

Declare Drools as the Rules Engine

<bpm:drools />

We declare Drools as the Rules Engine implementation, for more info. on this, refer to the Drools Module Reference.

AJAX Connector

<ajax:connector name="ajaxServer" serverUrl="http://0.0.0.0:8087/services/cepExample"         resourceBase="${app.home}/docroot" disableReplyTo="true" />

Declare the AJAX connector which will be used to update the Web UI.

In-memory Queues

<vm:endpoint name="stockTick" path="stock.tick" /><vm:endpoint name="alerts" path="stock.alerts" />

To keep things simple for the example, we define in-memory ("VM") queues to transport messages. If this were a real application, the incoming stock ticks would probably be received via a web service and the outgoing alerts would probably be delivered to a JMS queue or via IM/SMS/E-mail.

Generate stock tick

<flow name="generateStockTicks">    <quartz:inbound-endpoint jobName="eventTimer" repeatInterval="2000">        <quartz:event-generator-job>            <quartz:payload>tick-tock</quartz:payload>        </quartz:event-generator-job>    </quartz:inbound-endpoint>    <component>        <singleton-object class="org.mule.example.cep.TickFeed" />    </component>    <outbound-endpoint ref="stockTick" /></flow>

In this flow, we use Quartz to generate a periodic signal which triggers our TickFeed component. The TickFeed component simulates a new stock tick by reading in a line from a text file and putting it on the stockTick queue.

Process stock tick

<flow name="processStockTicks">    <inbound-endpoint ref="stockTick" />    <all>        <ajax:outbound-endpoint channel="/services/cepExample/stockTick" />        <bpm:rules rulesDefinition="broker.drl" cepMode="true" entryPoint="StockTick stream" initialFacts-ref="companies" />    </all></flow>

Here we receive the incoming stock ticks and simultaneously send them to two different places: 1. The Web UI (via AJAX) 2. The Rules Engine
For more info. on the configuration of the Rules Engine, refer to the Drools Module Reference.

Process stock tick

<flow name="sendAlerts">    <inbound-endpoint ref="alerts" />    <ajax:outbound-endpoint channel="/services/cepExample/alerts" /></flow>

The rules we have defined in Drools (see following section) will generate alerts on the Alerts queue. This flow simply receives those alerts and routes them to the Web UI via AJAX.

Walking Through the Rules Definition

The file broker.drl contains the rules definition for Drools. I will point out the part of this file which integrates with Mule, but it is beyond the scope of this example to walk through all of the rules defined there. For more information on how to create a rules definition file, please refer to the Drools Documentation.

Declare Mule callback inside Drools

global org.mule.module.bpm.MessageService mule;

Here we define a callback to Mule as a global variable within our rules definition file. We can then use this global callback to generate Mule messages from our rules.

Generate alert message

rule "sudden drop"when    ...cut...then    ...cut...    mule.generateMessage("alerts", msg, null, MessageExchangePattern.ONE_WAY);end

Here we use the global variable defined above to generate a Mule message and send it to the alerts endpoint (defined in our Mule configuration, above).