Search code examples
javadroolshigh-availability

Drools 7.x dynamically load rules at runtime without using maven


I want to load a drl file at runtime with high availability and versioning support.

The posts I've found work with older versions of Drools or with maven. Installing Maven on the docker pods won't be possible.

Related posts:

Drools 6.0 dynamically load rules at runtime

I need to add .drl files at runtime (From an S3 bucket) and supply it to drools rule engine

Ideas?

Thanks.


Solution

  • Drools supports loading rules from files, one alternative that we are using in a similar use case is managing rules in a Git repo.

    Component Start: Download the rules from git, and load them in a KieContainer.

    Example:

        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        // Rule from string
        gitRulesDao.getAllRulesFilesFromGitLab().forEach(r -> {
            try {
                addRuleToKie(kieFileSystem, kieServices, fileSystemBaseUrl, r);
            } catch (IOException | GitLabApiException e) {
                e.printStackTrace();
            }
        });
    
        // build
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        // Test rule
        Results results = kieBuilder.getResults();
        if (results.hasMessages(Message.Level.ERROR)) {
             ...
        }
        //
        KieModule kieModule = kieBuilder.getKieModule();
        return new 
        KieContainerLocal(kieServices.newKieContainer(kieModule.getReleaseId()));
    

    Rules Changes: When the rules are modified a git webhook sends a message to a Message Queue and the suscribed components reload the rules.

    KieContainerLocal is an structure that stores a KieContainer in memory.