gpt4 book ai didi

java - Drools 规则在 Spring Boot Controller 中不起作用,但在 Junit Test 中起作用

转载 作者:行者123 更新时间:2023-12-01 07:02:01 25 4
gpt4 key购买 nike

在我学习流口水的第一天。我遇到了这个奇怪的问题。

drl 中的规则“Hello World”无法在 Controller 中运行,但在 Junit 测试用例中运行良好。规则“另一条规则”始终在 Controller 和 Junit 测试上运行。

Controller 和junit测试中的代码完全相同。

欢迎任何有想法的人。谢谢。

<小时/>

示例.drl:

package com.happylifeplat.checkin

import com.happylifeplat.checkin.managerorder.beans.RaBean1;

rule "Hello World"
when
$h : RaBean1( id == 1)
then
$h.setContent("from drl content");
System.out.println("-----Hello World rule called id == 1");
end

rule "Another rule"
when
then
System.out.println("-----Another rule called");
end

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>

RaBean1.java:

package com.happylifeplat.checkin.managerorder.beans;

public class RaBean1 {
private int id;
private String content;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

HelloController.java:

@RestController
@RequestMapping("/hello")
public class HelloController {

private static KieContainer kieContainer;
private KieSession sessionStatefull = null;

@RequestMapping(value = "/helloworld", method = RequestMethod.GET)
@ApiOperation(value = "hello")
public Result metadata() {
try {
if (kieContainer == null) {
kieContainer = KnowledgeSessionHelper.createRuleBase();
}
sessionStatefull = KnowledgeSessionHelper.getStatefulKnowledgeSessionWithCallback(kieContainer, "ksession-rules");
RaBean1 bean1 = new RaBean1();
bean1.setId(1);
bean1.setContent("default content");
sessionStatefull.insert(bean1);
sessionStatefull.fireAllRules();
return new Result(CommonCode.sussess, bean1.getContent());
} catch (Exception e) {
return new Result(CommonCode.fail, null);
}
}
}

HelloControllerTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ImportResource({"classpath:spring/applicationContext.xml"})
@IntegrationTest("server.port=0")
@WebAppConfiguration
public class HelloControllerTest {
private static final Logger log = LoggerFactory.getLogger(HelloControllerTest.class);
private MockMvc mockMvc;
private static KieContainer kieContainer;
private KieSession sessionStatefull = null;

@Before
public void setUp() throws Exception {
// mockMvc = MockMvcBuilders.standaloneSetup(managerOrderController).build();
}

@Test
public void helloTest() throws Exception {
if (kieContainer == null) {
kieContainer = KnowledgeSessionHelper.createRuleBase();
}
sessionStatefull = KnowledgeSessionHelper.getStatefulKnowledgeSessionWithCallback(kieContainer, "ksession-rules");
RaBean1 bean1 = new RaBean1();
bean1.setId(1);
bean1.setContent("default content");
sessionStatefull.insert(bean1);
sessionStatefull.fireAllRules();
System.out.println("rabean.getContent---->"+bean1.getContent());
}
}

最佳答案

终于有 friend 帮我了。就是因为“热部署”。关闭它,问题就解决了。

它在 pom.xml 中:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

关于java - Drools 规则在 Spring Boot Controller 中不起作用,但在 Junit Test 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com