当前位置: 首页 > article >正文

drool 7 multiThread 测试

基本信息

通过option ,使用如下代码进行设置

 //线程数量10
        MaxThreadsOption option=MaxThreadsOption.get(10);

        kieBaseConf.setOption(option);
        kieBaseConf.setOption(MultithreadEvaluationOption.YES);

并发是以CompositeDefaultAgenda/Rule为颗粒度来的,不同CompositeDefaultAgenda/rule在不同线程内执行 。

限制条件

在 KnowledgeBaseImpl 类 有如下代码:

private void checkMultithreadedEvaluation( RuleImpl rule ) {
    if (config.isMultithreadEvaluation()) {
        if (!rule.isMainAgendaGroup()) {
            disableMultithreadEvaluation( "Agenda-groups are not supported with multithread evaluation: disabling it" );
        } else if (rule.getActivationGroup() != null) {
            disableMultithreadEvaluation( "Activation-groups are not supported with multithread evaluation: disabling it" );
        } else if (!rule.getSalience().isDefault()) {
            disableMultithreadEvaluation( "Salience is not supported with multithread evaluation: disabling it" );
        } else if (rule.isQuery()) {
            disableMultithreadEvaluation( "Queries are not supported with multithread evaluation: disabling it" );
        }
    }
}

即不支持

  • agenda 分组
  • getActivationGroup
  • 优先级设置
  • Query

以下为 Agenda-groups测试,验证了上述点

09:48:08.187 [main] WARN  o.drools.core.impl.KnowledgeBaseImpl.disableMultithreadEvaluation:1010 - Agenda-groups are not supported with multithread evaluation: disabling it
[Rule name=Stateless Hello World, agendaGroup=again, salience=0, no-loop=false]
1700876888206
Always  Stateless message in thread 1,1700876888279
Always Again Stateless message in thread 1,1700876901286
Hello World again in thread 1,1700876914293
Process finished with exit code 0

取消
09:47:15.242 [main] WARN  o.d.c.k.builder.impl.KieBuilderImpl.packageNameForFile:394 - File 'org/drools/learn/MultiThreadHelloWorld.drl' is in folder 'org/drools/learn' but declares package 'org.drools.examples.multiThreadHello'. It is advised to have a correspondance between package and folder names.
[Rule name=Stateless Hello World, agendaGroup=MAIN, salience=0, no-loop=false]
1700876836099
Hello World in thread 19,1700876836192
Always  Stateless message in thread 18,1700876836193
Always Again Stateless message in thread 18,1700876849220
Stateless Goodbye cruel world in thread 24,1700876862233

其它

在这里插入图片描述
在executor里面submit的是CompositeDefaultAgenda,如果多个rule的when一致,会在同一线程执行,是drools把相关when合并在一个compsite吗?

Java 代码

// private static KieSessionConfiguration kieBaseConf;
    public static final void main(final String[] args) {
        KieServices ks = KieServices.get();
        KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
        //设置SequentialOption以提升性能
        kieBaseConf.setOption(SequentialOption.YES);
        //设置使用对象的equals函数来进行对象比较
        kieBaseConf.setOption(EqualityBehaviorOption.EQUALITY);
        //设置exception 捕获,不设置为默认使用org.drools.core.runtime.rule.impl.DefaultConsequenceExceptionHandler
        kieBaseConf.setOption(ConsequenceExceptionHandlerOption
                .get(DroolsConsequenceExceptionHandler.class));
        //线程数量10
        MaxThreadsOption option = MaxThreadsOption.get(10);

        kieBaseConf.setOption(option);
        kieBaseConf.setOption(MultithreadEvaluationOption.YES);


        //使用resource模式装载,参考https://zhuanlan.zhihu.com/p/519969197
        Resource resource =
                new ClassPathResource("org/drools/learn/MultiThreadNoModifyHelloWorld.drl");
        KieBase base = new KieHelper().addResource(resource)
                .build(kieBaseConf);


        StatelessKieSession ksession = base.newStatelessKieSession();
        System.out.println(ksession.getKieBase().getRule("org.drools.examples.MultiThreadNoModifyExample",
                "Stateless 4 "));
        ArrayList result = new ArrayList<Object>();
        ksession.setGlobal("list", result);


        KieRuntimeLogger logger
                = ks.getLoggers().newFileLogger(ksession, "./helloworld");

        System.out.println(System.currentTimeMillis());
        Message message = new Message();
        message.setMessage("Hello World");
        message.setStatus(10);
        ksession.execute(message);

        logger.close();


    }

DRL

package org.drools.examples.MultiThreadNoModifyExample
 
import org.drools.learn.MultiThreadNoModifyExample.Message;

global java.util.List list


rule "Stateless 0  "
    dialect "mvel"

    when
        m : Message( status >0 , status : status )
    then
        System.out.println(   " 0 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());
        Thread.sleep(13000);
end

rule "Stateless 1  "
    dialect "mvel"

    when
        m : Message( status >1 , status : status )
    then
        System.out.println(  " 1 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());
        Thread.sleep(13000);
end


rule "Stateless 2  "
    dialect "mvel"

    when
        m : Message( status >2 , status : status )
    then
        System.out.println(  " 2 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());
        Thread.sleep(13000);
end


rule "Stateless 3  "
    dialect "mvel"

    when
        m : Message( status >3 , status : status )
    then
        System.out.println(   " 3 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());
        Thread.sleep(13000);
end


rule "Stateless 4  "
    dialect "mvel"

    when
        m : Message( status > 4 , status : status )
    then
        System.out.println(   " 4 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());
        Thread.sleep(13000);
end

http://www.kler.cn/a/152683.html

相关文章:

  • java分布式锁分布式锁
  • 鉴源论坛 · 观擎丨民机机载软件的配置管理
  • 【Android MediaCodec 将音频转换为 pcm 格式】
  • ModuleNotFoundError: No module named ‘mdtex2html‘ module已经安装还是报错,怎么办?
  • 利用python实现文件压缩打包的功能
  • 国际上有哪些比较出名的VOIP供应商
  • 计算两个经纬度之间的实际距离(Haversine公式)----c++
  • Python语言学习笔记之七(JOSN应用)
  • conda和pip常用命令整理
  • css加载会造成阻塞吗??
  • Linux 系统启动过程
  • Git常用命令#更改用户名
  • 软考:2024年软考高级:软件工程
  • windows11 phpstudy_pro php8.2 安装redis扩展
  • ChatGPT
  • Scanner常用知识点
  • Pytorch中的gather的理解和用法
  • Linux的基本指令(三)
  • java设计模式学习之【对象池模式】
  • 正则表达式(Regular Exprerssion)in Python