Mule with Drools

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Before we start the topic, let’s see what Drool is :-

Drools : Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE).
Drools supports the JSR-94 standard for its business rule engine and enterprise framework for the construction, maintenance, and enforcement of business policies in an organization, application, or service.

So, now the question is how to integrate Drool  with our Mule application.

How to use Drools in Mule ??

Mule have Drools component to execute business rules in it’s application.

Untitled

We generally consider Drool , when business logic is overly complex or time-consuming, or business logic often changes with time or even when business logic contains a lot of if-then-else statements.

Also, there are cases when business logic needs to be maintained by people (business related people) who don’t (or shouldn’t) have access to the application itself, in that case it’s ideal for implementing rules using Drool . In this way we can externalize all the complex rules for our application which can be created or modified by business related people, who don’t need to depend on programmer or developer to change the application logic.

In this way, Mule messages can be generated within the Mule application as a result of Rules firing.

Now, let us create a simple application in our Anypoint studio. This application we will be deploying and running in our Mule Standalone Server and will be using Business rules in our Mule application using Drools.

Untitled

We need to keep a .drl files in our application where all the business rules will be defined .. In our case all the business rules are in this routingRules.drl file:-

Untitled

Rules in the routingRules.drl are defined as follows:-

Untitled

So we can see the rules that if weight variable value is less than or equal to 50 then WAREHOUSE_B will be as printed as destination or else WAREHOUSE_A will be printed

We can see Mule code that the variable weight is generated randomly and the value of the weight is been checked by the Drools component which is referring routingRules.drl where all the rules are set :-

Untitled

Testing our application

Now we test our application by hitting http://localhost:8082/example and we will get the following:-

Untitledwe can see the value in weight variable is generated randomly and the destination value is set to WAREHOUSE_B as defined in .drl file.

Now we hit the url http://localhost:8082/example again and we will get the following:-

Untitled

You can see the value in weight variable is again generated randomly and the destination value is set to WAREHOUSE_A as the weight value is greater than 50 and the rules are defined in .drl file.

Conclusion

That’s it !! We can see you can arrange all the complex rules outside the application in .drl file, which can be created or modified  and application logic or code need no change even if the business rules need to be changed as per requirement every time.

So, this is the simple implementation of a Mule application with Drool  where we can fit all our business logic with our application without changing our application.

I hope you liked this post !!!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>