Alexander Alexander - 1 year ago 187
Java Question

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download