java - Drools 无法与 Spring Boot 一起正常工作

标签 java spring spring-boot drools

我想将 Drools 与 Spring Boot 一起用于 Bean 验证,但我已将问题缩小到这几行代码:

主类

@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 是一个只有 id、名字和姓氏以及 getter 和 setter 的实体。

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>

还有两条规则

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

调用SpringApplication.run(App.class, args)(如上)时一切正常:

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

但是,当我将 SpringApplication.run(App.class, args) 添加到 main 时,只会触发一个规则:

This rule is always fired

甚至 org.drools.core.common.DefaultAgenda 的日志记录也不再可见。

我不知道出了什么问题?我希望两种情况下的输出相同。 SpringBoot 是否在后台执行某些操作?

最佳答案

我知道这个问题已经有了可接受的答案,但我觉得在这里声明 删除 spring-boot-devtools(Maven 依赖项,在我的例子中),错误消失了。

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

关于java - Drools 无法与 Spring Boot 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898186/

相关文章:

java - JPA服务层事务

java - 从 Java 中的 Amazon s3 中删除对象列表

spring - 如何覆盖 dispatcherServlet 中的 contextConfigLocation

spring-boot - Azure 表和 Blob 测试

spring-boot - 使用 Spring Boot 2 和 Spring Security 5 进行多因素身份验证

java - 单击 JTable 时文本字段焦点丢失和获得

java - 获取找不到符号错误。数组列表、迭代器

Spring集成ErrorChannel问题

java - 仅使用 RabbitMQ 和 SpringAMQP 消费具有特定 header 的消息

spring - 如何将 OAuth2 session 存储到数据库中并在 Spring Boot 服务器之间共享