Introduction
This post shows a reference implementation of drools persistence on top of a non transactional HashMap. This should serve as inspiration for more complex scenarios (like the Berkley DB one). This work was also developed under Intalio’s sponsorship.
As the new abstraction is heavily inspired on JPA there are some mechanisms which should be emulated in order to get the same behavior on both implementations.
Involved classes
Starting from the abstraction described in my previous post this is the new hierarchy for persisting drools runtime into a HashMap.
I’ll try to explain the most relevant objects in the diagram relating them to JPA components.
- MapBasedPersistenceContext
- behaves like the EntityManager, it stores all objects which are not yet commited. Finding objects is also resolved through this interface, for support this behavior it has to have access to the KnowledgeSessionStorage.
- KnowledgeSessionStorage
- represents the real persistent storage. This is the extension point for support any other non-JPA implementation. It provides saveOrUpdate and find methods and is the responsible to assign id’s to the entities.
- ManualTransactionManager
- as we cannot rely on JTA to manage our session Drools hooks explicit calls whenever things should be serialized. This component must access the NonTransactionalPersistenceSession to get the entities waiting for being persisted into the KnowledgeSessionStorage.
JBPM5
The same concepts have been applied into JBPM5 codebase and we have there proper extensions for managing process semantics.
You’ll find there:
- MapBasedProcessPersistenceContext
- ProcessStorage
- ManualProcessTransactionManager
- etc
Usage
You must setup the environment you gonna pass to the JPAKnowledgeService (still keeping the name for backward compatibility) setting the TRANSACTION_MANAGER and PERSISTENCE_CONTEXT_MANAGER keys. For this I have just created simple factories but you can build them the way you want.
I suggest using KnowledgeSessionStorageEnvironmentBuilder or ProcessStorageEnvironmentBuilder. There doesn’t exist a default implementation of this storage, the one on top of HashMap is only used for testing porposes, so you have to pass to the builder your own implementation of the desired storage.
Testing further implementations
To test this two implementations (and any new one which wants to respect the drools persistence semantic) together, I have created a simple set of abstract tests(*).
For Drools Expert:
https://github.com/droolsjbpm/droolsjbpm/[...]/MapPersistenceTest.java
For JBPM5
https://github.com/krisv/jbpm/[...]/MapPersistenceTest.java
In a future will be great to abstract the current tests on JPA to be able to run abstract of the lower persistence layer.
What’s next
Clean up this interfaces a bit more and start working on a DB Berkley implementation
(*) tests names are not declarative enough, I’m holding a commit for this until coming repository split is done

[...] This post was mentioned on Twitter by Esteban Aliverti, Diego López León. Diego López León said: Drools persistence on top of HashMap http://wp.me/pT9x2-2d [...]