DEV Community

Cover image for Drools integration with Spring Boot
Raja Anbazhagan
Raja Anbazhagan

Posted on • Originally published at springhow.com

Drools integration with Spring Boot

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());
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
roddy profile image
Roddy

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:

@Bean
public KieContainer kieContainer() {
    KieServices kieServices = KieServices.Factory.get();
    return kieServices.getKieClasspathContainer();
}
Enter fullscreen mode Exit fullscreen mode