<div dir="ltr"><div><div><div><div><div><div><div>Hello,<br><br></div>I want to ask you if it is a good practive to pool stateful sessions for a specific ruleset to improve the execution performance.<br></div>Actually in my application I execute my rules by calling SOAP webservice. For performance purpose, I test multithreaded calls to my webservice and I noted that when I pool sessions in the server side, it improves the performance a lot.<br>
<br></div>To pool sessions, I just declare multiple ksession tag in my kmodule.xml :<br><br><kbase name="KBase" packages="com.example.*"><br> <ksession name="KSession1"/><br>
<ksession name="KSession2"/><br> <ksession name="KSession3"/><br> <ksession name="KSession4"/><br> <ksession name="KSession5"/><br>
</kbase><br><br></div>In my spring webservice endpoint I just put that code to handle the pool :<br><br>@Endpoint<br>public class ExampleEndpoint implements InitializingBean {<br><br> @Autowired<br> private ExampleRuleService ruleService;<br>
private Map<Integer, Boolean> isRunningMap = new HashMap<Integer, Boolean>();<br> private static final int NB_POOL_SESSIONS = 5;<br><br> @PayloadRoot(localPart = "com.example.ExampleRequest")<br>
@ResponsePayload<br> public ExampleResponse handleRequest(<br> @RequestPayload ExampleRequest request) throws InterruptedException {<br> KieServices ks = KieServices.Factory.get();<br> KieContainer kc = ks.getKieClasspathContainer();<br>
while (true) {<br> for (int i = 0; i < NB_POOL_SESSIONS; i++) {<br> boolean run = false;<br><br> synchronized (isRunningMap) {<br> if (!isRunningMap.get(i)) {<br>
isRunningMap.put(i, true);<br> run = true;<br> }<br> }<br><br> if (run) {<br></div> KieSession ksession = kc.newKieSession("KSession" + (i + 1));<br>
</div> ExampleResponse response = ruleService.run(ksession, request);<br></div> ksession.dispose();<br><div><div><div><div> isRunningMap.put(i, false);<br> return response;<br>
}<br> }<br> Thread.sleep(100);<br> }<br> }<br><br> public void afterPropertiesSet() throws Exception {<br> for (int i = 1; i <= NB_POOL_SESSIONS; i++) {<br> isRunningMap.put((i - 1), false);<br>
}<br> }<br><br>}<br><br></div><div>It works well because in my benchmark I improve 5 times the performance (as I have 5 different threads) but I wondered if it is a good practice and if it does not hide any issues that I could have in the future.<br>
<br></div><div>Thanks for your help.<br clear="all"></div><div><div><div><div><div><div><div><br>-- <br>Maxime FALAIZE
</div></div></div></div></div></div></div></div></div></div></div>