Drools is a versatile business rule engine that can elevate your application by externalizing a lot of decision logic. As the most application servers are now using Spring boot, let's understand how to use Drools with Spring boot.
For a simple example, you can create a KieContainer
as shown below.
@Bean
public KieContainer getKieContainer() {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write( ResourceFactory.newClassPathResource("discount.drl"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
Once you have a container, You can execute the rules within it by creating a session and firing all rules.
More on this explained at Drools integration with Spring Boot.
Top comments (1)
I've always just leveraged the
kmodule.xml
and let Drools load the various kbases from memory from files on the classpath. Simplifies things greatly, and your bean becomes just: