Drools-同一KieSession可以处理多个请求 [英] Drools - same KieSession for multiple requests

查看:282
本文介绍了Drools-同一KieSession可以处理多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了Drools 6.3.0.我有3千条规则,涉及2个类别.举例来说,category1具有1500条规则,category2具有1500条规则.有2万个具有不同数据的订单.每个订单将具有一组不同的属性.现在,为每个请求创建kiesession都很耗时,而且确实很慢.因此,计划使用相同的kiesession执行所有订单,并且订单将使用多线程处理.

I use drools 6.3.0 in my projects. I have around 3 thousands rules with 2 categories. Say, category1 with 1500 rules and category2 with 1500 rules. There are 20k orders with different data. Each order will have set of different attributes. Now, creating kiesession for each request is taking time and it is really slow. So, planning to use the same kiesession for executing all the orders and orders will be processed using multi threading.

下面是我目前的做法.

 KieBase kieBase = null;
 KieServices kieServices = KieServices.Factory.get();
 KieRepository kieRepository = kieServices.getRepository();
        kieRepository.addKieModule(new KieModule() {
            @Override
            public ReleaseId getReleaseId() {
                return kieRepository.getDefaultReleaseId();
            }
        });

 KieFileSystem kfs = kieServices.newKieFileSystem();

 kfs.write(******);  // Load 3k rules in a for loop
 KieBuilder kb = kieServices.newKieBuilder(kfs);
            kb.buildAll();
 KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
 kieBase = kContainer.getKieBase();

 kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
            kieBase = kContainer.getKieBase();
 KieSession kieSession = kieBase.newKieSession();

这就是我创建kiesession的方式,并希望将此kiesession用于所有订单执行.

This is how i create kiesession and would like to use this kiesession for all the order execution.

我想使用线程池并行执行订单.

I want to execute orders in parallel using thread pool.

这就是方法

//这里的输入是基于类别和一些ID的.每个规则都有ID和类别.对于每个ID和类别,都会有一组应评估的规则.

// Here the entry is based on the category and some id. Each rule will have id and category. For each id and cateogory, there would be set of rules which should be evaluated.

流程就像,

1)从数据库接受2万笔订单2)对于每个订单,获取要执行的ID3)对于每个id,请格式化入口点,例如 entry = id + _category

1) Take 20k orders from DB 2) For each order, get ids to be executed 3) For each id, format entry point like entry = id+_category

4)插入订单属性数据和fireRules.

4) insert order attributes data and fireRules.

final EntryPoint entryPoint = ksession.getEntryPoint(entry);
        if (entryPoint != null) {
            entryPoint.insert(data);
            ksession.fireAllRules();
        } else {
            log.warn("No rules to be executed");
        }

现在,由于我必须并行执行订单,

Now, as i have to execute the orders in parallel,

每个订单将并行调用以下代码

Each order will call the below code in parallal,

entryPoint.insert(data);
ksession.fireAllRules();

由于每个订单将具有不同的属性,并且向入口点插入不同的数据,这会导致问题,而无法获得预期的结果.

Since, each order will have different attributes and inserting different data to entrypoint, it is causing problem and not getting the expected result.

反正有实现这一目标的方法吗?

Is there anyway to achieve this?

谢谢

推荐答案

也许您应该使用原型会话.在kmodule.xml文件中添加:

Maybe you should use prototype sessions. In kmodule.xml file add:

<ksession name="YourSessionName" type="stateful" default="false" clockType="realtime" scope="prototype"/>

这篇关于Drools-同一KieSession可以处理多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆