<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Session Management - only one inside application or one by processs
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/gardellajp">gardellajp</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/643768#643768">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hi,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I have some questions about how manage sessions. I read official doc and says:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong><em>Chapter 4. Core Engine: API</em></strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em>Sessions can be created based on a knowledge base and are used to execute processes and interact with the engine.&#160; You can create as many independent session as you need and creating a session is considered relatively lightweight.&#160; How many sessions you create is up to you.&#160; In general, most simple cases start out with creating one session that is then called from various places in your application.&#160; 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.&#160; If you don't know what to do, simply start by having <strong>one knowledge base that contains all your process definitions </strong>and <strong>one create session </strong>that you then use to execute all your processes.</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>After read it, I want decide how manage session, so.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>1) Create one session inside application and never dispose it.</p><p>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.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I like option two, with persistent sessions. So when I start a process, I do:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"> start&gt;&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; StatefulKnowledgeSession ksession = newSession();&#160;&#160;&#160;&#160;&#160; 
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// start a new process instance</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProcessInstance instance = ksession.startProcess(id, params);&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProcessIntanceInfo(instance.getId(), ksession.getId());
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; ksession.dispose();
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> p;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So in my application I save <em>p. </em>When I must interact with p, I load the session associate with <em>p.getProcessId()</em>. Cool, I have a session persistent per process.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>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. <strong>But </strong>after work with the session I dispose it, so the listener don't work. I think the solution is this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>long</b></font> processId = p.getProcessId();
<font color="navy"><b>int</b></font> sessionId =&#160; p.getSessionId();
StatefulKnowledgeSession s = loadSession(sessionId);
attachListeners(s);
&#160;
TaskClient t = ....;
&#160;
t.complete( taskId, serId, outputData, responseHandler );
&#160;
responseHandler.waitTillDone(5000);
&#160;
s.dispose();
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now, the questions:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>1) With this code, is safe except that the listener will listen the event of complete task?</p><p>2) Is this a good way to manage sessions? I don't find a good tutorial or document of differents strategies for handle sessions.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thanks for your time,</p><p>Juan</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/643768#643768">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>