问题描述
学习流口水的第一天.我遇到了这个奇怪的问题.
On my first day to learn drools.I got this weird problem.
规则" Hello World "不能在Controller中运行,但在Junit测试用例中可以正常运行.规则"另一个规则"始终在Controller和Junit测试上运行.
rule "Hello World" in drl cannot run in Controller,but working well in Junit test case.rule "Another rule" always run on both Controller and Junit test.
控制器和junit测试中的代码完全相同.
the code in controller and junit test are same totally.
欢迎有想法的人.谢谢.
Anyone who have idea is welcome.thanks .
Sample.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:
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:
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:
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:
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());
}
}
推荐答案
最后,一个朋友帮助了我.这是因为热部署".关闭它,然后问题就解决了.
Finally a friend helped me.It is because the "hot deployment".close it ,then problem solved.
它在pom.xml中:
It is in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
这篇关于Drools规则在Spring Boot Controller中不起作用,但在Junit Test中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!