<!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="https://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;">
Re: How to make a process instance go on with jBPM5?
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/jesus.deoliveira">Jesus De Oliveira</a> in <i>jBPM</i> - <a href="https://community.jboss.org/message/719772#719772">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;"> </p><p>I also have found that you must force to complete the WorkItem after completing a human task.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>A more detailed and a little bit cleaner version of Melih and Donald approach also works perfectly, even when the human task server is running on a separate thread or JVM, provided that the process engine JPA persistence is correctly configured and the original statefull knowledge session is restored upon start-up. Some boilerplate code intentionally omitted for the sake of simplicity:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><span>// If no previous statefull knowledge session exists public void createStatefullKnowledgeSession() {      //... create kbase, load process definitions, etc ...      EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");      Environment env = KnowledgeBaseFactory.newEnvironment();      env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);      env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());      ksessionStatefull = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);      int sessionId = ksessionStatefull.getId();      // *** STORE sessionId SOMEWHERE PERSISTENTLY, to be able to      // recover this session later upon services restart }</span><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> loadStatefullKnowledgeSession() <font color="navy">{</font>
 
     <font color="darkgreen">//... create kbase, load process definitions, etc ...</font>
 
     EntityManagerFactory emf = Persistence.createEntityManagerFactory(<font color="red">"org.jbpm.persistence.jpa"</font>);
     Environment env = KnowledgeBaseFactory.newEnvironment();
     env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
     env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
 
     <font color="darkgreen">// *** RECOVER sessionId FROM PERSISTENT STORAGE</font>
     <font color="darkgreen">// and use it here to recover persisted process engine state</font>
     ksessionStatefull = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> complete(<font color="navy"><b>long</b></font> taskId, String userName)
    <font color="navy"><b>throws</b></font> Exception <font color="navy">{</font>
 
            <font color="darkgreen">// ... create the TaskClient (client), and connect it to the task server ...</font>
 
            <font color="darkgreen">// first mark the task as completed on the task server</font>
            taskOperationHandler = <font color="navy"><b>new</b></font> BlockingTaskOperationResponseHandler();
            client.complete(taskId, userName, null, taskOperationHandler);
            taskOperationHandler.waitTillDone(1000);
 
            <font color="darkgreen">// then retrieve the task to extract the workItemId, where the process instance is waiting</font>
            BlockingGetTaskResponseHandler responseHandler = <font color="navy"><b>new</b></font> BlockingGetTaskResponseHandler();
            client.getTask(taskId, responseHandler);
            responseHandler.waitTillDone(1000);
 
            Task task = responseHandler.getTask();
 
            <font color="darkgreen">// force completition of the workItem associated with the task, on which the process instance is waiting</font>
            <font color="navy"><b>final</b></font> Map<String, Object> results = <font color="navy"><b>new</b></font> HashMap<String, Object>();
            ksessionStatefull.getWorkItemManager().completeWorkItem(task.getTaskData().getWorkItemId(), results);
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Hope this will be useful to anyone, best regards!</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/719772#719772">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="https://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>