SpringBoot+Drools使用数据库

阅读数:176 评论数:0

跳转到新版页面

分类

python/Java

正文

POM依赖

        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
        </dependency>

建表

CREATE TABLE `drools_rule` (
  `rule_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `rule_uuid` varchar(64) DEFAULT NULL COMMENT '业务主键',
  `rule_content` longtext COMMENT '规则',
  PRIMARY KEY (`rule_id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

droolService

@Slf4j
@Service
public class DroolServiceImpl implements IDroolsService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TbRuleMapper tbRuleMapper;

    @Override
    public void reload() {
        Boolean needReload = redisUtil.exists("droolsNeedLoad");
        if(needReload){
            KieServices kieServices = KieServices.Factory.get();
            KieFileSystem kfs = kieServices.newKieFileSystem();
            TbRule tbRule = tbRuleMapper.findEffectiveRule();
            if(tbRule!=null&& StringUtils.isNotBlank(tbRule.getRuleContent())){
                kfs.write("src/main/resources/rules/temp.drl",tbRule.getRuleContent().getBytes());
                KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
                Results results = kieBuilder.getResults();
                if(results.hasMessages(Message.Level.ERROR)){
                    System.out.println(results.getMessages());
                    return;
                }
                KieUtil.setKieContainer(kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId()));
                log.info("drools规则重新加载成功");
            }
        }
        redisUtil.del("droolsNeedLoad");
    }


}

KieUtil

public class KieUtil {
    private static KieContainer kieContainer;
    private static KieSession kieSession;

    public static KieContainer getKieContainer(){
        return kieContainer;
    }

    public static void setKieContainer(KieContainer kieContainer){
        KieUtil.kieContainer = kieContainer;
        kieSession = KieUtil.kieContainer.newKieSession();
    }

    public static KieSession getKieSession(){
        return kieSession;
    }

    public static void setKieSession(KieSession kieSession){
        KieUtil.kieSession = kieSession;
    }
}

使用

            iDroolsService.reload();
            KieSession kieSession = KieUtil.getKieSession();
            kieSession.insert(entity);
            kieSession.fireAllRules();



相关推荐

一、概述 springboot中有两种方式使用kafka时,直接使用kafka-client连接kafka服务;另一种是使用spring-kafka框架来连接kafka。 1、版本兼容 使用时要注意版

当然可以自己写redis的工具类,或者使用第三方开源jar包或代码,这里使用spring boot的集成类。 一、pom依赖 <dependency> <gro

websocket协议基于tcp的网络协议,它实现浏览器与器全双工通信。 spring boot2 +websocket 1、添加依赖 <pre clas

背景: 之前用spring boot+mybatis+oracle,现在要改成spring boot_mybatis+postgresql。 为了想让一套代码即可

共同点 都是用来表示Spring某个类是否可以接收HTTP请求。 不同点 @Controller标识一个spring类是Spring MVC c

系统异常捕获 参见:spring boot 2 全局异常处理 @ControllerAdvice(annotations = {RestController.class}) public class

从SSH(Structs/Spring/Hibernate)到SSM(SpringMVC/Spring/MyBatis),到现在一个S(Spring)就够的年代,可见Spring越来越重要了。<

insert package com.sample import com.sample.

@ConditionalOnMissingBean只能在@Bean注解的方法上使用。 可以给该注解传入参数例如@ConditionOnMissingBean(name = "exa

2.4版本之前 在之前,我们在yaml配置文件中,使用spring.profiles来定义不同环境的标识,比如下面这样: <pre class="language-ma