简体   繁体   中英

Drools not working correctly with Spring Boot

I want to use Drools with Spring Boot for Bean Validation but I have narrowed the problem down to this few lines of code:

The Main-Class

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        //SpringApplication.run(App.class, args);
        check();
    }

    public static void check() {
        // load up the knowledge base
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");
        //go
        Patient patient = new Patient("Hans", "Mueller");
        kSession.insert(patient);
        kSession.fireAllRules();
    }
}

Patient is an Entity with just an id, firstname and lastname with getter and setter.

The kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>

and two rules

package com.sample

import com.sample.Patient;

rule "Test"
    when
        eval(1 == 1)
    then
        System.out.println("This rule is always fired");
end

rule "Patient"
    when
        exists Patient()
    then
        System.out.println("Patient found");
end

When not calling SpringApplication.run(App.class, args) (like above) everything works fine:

15:50:12.730 [main] DEBUG org.drools.core.impl.KnowledgeBaseImpl - Starting Engine in PHREAK mode
15:50:12.820 [main] DEBUG org.drools.core.common.DefaultAgenda - State was INACTIVE is nw FIRING_ALL_RULES
15:50:12.821 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
This rule is always fired
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
Patient found
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - State was FIRING_ALL_RULES is nw HALTING
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - State was HALTING is nw INACTIVE

However, when I add SpringApplication.run(App.class, args) to the main, only one rule is fired:

This rule is always fired

Not even the logging of org.drools.core.common.DefaultAgenda is visible anymore.

I have no idea whats going wrong? I expect the same output in both situations. Is SpringBoot doing something in the background?

I know the question already has its accepted answer, but I feel it would be nice to state here that after removing the spring-boot-devtools (Maven dependency, in my case), the bug is gone.

Reference: "No rules are fired in helloworld using Spring rest controller!" on Google groups .

For everybody that has the same problem and reads my question:

I haven't solved it directly but the problem has something to do with netbeans. Everything works fine when starting the application via command line. So this is the way to go.

I added below lines in kmodule.xml . it worked in eclipse too. <kbase name="rules" packages="rules" e ventProcessingMode="cloud" equalsBehavior="equality" declarativeAgenda="enabled" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM