<!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: saving state after workitem
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/melc">Chris Melas</a> in <i>jBPM</i> - <a href="https://community.jboss.org/message/649187#649187">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p>So for the following case,</p><p>start-&gt;workItem1-&gt;script1-&gt;workItem2-&gt;script2-&gt;end</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>a) Yes by reaching script2 the workitem1 is completed and its entry is deleted from the workiteminfo table. It also means that workitem2 has executed and by failing at script2 you can only examine the NodeInstanceLog<span style="color: #555555; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"> </span>table to figure out what has happened and maybe take some action. However&#160; i think the process instance at that stage will not resume since there will be no associated workitem persisted to execute/complete.</p><p>b) If workitem1 completes then it is deleted from the workiteminfo. But generally if you pickup a persisted work item (other than a work item associated with a human task....because a human task has an entry in the task table as well and needs a bit more care i.e. whether the task has completed or not etc) from the database you can either re execute it or complete it. These are achieved by quering the database table workiteminfo retrieving the object associated with the work item you want and calling either</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>YourWorkItemHandler yourWorkItemHandler = new YourWorkItemHandler();</p><p>yourWorkItemHandler.executeWorkItem(workItemFromDB, ksession.getWorkItemManager());</p><p><em>to re execute the work item </em></p><p><em>or complete it</em></p><p>ksession.getWorkItemManager().completeWorkItem(workItemFromDB, workItemFromDB.getParameters()/*or some other params etc*/);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The following bootstrap code can be used for non human task work items that have not completed (i.e. system crashed while running) and choosing to re execute them, as well as human task work items that have completed and the system crashed right after the human task completion but before entering the next wait point.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>private void bootstrapForWorkItems() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; EntityManagerFactory emf = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; EntityManager tempEntityManager = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; List results = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; emf = (EntityManagerFactory) ksession.getEnvironment().get(EnvironmentName.ENTITY_MANAGER_FACTORY);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tempEntityManager = emf.createEntityManager();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; results = tempEntityManager.createNativeQuery("SELECT w.* FROM WorkItemInfo w inner join Task t on t.workItemId=w.workItemId where t.status='Completed' union SELECT w.* FROM WorkItemInfo w&#160; where name &lt;&gt; 'Human Task'", WorkItemInfo.class).getResultList();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } finally {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (tempEntityManager != null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tempEntityManager.close();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (results != null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (Object resultObject : results) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WorkItemInfo workItemInfo = (WorkItemInfo) resultObject;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WorkItem workItem = workItemInfo.getWorkItem(ksession.getEnvironment());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (workItem.getName().equals("YourWorkItem1")) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; YourWorkItem1WorkItemHandler yourWorkItem1WorkItemHandler = new YourWorkItem1WorkItemHandler(ksession);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; yourWorkItem1WorkItemHandler.executeWorkItem(workItem, ksession.getWorkItemManager());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (workItem.getName().equals("YourWorkItem2")) {</p><p> YourWorkItem2WorkItemHandler yourWorkItem1WorkItemHandler = new YourWorkItem2WorkItemHandler(ksession);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; YourWorkItem2WorkItemHandler.executeWorkItem(workItem, ksession.getWorkItemManager());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else if (workItem.getName().equals("Human Task")) {</p><p>/*i'm not sure about the workItem.getParameters() here, might be getting the params of the previous human task....might need a little fixing*/</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ksession.getWorkItemManager().completeWorkItem(workItem.getId(), workItem.getParameters());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //or</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //call correct workitemhandler for this task and re execute</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }//for - results</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }//if - results</p><p>&#160;&#160;&#160; }//method</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Of course in some cases you may choose to act differently i.e.instead of re executing a work item, complete it because you checked your business logic (i.e. checked your system's database) and realised that data has been commited or web service has been called but the jbpm engine system crashed right before erasing the workitem etc.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>c) By quickly looking at your code and&#160; log&#160; i see that workitem1 executed then script1, followed by workitem2 and script2 .... so i think it went fine. I'm not sure i get the problem.</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/649187#649187">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>