Search code examples
javadroolskie

KieScanner not updating KieSessions at runtime


I'm using Drools with Eclipse and Maven for an application doing many-many pattern matching. I wanted to use KieScanner to automatically update the running KieSession without restarting the application. But this doesn't seem to be working.

I am using 7.24.0.t043 for org.kie and org.drools.

I'm only using my local Maven repo for this, so I've specified the path and set true in my settings.xml. I also verified that creating a new KieContainer with the most recent version registers the changed rules. KieScanner.scanNow() doesn't seem to work for me. I've looked through the error logs and there doesn't seem to be any errors at all, everything is running fine. I'm pretty sure I'm also packaging each version correctly into kjars and installing them in my local Maven repo. Additionally, I'm putting my new kjar version as 2.0.0-SNAPSHOT.

kContainer.updateToVersion() works for me only when I call it at the beginning of my runner class during KieSession setup, but does not work if I call it in the while loop below.

Here I am setting up my KieSession in my runner class:

KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("com.app", "my-app", "1.0.0");
KieContainer kContainer = ks.newKieContainer(releaseId);
KieScanner kScanner = ks.newKieScanner(kContainer);
kScanner.scanNow();
KieSession kSession = kContainer.getKieBase().newKieSession();

After setting this up, I insert my objects into kSession and call fireAllRules() once.

I'm using a while loop to print out the changes done to my object (which I've inserted into the kSession):

while (true) {
  System.out.println("Matches of a1: " + a1.getMatches());
  TimeUnit.SECONDS.sleep(10);
}

I expect printing out the updated matches after the updated rules, however the actual printing out is not changing the matches.

Pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.app</groupId>
    <artifactId>genentech-maven-poc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Genentech Maven POC</name>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <runtime.version>7.24.0.t043</runtime.version>
    </properties>
    <repositories>
        <repository>
            <id>local-repo</id>
            <name>Local Maven Repo</name>
            <url>file://Users/mtsiang/.m2/repository</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-ci</artifactId>
            <version>${runtime.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Please let me know if you need more information from me, I'm not really sure what else I'd need to include for you to get a sense of the problem.


Solution

  • So, I found the reason why.

    When specifying the KieContainer creation and releaseId, make sure to use "LATEST" as in:

    ReleaseId releaseId = ks.newReleaseId("com.app", "my-app", "LATEST"); //instead of "1.0.0"
    KieContainer kContainer = ks.newKieContainer(releaseId);
    

    This way, KieScanner will read in the <release></release> tag in the Maven repo metadata.xml file.

    To fire the new rules, you'd also need to call kSession.update(FACTHANDLE, OBJ); and then kSession.fireAllRules();.