4
votes

I have a Java Drools app running 7.4.1.Final version, with a .drl file with its rules. My application allows generating a new .drl file at runtime, or modify the current .drl file.

I want to run this new drl file without to relaunch again my app. I know that if I relaunch again, it loads the new version of the rules file, but I want to avoid to stop the execution of the app. My application is a .jar application that I run using java -jar command. In the code below CTES.Rules contain the file system with the .drl file.

Is it possible?

I attach the code that I use to load my rules file:

private void build() throws Exception {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.write(ResourceFactory.newFileResource(new File(CTES.RULES)));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
    KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
    KieSessionConfiguration conf=  SessionConfiguration.newInstance();
    kSession = kieContainer.newKieSession(conf);
    if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
        List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
        StringBuilder sb = new StringBuilder("Errors:");
        for (Message msg : errors) {
            sb.append("\n  " + msg);
        }
        throw new Exception(sb.toString());
    }

    System.out.println("KieServices built: ");
}

UPDATE I have read about KieScanner and also about KieModule. But I don't have clear how to include new released in my app or how use KieScanner.

What I have to change in this code to allow the load of a new version of the .drl file (in CTES.RULES) without to stop my application?

Thanks

2

2 Answers

1
votes

You can use KieScanner to load rule dynamically. KieScanner uses local maven repository to dynamically load KieModule. You will get the details here

1
votes

Posting for latest solution- We are using drools with Spring boot, Spring cloud config, we were able to reload the rules in runtime using /actuator/refresh endpoint with @refreshscope on the @kiecontainer bean.

We have yml like below:

rules:
    app1:
      program1:
          rulesetkbasename
          rulesetkbasepackage
          rulesetfilelocation
          rulesetfiles
          rulesetKsessionname
    app2:
      program2:
          rulesetkbasename
          rulesetkbasepackage
          rulesetfilelocation
          rulesetfiles
          rulesetKsessionname