<!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;">
Re: ReceiveTaskHandler handling multiple process intances with the same message-id
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/mscetin">Melih Cetin</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/627690#627690">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Original implementation did not work as expected after server re-start. This is a an improved version in case someone else finds it useful. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I still would like someone from jBPM team to provide some remarks about this implementation</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">package com.mycomp.bpm.handler;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import java.util.HashMap;</span></p><p><span style="font-family: courier new,courier;">import java.util.Map;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">import org.apache.log4j.Logger;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.KnowledgeRuntime;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.NodeInstance;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.ProcessInstance;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.WorkItem;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.WorkItemHandler;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.WorkItemManager;</span></p><p><span style="font-family: courier new,courier;">import org.drools.runtime.process.WorkflowProcessInstance;</span></p><p><span style="font-family: courier new,courier;">import org.jbpm.workflow.instance.node.CompositeContextNodeInstance;</span></p><p><span style="font-family: courier new,courier;">import org.jbpm.workflow.instance.node.WorkItemNodeInstance;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">public class ReceiveTaskHandler implements WorkItemHandler {</span></p><p>    </p><p><span style="font-family: courier new,courier;">    private static Logger logger = Logger.getLogger(ReceiveTaskHandler.class.getName());</span></p><p>    </p><p><span style="font-family: courier new,courier;">    private Map<String, Long> waiting = new HashMap<String, Long>();</span></p><p><span style="font-family: courier new,courier;">    private KnowledgeRuntime ksession;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    public ReceiveTaskHandler(KnowledgeRuntime ksession) {</span></p><p><span style="font-family: courier new,courier;">       this.ksession = ksession;</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    public void setKnowledgeRuntime(KnowledgeRuntime ksession) {</span></p><p><span style="font-family: courier new,courier;">        this.ksession = ksession;</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: courier new,courier;">    private String constructKey(long   processInstanceId, String messageId) {</span></p><p><span style="font-family: courier new,courier;">        return processInstanceId + "|" + messageId;</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p>    </p><p><span style="font-family: courier new,courier;">    @Override</span></p><p><span style="font-family: courier new,courier;">    public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {</span></p><p><span style="font-family: courier new,courier;">        long processInstanceId = workItem.getProcessInstanceId();</span></p><p><span style="font-family: courier new,courier;">        String messageId = (String) workItem.getParameter("MessageId");</span></p><p><span style="font-family: courier new,courier;">        waiting.put(constructKey(processInstanceId, messageId), workItem.getId());</span></p><p><span style="font-family: courier new,courier;">        // If waiting map previously contained a mapping for messageId, the old value is replaced !!!</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p>    </p><p><span style="font-family: courier new,courier;">    public void messageReceived(String messageId, Object message) {</span></p><p><span style="font-family: courier new,courier;">        throw new UnsupportedOperationException("messageReceived(String messageId, Object message) method is not supported. " +</span></p><p><span style="font-family: courier new,courier;">                                                "Instead use messageReceived(long, String, Object) method" );</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p>    </p><p><span style="font-family: courier new,courier;">    private Long findWorkItemId(NodeInstance prmNodeInstance,</span></p><p><span style="font-family: courier new,courier;">                                String       prmMessageId) {</span></p><p><span style="font-family: courier new,courier;">        Long workItemId = null;</span></p><p><span style="font-family: courier new,courier;">        if (prmNodeInstance instanceof WorkItemNodeInstance) {</span></p><p><span style="font-family: courier new,courier;">            WorkItemNodeInstance workItemNode = (WorkItemNodeInstance)prmNodeInstance;</span></p><p><span style="font-family: courier new,courier;">            WorkItem workItem = workItemNode.getWorkItem();</span></p><p><span style="font-family: courier new,courier;">            if (workItem.getName().equals("Receive Task") &&</span></p><p><span style="font-family: courier new,courier;">                workItem.getParameter("MessageId").equals(prmMessageId)) {</span></p><p><span style="font-family: courier new,courier;">                workItemId = workItem.getId();</span></p><p><span style="font-family: courier new,courier;">            }</span></p><p><span style="font-family: courier new,courier;">        }</span></p><p><span style="font-family: courier new,courier;">        if (prmNodeInstance instanceof CompositeContextNodeInstance) {</span></p><p><span style="font-family: courier new,courier;">            for (NodeInstance ni : ((CompositeContextNodeInstance)prmNodeInstance).getNodeInstances(false)) {</span></p><p><span style="font-family: courier new,courier;">                workItemId = findWorkItemId(ni, prmMessageId);</span></p><p><span style="font-family: courier new,courier;">                if (workItemId != null) {</span></p><p><span style="font-family: courier new,courier;">                    break;</span></p><p><span style="font-family: courier new,courier;">                }</span></p><p><span style="font-family: courier new,courier;">            }</span></p><p><span style="font-family: courier new,courier;">        }</span></p><p><span style="font-family: courier new,courier;">        return workItemId;</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p>    </p><p><span style="font-family: courier new,courier;">    public void messageReceived(long processInstanceId, String  messageId, Object message) {</span></p><p><span style="font-family: courier new,courier;">        Long workItemId = waiting.get(constructKey(processInstanceId, messageId));</span></p><p><span style="font-family: courier new,courier;">        if (workItemId == null) {</span></p><p><span style="font-family: courier new,courier;">            // See if this is a work item persisted pefore the re-start of the server</span></p><p><span style="font-family: courier new,courier;">            ProcessInstance pi = ksession.getProcessInstance(processInstanceId);</span></p><p><span style="font-family: courier new,courier;">            final WorkflowProcessInstance workflowProcessInstance = ((WorkflowProcessInstance) pi);</span></p><p><span style="font-family: courier new,courier;">            for (NodeInstance nodeInstance : workflowProcessInstance.getNodeInstances()) {</span></p><p><span style="font-family: courier new,courier;">                workItemId = findWorkItemId(nodeInstance, messageId);</span></p><p><span style="font-family: courier new,courier;">                if (workItemId != null) {</span></p><p><span style="font-family: courier new,courier;">                    break;</span></p><p><span style="font-family: courier new,courier;">                }</span></p><p><span style="font-family: courier new,courier;">            }</span></p><p><span style="font-family: courier new,courier;">        }</span></p><p><span style="font-family: courier new,courier;">        if (workItemId != null) {</span></p><p><span style="font-family: courier new,courier;">            Map<String, Object> results = new HashMap<String, Object>();</span></p><p><span style="font-family: courier new,courier;">            results.put("Message", message);</span></p><p><span style="font-family: courier new,courier;">            ksession.getWorkItemManager().completeWorkItem(workItemId, results);</span></p><p><span style="font-family: courier new,courier;">        } else {</span></p><p><span style="font-family: courier new,courier;">            logger.error("No workItem found for receiveTask of process with id:" + processInstanceId);</span></p><p><span style="font-family: courier new,courier;">        }</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p>    </p><p><span style="font-family: courier new,courier;">    @Override</span></p><p><span style="font-family: courier new,courier;">    public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {</span></p><p><span style="font-family: courier new,courier;">        long processInstanceId = workItem.getProcessInstanceId();</span></p><p><span style="font-family: courier new,courier;">        String messageId = (String) workItem.getParameter("MessageId");</span></p><p><span style="font-family: courier new,courier;">        waiting.remove(constructKey(processInstanceId, messageId));</span></p><p><span style="font-family: courier new,courier;">    }</span></p><p><span style="font-family: courier new,courier;">}</span></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/627690#627690">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>