axnet
axnet

Reputation: 5790

How to avoid creating new kiesession on each drools rule-fire?

how can i re-use the session with updated KieFileSystem?

below is code which i am using

  private static KieSession getNewKS(File dir) {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    dir.listFiles().forEach(file -> kfs.write(ResourceFactory.newFileResource(file)));
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    KieModule km = kb.getKieModule();
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    return kc.newKieSession();
  }

Upvotes: 0

Views: 635

Answers (1)

Toshiya Kobayashi
Toshiya Kobayashi

Reputation: 705

Your getNewKS method contains "Build resources (kb.buildAll())" which is a heavy process. You should want to call it only when you add/modify resources. On the other hand, creating a new KieSession is a lightweight process. Creating a new KieSession on every request is a normal usage. You can repeatedly create new KieSessions from the same KieContainer. I recommend to measure elapsed times.

Here is an example, I hope it helps

https://github.com/tkobayas/kiegroup-examples/tree/master/Ex-KieFileSystem-considering-cache-7.73

At this point, you don't need to consider "re-use ksession". Actually, KieContainer has KieSessionPool but it would be required for finer tuning scenario.

Upvotes: 1

Related Questions