AnsweredAssumed Answered

Difference between activiti:delegateExpression and activiti:class. Bug?

Question asked by richardmward on Aug 20, 2015
Latest reply on Aug 21, 2015 by richardmward
Hi,

I have come across what I think may be a bug with the use of
activiti:delegateExpression
as an alternative to
activiti:class
on both
serviceTask
's and
taskListener
's.

When using
activiti:class
, one instance of a
JavaDelegate
  is created for each
serviceTask
in a
ProcessDefinition
. When using
activiti:delegateExpression
, one instance of a
JavaDelegate
, peroid. This is as expected.

The issue comes when you have two
serviceTask
's which use different
Expression
's. For example:

In the
JavaDelegate
:

@Component("processDefinitionBean")
public class ProcessDefinitionBean {
    private Expression requiredVariable;
    private Expression optionalVariable;
   …
}


In
bpmn1.bpmn20.xml



<serviceTask id="serviceTask" name="Service task" activiti:delegateExpression="${processDefinitionBean}">
  <extensionElements>
    <activiti:field name="requiredVariable">
      <activiti:expression>${requiredVariableValue}</activiti:expression>
    </activiti:field>
    <activiti:field name="optionalVariable">
      <activiti:expression>${optionalVariableValue}</activiti:expression>
    </activiti:field>
</serviceTask>



In
bpmn2.bpmn20.xml



<serviceTask id="serviceTask" name="Service task" activiti:delegateExpression="${processDefinitionBean}">
  <extensionElements>
    <activiti:field name="requiredVariable">
      <activiti:expression>${requiredVariableValue}</activiti:expression>
    </activiti:field>
</serviceTask>



Now, if an instance of
bpmn1
is started, when it reaches the
serviceTask
it will initialise all three of the
Expression
's in the
JavaDelegate
. If an instance of
bpmn2
is then started, when it reaches the
serviceTask
, the
requiredVariable
Expression
is being set to the value in
bpmn2
, but the
optionalVariable
Expression
is still set to the value from
bpmn1
. This is a pretty significant issue as it means that the
processDefinitionBean
is not in the state it expects to be in, and will either end up with a value it is not meant to have in it (should
bpmn2
happen to set
${optionalVariableValue}
somewhere along the way, or it will fall over because
optionalVariableValue
does not exist.

This isn't a problem when using
activiti:class
becuase there is one instance for each of the two
serviceTask
's, so there is no possibility that different combinations of
activiti:field
's can be set on the same isntance of a
JavaDelegate
.

For the time being, we are nullifying each of the
Expression
's at the end of the 
exectute
method in each of our
JavaDelegate
's - but I wonder if an official fix should be investigated (should this be considered a bug).

Outcomes