DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • GenAI: From Prompt to Production
  • SAP HANA Triggers: Enhancing Database Logic and Automation
  • GenAI: Running Prototypes Faster Than Wireframes
  • Business Logic Database Agent

Trending

  • Secure IaC With a Shift-Left Approach
  • The Synergy of Security and Development: Integrating Threat Models With DevOps
  • Finding Needles in Digital Haystacks: The Distributed Tracing Revolution
  • Master SQL Performance Optimization: Step-by-Step Techniques With Case Studies
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Integrating Drools With Mule ESB

Integrating Drools With Mule ESB

Drools is a Business Rules Engine with its own rules management application and Eclipse IDE plugin for core development. Read about its advantages.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jun. 17, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.8K Views

Join the DZone community and get the full member experience.

Join For Free

1.0 Overview

Drools is a Business Logic Integration platform (BLiP). It is written in Java. It is open source and backed by RedHat and JBoss. It is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), web authoring, and rules management application (DROOLS Workbench) and an Eclipse IDE plugin for core development. It is one of the best open source rule engines providing Complex Event Processing.

Facts are stored in working memory (like an in-memory database). Fact is a piece of data, such as salary=3000 or, in an object-oriented rules engine, it may be a Java object with properties.

Rules are defined in a knowledge base. A rule consists of conditions (which typically depend on facts in the working memory) and actions which are executed when the conditions are true (similar to an "if-then" statement).

2.0 Advantages of the Drools Rules Engine

  • Reusability: The rules are kept at a centralized location (separation of business logic from the rest of the system); it is easy to reuse the Drools rules engine.

  • Flexibility: Changing the application or its model is not an easy task, but it is easier to change the rules than Java code.

  • Redeploying: It is easy to redeploy modified rules without stopping or modifying the application logic.

  • Easier to Understand: It is easier to understand the rules for new developers than code written in Java or any other languages.

  • You can avoid a lot of if-else from your actual business logic by separating or implementing business rules separately.

  • Unifying: The Drools platform brings unification of rules and processes. It is easy to call rules from a process or vice versa.

  • Easy to implement complex logic using the Drools Rules Engine.

3.0 Considerations for Using the Drools Rule Engine

Let consider that Drools should be used when your business logic is very complex. Apart from complex business logic, you can use Drools in the below situations:

  • Application business logic often changes.

  • The business logic contains a lot of If-Else statements.

  • The business logic is overly complex or time-consuming when defined procedurally.

  • The business logic needs to be maintained by people who don’t (or shouldn’t) have access to the application itself (to recompile/redeploy it).

  • Domain experts (or business analysts) are readily available, but are nontechnical.

4.0 Business Use Cases

Today we will implement a business use case such as a life insurance policy; depending on age and policy type, we will be providing a premium amount. Below is the logic for our business case:

  • If age < 10 and policyType = "Policy1", then premiumAmount = 20000.

  • If age is between 10 and 40 and policyType = "Policy1", then premiumAmount = 30000.

  • If age > 40 and policyType = "Policy1", then premiumAmount = 40000.

  • If age < 10 and policyType = "Policy2", then premiumAmount = 21000.

  • If age is between 10 and 40 and policyType = "Policy2", then premiumAmount = 31000.

  • If age > 40 and policyType = "Policy2", then premiumAmount = 41000.

Our flow will receive the below JSON message and we will use JSON data to pass through our rules engine.

{
"name":"Joseph",
"age":30,
"policyType":"Policy1"
}

Once the JSON message has passed through the rules engine and our expected output is shown below:

{
"name":"Joseph",
"age":30,
"policyType":"Policy1"
"premiumAmount":30000
}

5.0 Implementing Drools With Mule ESB

Now, we will walk through how to integrate the Mule ESB and Java rules engine, namely Drools.

Create a Mule project and name the project droolsexample.xml; you can select Mule runtime 3.6 or higher.

5.1 Namespace and Schema

The first thing we need to add is the namespace and schema location to the Mule configuration file (e.g. droolsexample.xml).

<mule xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/bpm 
 http://www.mulesoft.org/schema/mule/bpm/current/mule-bpm.xsd"></mule>

5.2 Initialize BPM Rules

Add the below tag in droolsdemo.xml and it will tell the Mule flow there are rules defined in your flow and it will start Drool's working memory.

<bpm:drools/>

5.3 Defining Mule Flow With Drools Rules

We will be receiving the JSON message and it will be converted into a Java object using the JSON To Object transformer. Our Java class for Insurance will look like this:

package com.drools.example;

public class Insurance {

private String name;
private int age;
private String policyType;
private int premiumAmount;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name=name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age=age;
}

public String getPolicyType()
{
return policyType;
}

public void setPolicyType(String policyType)
{
this.policyType=policyType;
}

public int getPremiumAmount()
{
return premiumAmount;
}

public void setPremiumAmount(int premiumAmount)
{
this.premiumAmount=premiumAmount;
}


}

Add file with .drl extension to your classpath (src/main/resources). This file will contain all rules in Plain Text, which can be easily modified and loaded at runtime if needed.

package com.drools.example;
import org.mule.MessageExchangePattern;
import com.drools.example.Insurance;
global org.mule.module.bpm.MessageService mule;
dialect "mvel"

declare Insurance
@role('event')
end

rule "Policy 1 And Infant"
lock-on-active when
$insurance: Insurance(age < 10 && policyType == "Policy1") then
modify($insurance) {
setPremiumAmount(20000)
}
end

rule "Policy 1 And Adult"
lock-on-active when
$insurance: Insurance((age >= 10 && age <= 40) && policyType == "Policy1") then
modify($insurance) {
setPremiumAmount(30000)
}
end

rule "Policy 1 And Older"
lock-on-active when
$insurance: Insurance(age > 40 && policyType == "Policy1") then
modify($insurance) {
setPremiumAmount(40000)
}
end

rule "Policy 2 And Infant"
lock-on-active when
$insurance: Insurance(age < 10 && policyType == "Policy2") then
modify($insurance) {
setPremiumAmount(21000)
}
end

rule "Policy 2 And Adult"
lock-on-active when
$insurance: Insurance((age >= 10 && age <= 40) && policyType == "Policy2") then
modify($insurance) {
setPremiumAmount(31000)
}
end

rule "Policy 2 And Older"
lock-on-active when
$insurance: Insurance(age > 40 && policyType == "Policy2") then
modify($insurance) {
setPremiumAmount(41000)
}
end

In the above rule file, we have defined import namespaces and packages statements. Then, we create the Insurance object to be used by our rule engine and followed by some rules with conditions that explained above.

5.4 Define Spring Beans

You need to define spring beans in your configuration file  (e.g. droolsexample.xml) as shown below

<spring:beans>
<spring:bean name="NoFactsBean" class="java.util.ArrayList" />
</spring:beans>

5.5 Call Rule Definition in Mule Flow

Rule defintions can be called in Mule Flow by defining the below statement in the configuration file (droolsexample.xml):

<bpm:rules rulesDefinition="insuranceRules.drl" 
           initialFacts-ref="NoFactsBean" doc:name="BPMRules" />

5.6 Mule Flow [Code]

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:wmq="http://www.mulesoft.org/schema/mule/ee/wmq" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/wmq http://www.mulesoft.org/schema/mule/ee/wmq/current/mule-wmq-ee.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/current/mule-bpm.xsd">
    <bpm:drools/>
<spring:beans>
<spring:bean name="NoFactsBean" class="java.util.ArrayList" />
</spring:beans>
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="mule-proj-flowFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/drools" allowedMethods="POST" doc:name="HTTP"/>
        <wmq:object-to-message-transformer doc:name="Object to Message"/>
        <json:json-to-object-transformer returnClass="com.drools.example.Insurance" doc:name="JSON to Object"/>
        <bpm:rules rulesDefinition="insuranceRules.drl" initialFacts-ref="NoFactsBean" doc:name="BPMRules" />
        <set-payload value="#[payload.object]" doc:name="Set Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

Image title6.0 Testing

We will use Postman to test all the above scenarios, and hopefully, we will get the expected result back.

Image title

Image title

Image title

7.0 Integrating Drools With Mule ESB [Video]


8.0 Conclusion

Mule provides easy steps to integrate with DROOLS, as explained in above article. I hope you find this article is interesting and provides the required information to integrate DROOLS with Mule Flow. 

Enterprise service bus Drools Business logic

Opinions expressed by DZone contributors are their own.

Related

  • GenAI: From Prompt to Production
  • SAP HANA Triggers: Enhancing Database Logic and Automation
  • GenAI: Running Prototypes Faster Than Wireframes
  • Business Logic Database Agent

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: