线程中创建一个StatlelessKiesession
请看如下代码:
package com.droolstest.dynamicRuleThread;
import com.droolstest.Person;
import com.droolstest.School;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.command.CommandFactory;
import org.kie.internal.conf.MultithreadEvaluationOption;
import org.kie.internal.utils.KieHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class DrlStatlelesss {
private static int thread_num = 200;//线程数,设置同时并发线程数
private static int client_num = 10;//访问次数
/**
* 测试 并发下 使用
* 不同一个StatlelessKiesession无状态 线程中创建一个StatlelessKiesession
* 同 KieHelper
*/
/**
* 结论1:rule 名是不能重复,这一点,符合kmodule.xml 规范要求 如果创建多次session则判断多次rule名
* 结论2:rule与rule之间的Fact会相互作用 在不设置 MultithreadEvaluationOption.YES 规则执行
* myRuleFile1与myRuleFile2 随机执行 但只要有其中一个执行了 则另一个不执行 原因Fact对象已经发现改变
* 结论3:helper1.build(MultithreadEvaluationOption.YES).newKieSession(); 并无实际效果 与结论2效果一样
* 结论4:执行规则得到结果 受salience 影响, 可能是王王 也可能是李四
* @param args
*/
public static void main(String[] args) {
//规则文件
String myRuleFile1 = "package rules " + "import com.droolstest.Person;" + "import com.droolstest.School;" +
"rule \"myRule1\" " +
" when " +
" $p:Person(name==\"张三\")" +
" $s:School(name==\"北大\")" +
" then " +
" $p.setName(\"李四\");" +
" $s.setName(\"清华\");" +
" update($p);" +
" update($s);" +
"end " +
"rule \"myRule2\" " +
" when " +
" $p:Person(name==\"李四\")" +
" $s:School(name==\"清华\")" +
" then " +
" System.out.println(\"规则myRuleFile1被调用\");" +
"end";
String myRuleFile2 = "package rules " + "import com.droolstest.Person;" + "import com.droolstest.School;" +
"rule \"myRule3\" " +
" when " +
" $p:Person(name==\"张三\")" +
" $s:School(name==\"北大\")" +
" then " +
" $p.setName(\"王王\");" +
" $s.setName(\"张大\");" +
" update($p);" +
" update($s);" +
"end " +
"rule \"myRule4\" " +
" when " +
" $p:Person(name==\"王王\")" +
" $s:School(name==\"张大\")" +
" then " +
" System.out.println(\"规则myRuleFile2调用\");" +
"end";
long start = System.currentTimeMillis();
KieHelper helper1 = new KieHelper();
long end = System.currentTimeMillis();
System.out.println("输出创建KieHelper用的毫秒是="+(end - start));
long startAddrule = System.currentTimeMillis();
//分别将规则myRuleFile2 myRuleFile1 加载到虚拟文件中
helper1.addContent(myRuleFile1, ResourceType.DRL);
helper1.addContent(myRuleFile2, ResourceType.DRL);
long endAddrule = System.currentTimeMillis();
System.out.println("导入规则所用到的毫秒是="+(endAddrule - startAddrule));
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semp = new Semaphore(thread_num);
for (int index = 0; index < client_num; index++) {
Runnable run = new Runnable() {
public void run() {
try {
semp.acquire();
long startNewKiesession = System.currentTimeMillis();
StatelessKieSession kieSession = helper1.build(MultithreadEvaluationOption.YES).newStatelessKieSession();
long endNewKiesession = System.currentTimeMillis();
System.out.println("创建无状态StatelessKieSession所用到的毫秒是="+(endNewKiesession - startNewKiesession));
long startrule = System.currentTimeMillis();
Person person = new Person();
person.setName("张三");
person.setAge("" + (int) (Math.random() * 100));
School school = new School();
school.setName("北大");
school.setCount("40");
List cmds = new ArrayList();
cmds.add(CommandFactory.newInsert(person, "p1"));
cmds.add(CommandFactory.newInsert(school, "s1"));
kieSession.execute(CommandFactory.newBatchExecution(cmds));
System.out.println(person.getName());
long endrule = System.currentTimeMillis();
System.out.println("规则执行所用到的毫秒是="+(endrule - startrule));
System.out.println("=============================牛逼的分割线=========================================");
semp.release();
} catch (Exception e) {
e.printStackTrace();
}
}
};
exec.execute(run);
}
exec.shutdown();
}
}