Hello,

I want to ask you if it is a good practive to pool stateful sessions for a specific ruleset to improve the execution performance.
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.

To pool sessions, I just declare multiple ksession tag in my kmodule.xml :

<kbase name="KBase" packages="com.example.*">
        <ksession name="KSession1"/>
        <ksession name="KSession2"/>
        <ksession name="KSession3"/>
        <ksession name="KSession4"/>
        <ksession name="KSession5"/>
</kbase>

In my spring webservice endpoint I just put that code to handle the pool :

@Endpoint
public class ExampleEndpoint implements InitializingBean {

    @Autowired
    private ExampleRuleService ruleService;
    private Map<Integer, Boolean> isRunningMap = new HashMap<Integer, Boolean>();
    private static final int NB_POOL_SESSIONS = 5;

    @PayloadRoot(localPart = "com.example.ExampleRequest")
    @ResponsePayload
    public ExampleResponse handleRequest(
            @RequestPayload ExampleRequest request) throws InterruptedException {
        KieServices ks = KieServices.Factory.get();
        KieContainer kc = ks.getKieClasspathContainer();
        while (true) {
            for (int i = 0; i < NB_POOL_SESSIONS; i++) {
                boolean run = false;

                synchronized (isRunningMap) {
                    if (!isRunningMap.get(i)) {
                        isRunningMap.put(i, true);
                        run = true;
                    }
                }

                if (run) {
                    KieSession ksession = kc.newKieSession("KSession" + (i + 1));
                    ExampleResponse response = ruleService.run(ksession, request);
                    ksession.dispose();
                    isRunningMap.put(i, false);
                    return response;
                }
            }
            Thread.sleep(100);
        }
    }

    public void afterPropertiesSet() throws Exception {
        for (int i = 1; i <= NB_POOL_SESSIONS; i++) {
            isRunningMap.put((i - 1), false);
        }
    }

}

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.

Thanks for your help.

--
Maxime FALAIZE