| Sign In/My Account | View Cart |
These days enterprise Java could almost put you to sleep. How many hundreds of J2EE-EJB web applications have been written that capture information from a web page and store it in a database? What really keeps developers awake at night is trying to write and maintain the complex business logic in their applications. This is a problem not only for new applications, but increasingly, for long-lived, business-critical apps whose internal logic needs to change frequently, often at very short notice.
In an earlier article, "Give
Your Business Logic a Framework with Drools," I introduced the
Drools framework and showed how it could be used to organize
complicated business logic. Drools replaced many tangled
if ... then statements with a simple set of things known
to be true. If you are ever in a meeting with business customers,
and your head hurts with the complexity of what they want you to
implement, then maybe you should consider a rule engine such as
Drools. This article will show you how you can do this in an
enterprise Java application.
Most enterprise Java developers already have their favorite frameworks. In no particular order, these include presentation frameworks (Struts, JSF, Cocoon, and Spring), persistence frameworks (JDO, Hibernate, Cayenne, and Entity Beans) and structural frameworks (EJB, Spring again, Pico, and Excalibur), as well as many others. Each framework does one very useful thing (or more), and gives developers a lot of instant "out of the box" functionality. Deploying an application using frameworks means you avoid a lot of the boring bits and concentrate on what is really needed.
Until now, there was a gap in what the frameworks were able to
do, in that business logic had no framework. Tools like EJB and
Spring are good, but have little to say about how to organize your
if ... then statements! Adding Drools to your developer
toolbox means that it is now possible to build an application with
"frameworks all the way down." Figure 1 shows a diagram of such an
application.

Figure 1. Frameworks for Java
applications
This article will build on what we already know of the Drools framework and allow us to build such an application.
|
Related Reading
Better, Faster, Lighter Java |
It's almost a cliche in software engineering to say that "if you have a hammer, everything looks like a nail." While rule engines can solve a lot of problems for us, it is worth considering whether a rule engine is really appropriate for our enterprise Java application. Some questions to ask are:
If you're writing an enterprise application, chances are that it will need to scale to hundreds, if not thousands, of users. You know that existing Java and J2EE applications can do this, but how will a application using Drools cope with this pressure? The answer is "surprisingly well." While most developers hate to "lose control" and rely on other people's code (i.e., a framework), consider the points below--not only should your application be as fast as "traditional" coding methods, but Drools may even make your application run faster:
if ... then statements with an
optimized network. It is important to note that the Rete algorithm
involves a tradeoff between using more memory to reduce delays at
run time. While this isn't a factor in most modern servers, we
wouldn't yet recommend deploying Drools on your mobile phone!