Session Management - only one inside application or one by processsgardellajp Dec 29, 2011 7:12 AM
I have some questions about how manage sessions. I read official doc and says:
Chapter 4. Core Engine: API
Sessions can be created based on a knowledge base and are used to execute processes and interact with the engine. You can create as many independent session as you need and creating a session is considered relatively lightweight. How many sessions you create is up to you. In general, most simple cases start out with creating one session that is then called from various places in your application. You could decide to create multiple sessions if for example you want to have multiple independent processing units (for example, if you want all processes from one customer to be completely independent from processes for another customer, you could create an independent session for each customer) or if you need multiple sessions for scalability reasons. If you don't know what to do, simply start by having one knowledge base that contains all your process definitions and one create session that you then use to execute all your processes.
After read it, I want decide how manage session, so.
1) Create one session inside application and never dispose it.
2) Create a session per process. So when I start a process, I save somewhere the information about which sessionId must use for a processId.
I like option two, with persistent sessions. So when I start a process, I do:
start>> StatefulKnowledgeSession ksession = newSession(); // start a new process instance ProcessInstance instance = ksession.startProcess(id, params); ProcessIntanceInfo(instance.getId(), ksession.getId()); ksession.dispose(); return p;
So in my application I save p. When I must interact with p, I load the session associate with p.getProcessId(). Cool, I have a session persistent per process.
But when I go deep in implementation, I found some problems, for example related events. I want listen when a task is complete so I attach some listener to the session when I load/start it. But after work with the session I dispose it, so the listener don't work. I think the solution is this:
long processId = p.getProcessId(); int sessionId = p.getSessionId(); StatefulKnowledgeSession s = loadSession(sessionId); attachListeners(s); TaskClient t = ....; t.complete( taskId, serId, outputData, responseHandler ); responseHandler.waitTillDone(5000); s.dispose();
Now, the questions:
1) With this code, is safe except that the listener will listen the event of complete task?
2) Is this a good way to manage sessions? I don't find a good tutorial or document of differents strategies for handle sessions.
Thanks for your time,