Batch Core - Dynamic Business Rules
In this Java batch series I hardcoded the business rules in the program. This may be ok for some cases but in our fast moving world having the ability to change business rules without changing the program is a big plus.
And it is not only about having the end user change the business rules. I wouldn’t go that far in my first step. Having business rules extracted to a text file or a database is already a big plus because you can instantly change a business rule without too much fuzz.
Does the new or changed business rule needs some kind of testing?
Yes, definitely! But we don’t need to compile and test the whole program, just the business rule.
So how to implement that?
Implementation
Having such a mature programming language as Java has its advantages. There probably is already a solution to your problem as most developers don’t face brand new problems. Or there is a solution which can be modified so it fits your problem.
And that is also the case here. Dynamic business rules can be easily implemented using the Jakarta Expression Language. Most have heard of Jakarta EL (Expression Language) in conjunction with Java Server Faces and Java Server Pages. But it can also be used stand alone, outside of Jakarta EE servers.
Eclipse Expressly is the reference implementation of the Jakarta EL specs. And if we are taking a look at the dependencies of Expressly we astonishingly see only a single dependency – the EL API. That means we can use Expressly in many scenarios and environments.
User Defined Context
Mostly there are two ways to use Jakarta EL.
- evaluate an expression and use the result of the evaluation for further processing
- execute an expression and do every processing inside the expression
If we evaluate an expression and work with the result that would mean that we need to hard code what to do with the result. That would remove some of the dynamics we get from using an expression language.
The solution is … providing your own context (or rather your own ELResolver class). That means
we can add our own objects to the expression context so that our own objects are available in the
expression and thus can be queried and changed (despite ChatGPT saying that this is not possible,
so much for our glorious AIs 😄).
ChatGPTs answer:
❌ Not possible in EL
You cannot do this:
- loop + modify entity field
- assign values in EL
- create mutable counters in EL
because EL has no assignment operators.
But I can do this:
1 | entity.packagingWorkplace = entity.line == 5 ? 5 : entity.packagingWorkplace |
… which assigns a value to a field by using an operator. AI is so ridiculous.
Business Rules Expressions
Our initial business rules from the first Java Batch post were the following:
- fragile product → workplace 4
- oversize product → workplace 3
- line 5 (hall 2) → workplace 5
- else → distribute evenly between workplace 1 and 2
Rule 1 to 3 are very easily translated to the following Jakarta EL expressions:
1 | entity.packagingWorkplace = entity.fragile ? 4 : entity.packagingWorkplace |
But the fourth rule is a bit more complicated as we need to save some state (the last workplace assignment for that case). This can be solved in various ways. I chose to add a counter object to the context.
1 | private Map<Object, Object> userContext = new HashMap<>(); |
The following expression queries and increments the counter:
1 | entity.packagingWorkplace = ((counter.getAndIncrement() mod 2) + 1) : entity.packagingWorkplace") |
But we only want to use this expression as the last resort if no other expression matched.
1 | entity.packagingWorkplace = entity.packagingWorkplace == NULL ? ((counter.getAndIncrement() mod 2) + 1) : entity.packagingWorkplace") |
Note: Using the ternary operator “?” is a workaround for the lack of an if/else construct in the Jakarta Expression Language.
Performance
I was positively surprised by the performance of Expressly. I had a database table with around 850k records. I read the whole table and executed a single expression on every record. The whole processing was done in under 15 seconds. Kudos to the developers of Jakarta EL and Expressly.
Source Code
I have uploaded an example project at Bitbucket. Feel free to take a look at it. I added various branches to the Git repository which reflect my various steps and stops of my journey.
Wrap Up
Turning the program from hardcoded business rules into using configurable user defined business rules was much easier than anticipated and the real surprise for me was the very good performance. Again it shows that Java is a really great programming language with its excellent libraries. One can achieve so much with so little effort.
Happy new year! 🎉
Mihael