Drools Rules Template - first invocation 1000 times slower than subsequence invocations

纵然是瞬间 提交于 2019-12-04 21:52:15

In the example you have linked, there is a line that is doing more than what it appears to be doing. The line I'm talking about is:

KieSession ksession = kc.newKieSession( "DTableWithTemplateKS" );

One of the steps inside KieContainer.newKieSession() is to create the KieBase the specified KieSession belongs to. A KieBase is the binary representation of your rules. Once a KieBase is built, it can be used to spawn multiple KieSessions (their runtime counterpart). The creation of a KieBase could be very time consuming. Spawning new KieSessions from it it is not.

The KieContainer class uses an internal Map to keep a reference to the KieBases that were already built. The first time you ask KieContainer for a KieSession the KieContainer has first to build the KieBase. Following invocations of newKieSession() will reuse the KieBase already built. Note that this is true as long as you always ask for the same KieSession. Try having multiple KieBases and ask for different KieSessions from them and you will see that the first time you ask for a KieSession from a new KieBase you will experience this delay.

One thing you could do is to ask KieContainer for your KieBase when your app is starting. You can do this by either executing kc.newKieSession( "XXX" ); or kc.getKieBase("YYY");

Hope it helps,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!