本文介绍: 在executor里面submit的是CompositeDefaultAgenda,如果多个rule的when一致,会在同一线程执行,是drools把相关when合并在一个compsite吗?并发是以CompositeDefaultAgenda/Rule为颗粒度来的,不同CompositeDefaultAgenda/rule在不同线程内执行。以下为 Agenda-groups测试,验证了上述点。通过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-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
原文地址:https://blog.csdn.net/weixin_40455124/article/details/134622475
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_1120.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。