KieUtils
public class KieUtils {
private static KieContainer kieContainer;
private static KieSession kieSession;
private static KieBase kieBase;
public static KieContainer getKieContainer() {
return kieContainer;
}
public static KieSession getKieSession() {
return kieSession;
}
public static KieBase getKieBase() {
return kieBase;
}
public static void setKieContainer(KieContainer kieContainer) {
KieUtils.kieContainer = kieContainer;
KieUtils.kieSession = kieContainer.newKieSession();
KieUtils.kieBase = kieContainer.getKieBase();
}
}
reload方法
public void reload() {
KieServices kieServices = getKieServices();
KieFileSystem kfs = kieServices.newKieFileSystem();
// 这里可以替换成数据库的查询结果
String loadRules = loadRules();
System.out.println("待写入的规则为:" + loadRules);
kfs.write("src/main/resources/rule/test.drl", loadRules);
Results results = kieServices.newKieBuilder(kfs).buildAll().getResults();
if (results.hasMessages(Message.Level.ERROR)) {
log.error("{}", results.getMessages());
throw new IllegalStateException("### build errors ###");
}
KieUtils.setKieContainer(kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId()));
log.info("reload新规则重载成功");
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
注意
如果想要项目启动时初始化加载rules,不能用@Postconstruct,可以用@Scheduled(fixedRate = 60L * 1000 * 60L * 1, initialDelay = 0),启动后自动加载且每小时刷新一次