Making a per-request DROOLS engine use data from a singleton engine in kie workbench

34 views
Skip to first unread message

Steve Schneider

unread,
Apr 24, 2024, 8:45:17 PMApr 24
to Drools Usage
Hi,
We have several per-request DROOLS rule engines that get called for various types of requests in our system. We have some large global data that is stored in a database that we would like to load into a global variable for a singleton DROOLS engine that the others would use so that we wouldn't need to send all the data with each request.

All these engines will run on the same Kie workbench.

I tried creating and including a singleton project as an "Included KIE base" for a per-request project, but when each request was sent, it would initialize the data each time. It was storing its data in a global variable of the singleton.
  I found (from Chat GPT) that including a singleton project in a per-request project changes its nature so it is treated like a per-request. (Chat GPT seems to make suggestions that don't always work in the DROOLs world.)

It looks like I should probably avoid making an actual API call because all the data would be sent in a request. Since both engines are on the same kie workbench, is there a good way to share the data so that it is loaded once and used by other projects efficiently?

Do I need to consider using a JBPM with rule tasks? I prefer to not have to rearchitect existing production per-request DROOLS engines and would like to keep things as simple as possible.

Thanks in advance,
  Steve

Richard Bourner

unread,
Apr 24, 2024, 11:46:37 PMApr 24
to Drools Usage
The PER_REQUEST, PER_PROCESS_INSTANCE and SINGLETON (defined in the deployment-descriptor.xml file) are strategies only valid for the process engine (jBPM), not for the business rule engine (Drools).
So if you are using Drools APIs only, then the strategy is ignored.
What you seem interested in using is defined in the kmodule.xml and consist in setting your named KieSession instance as stateful rather than stateless. When doing so, you can use the same session instance for multiple runs, after eventually doing some clean-up between 2 runs, keeping only the global data in it for the next run.
But it is not going to solve your requirement of sharing data across multiple sessions. For this you need to implement some shared cache mechanism accessible from all KieSession instances and which would be loaded once, either at startup or the very first time it is being invoked.
I would not rely too much on ChatGPT for this kind of very specific request, I also got strange results in the past.

Steve Schneider

unread,
May 3, 2024, 6:20:22 PMMay 3
to Drools Usage
Thanks Richard!
It took me a while to mull this over and process it.
It seems like this may be the best way, although we have individual financial transactions, and there is some fear that switching to a stateful approach could allow the possibility (maybe theoretically) of one person's financial information not being cleared if, say, an exception is thrown during rule processing. Maybe I just need to go stateful and try to clear the session data, other than globals, after each transaction and be very careful about it.

Does anyone know of a good way to implement the shared cache mechanism accessible from all KieSession instances? We are currently using a Kie workbench, but in the future, if we upgrade to some more modern approach (Kogito?), I wouldn't want to have to rework it.
Thanks,
  Steve
Reply all
Reply to author
Forward
0 new messages