基本信息
通过option ,使用如下代码进行设置
//线程数量10MaxThreadsOption 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.DefaultConsequenceExceptionHandlerkieBaseConf.setOption(ConsequenceExceptionHandlerOption.get(DroolsConsequenceExceptionHandler.class));//线程数量10MaxThreadsOption option = MaxThreadsOption.get(10);kieBaseConf.setOption(option);kieBaseConf.setOption(MultithreadEvaluationOption.YES);//使用resource模式装载,参考https://zhuanlan.zhihu.com/p/519969197Resource 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.MultiThreadNoModifyExampleimport org.drools.learn.MultiThreadNoModifyExample.Message;global java.util.List listrule "Stateless 0 "dialect "mvel"whenm : Message( status >0 , status : status )thenSystem.out.println( " 0 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());Thread.sleep(13000);
endrule "Stateless 1 "dialect "mvel"whenm : Message( status >1 , status : status )thenSystem.out.println( " 1 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());Thread.sleep(13000);
endrule "Stateless 2 "dialect "mvel"whenm : Message( status >2 , status : status )thenSystem.out.println( " 2 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());Thread.sleep(13000);
endrule "Stateless 3 "dialect "mvel"whenm : Message( status >3 , status : status )thenSystem.out.println( " 3 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());Thread.sleep(13000);
endrule "Stateless 4 "dialect "mvel"whenm : Message( status > 4 , status : status )thenSystem.out.println( " 4 in thread " + Thread.currentThread().getId()+","+ Thread.currentThread().getName()+","+System.currentTimeMillis());Thread.sleep(13000);
end