Drools 规则在 Spring 引导控制器中不起作用,但在 Junit 测试中起作用
Drools rule does't working in Spring Boot Controller but working in Junit Test
第一天学习 drools.I 遇到了这个奇怪的问题。
规则“Hello World”在 drl 中不能在 Controller 中 运行,但在 Junit 测试用例中运行良好。
控制器和 Junit 测试中的规则“另一个规则”总是运行。
控制器和junit测试中的代码完全相同。
欢迎任何有想法的人。
谢谢.
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:
<?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());
}
}
终于有朋友帮助了我。
那是因为"hot deployment"。
关闭它,问题就解决了。
在pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
我也遇到了同样的问题。
在我的案例中,原因是由于开发工具依赖性而导致的冲突。
删除冲突的依赖项后,它开始为我工作。
我们仍然可以通过在资源文件夹的 META-INF/spring-devtools.properties 文件中添加此行restart.include.dools=/(drools|kie)\-.*\.jar
来使用开发工具。
我删除了这个依赖它的工作
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
第一天学习 drools.I 遇到了这个奇怪的问题。
规则“Hello World”在 drl 中不能在 Controller 中 运行,但在 Junit 测试用例中运行良好。 控制器和 Junit 测试中的规则“另一个规则”总是运行。
控制器和junit测试中的代码完全相同。
欢迎任何有想法的人。 谢谢.
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:
<?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());
}
}
终于有朋友帮助了我。 那是因为"hot deployment"。 关闭它,问题就解决了。
在pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
我也遇到了同样的问题。 在我的案例中,原因是由于开发工具依赖性而导致的冲突。 删除冲突的依赖项后,它开始为我工作。
我们仍然可以通过在资源文件夹的 META-INF/spring-devtools.properties 文件中添加此行restart.include.dools=/(drools|kie)\-.*\.jar
来使用开发工具。
我删除了这个依赖它的工作
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>