流口水|重新加载规则而无需重新启动服务器

时间:2019-05-14 13:47:03

标签: drools kie drools-kie-server kie-server

我正在使用Gradle和7.20 drools版本创建一个Spring Boot应用程序。我们从路径drl_file_path的外部资源文件位置加载Drools知识库。以下是我的配置代码。

@Configuration
public class DroolConfig {
    KieServices kieServices = KieServices.Factory.get();
    @Bean
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem =kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newFileResource(new File("drl_file_path")));
        return kieFileSystem;
    }

    @Bean
    public KieContainer kieContainer() throws IOException{
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
        kieBuilder.buildAll();
        KieModule kieModule = kieBuilder.getKieModule();
        KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
        KieScanner kScanner = kieServices.newKieScanner(kieContainer);
        kScanner.start( 10000L );
        return kieContainer;
    }

    @Bean
    public KieScanner kieScanner() throws IOException{
        return kieServices.newKieScanner(kieContainer());
    }
}

我们有可能必须更新drools规则,并且我们想要更新drools知识库而无需重新启动服务器,我们正在使用KieScanner.sacnNow(),它不起作用,这是我缺少的关键点。是重新加载并应用流口水规则的代码-

@Component
public class DroolComponent {
    @Autowired private KieContainer kieContainer;
    @Autowired private KieScanner kieScanner;

    public Customer reloadDroolsAndApply(Customer customer) {
        kieScanner.scanNow();
        //above scanning not applying updated rules. 
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(customer);
        kieSession.fireAllRules();
        kieSession.dispose();
        return customer;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法为Drools获取新容器。

const handleSubmitCountry = e => {
  e.preventDefault();
  const countryQuery = e.target.countryQuery.value;
  // use countryQuery now
  countryQuery && alert(`Submitted Country Query: ${countryQuery}`);
};

...
<form onSubmit={handleSubmitCountry}>
  <input name="countryQuery" type="text" />
  <button type="submit">Submit</button>
</form>

使静态变量分配并使用容器。