Load dynamic rules for stateful KieSessions in SpringBoot

949 views
Skip to first unread message

Amit Karandikar

unread,
Apr 12, 2016, 3:36:21 PM4/12/16
to Drools Usage
Hello

I'm trying to find information around how I can configure my SpringBoot application to load rules for a stateful KieSession  that have changed via a change propagated through the KIE workbench. I have the code that does most of it, but some questions still remain. The following snippet will likely help the conversation - 

@Inject
@KContainer(name = "dynamic-rules-container")
@KReleaseId(groupId = "com.org", artifactId = "ops", version = "LATEST")
private KieContainer kieContainer;

private void executeOpsRules() {

        // Get the latest release
        ReleaseIdImpl releaseId = new ReleaseIdImpl("com.org", "ops", "LATEST");
        kieContainer.updateToVersion(releaseId);
        KieSession kSession = kieContainer.newKieSession("ops-session");

if ( kSession!=null ) {
            // Get fact data
           ....
           // Add facts
           kSession.insert(factA);
           kSession.insert(factB);
            
           // Fire rules
           kSession.fireAllRules();    
       }
}

Firstly, is there a way for me to avoid the "updateToVersion" call? KIE-CI should be able to handle this if its in the application's classpath is what I read. But it doesn't seem to do this, making it necessary for me to invoke this on the container. Also, the maven terminology of picking up latest version deployed via the "LATEST" version value does not take hold even if the Rule deployment environment in workbench has a scanner setup to run ever so often to pick up the latest version of the release. Feels like a bug, since I actually have to give an exact version everywhere for the above to work. At this point I've gotten around the whole issue by actually putting "LATEST" as part of the Project container version. 

The other part seems to be that I do not get a handle to a stateful kieSession. Though it is marked as stateful, the facts that were inserted earlier like in the above "factA" & "factB" do not show up the next time those rules are fired on that session - because it looks like I get a handle to a new stateful session, even though I'm not disposing the old session. Also, there doesn't seem to be a way to get the old session through the APIs (like possibly using ID which is now deprecated ). Only way around this has been to create the KieSession as a singleton bean in Spring but that prevents rules loading dynamically as initialization will not get called every time for "updateToVersion" to be invoked.

Appreciate any guidance or pointers on this at this point....

Thanks
Amit


Amit Karandikar

unread,
Apr 14, 2016, 2:24:24 AM4/14/16
to Drools Usage
On further deep dives, it looks like this seems to be an issue with CDI based KieSession injection. Such KieSessions do not update even though the scanner gets kicked off via a push from the workbench. The only way I've resolved this is by creating a spring bean method that starts up a scanner and polls for a change. Method below - though I would expect the push via the Kie Workbench -> Rule deploy option to do the same on the Kie Execution Server. 

    @Bean(name="rules-session")
public KieSession getKieSession() {
    KieServices kieServices = KieServices.Factory.get();
            ReleaseId releaseId = kieServices.newReleaseId("com.org", "ops", "LATEST");

           // make sure you use "LATEST" version
           KieContainer kieContainer = kieServices.newKieContainer(releaseId);
           KieSession kSession = kieContainer.newKieSession("dynamic-session");

           // Poll every 30 seconds
           KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
           kieScanner.start(30000L);

   return kSession;
}

Surprised at the lack of response on this so far. Would appreciate any feedback. 

Thanks
Amit
Reply all
Reply to author
Forward
0 new messages