springboot集成drools的方式二

springboot集成drools的方式二(kmodule.xml)

方式二比较简单,实际开发不常用

maven配置

详细的配置见码云上的代码工程:springbootDroolsMvn

    
        
            org.kie
            kie-api
            6.5.0.Final
        
        
            org.drools
            drools-compiler
            6.5.0.Final
        
        
            org.kie
            kie-spring
            6.5.0.Final
        
    

代码

在src/main/resource目录下新建,META-INF/kmoudule.xml,内容如下:



    
        
    

编写DroolRuleController类,用于测试

package com.pingan.core.controller;

import com.pingan.core.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class DroolRuleController {

    @RequestMapping("/rule2")
    @ResponseBody
    public String rule2(){
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kieSession = kContainer.newKieSession("ksession-rules");

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("superbing");
        userInfo.setTelephone("13618607409");

        kieSession.insert(userInfo);
        //kieSession.setGlobal("log",log);
        int firedCount = kieSession.fireAllRules();
        //kieSession.dispose();
        System.out.println("触发了" + firedCount + "条规则");
        return "触发了" + firedCount + "条规则";
    }
}

你可能感兴趣的:(drools规则流开发)