cancel
Showing results for 
Search instead for 
Did you mean: 

Execution of JavaServiceTask via variable

anandvivek
Champ in-the-making
Champ in-the-making

Hi, I am trying to execute the service task via variable.

When I am using literal in the service task it works. But when I assign the variable with the same literal it doesnt work.

CASE 1: Using literal in delegateExpression

Below is an example

Process XML

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC" xmlnsSmiley Surprisedmgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="test1" name="test1" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${LineManagerCompletionService}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
<dataObject id="assignee" name="assignee" itemSubjectRef="xsd:string">
<extensionElements>
<activiti:value></activiti:value>
</extensionElements>
</dataObject>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_test1">
<bpmndi:BPMNPlane bpmnElement="test1" id="BPMNPlane_test1">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="280.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="910.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="520.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="315.0" y="227.0"></omgdi:waypoint>
<omgdi:waypoint x="520.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="625.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="910.0" y="227.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

@Named("LineManagerCompletionService")
public class LineManagerCompletionService implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("******Saving LM Decision******");
}

}

Case 2 : Using Delegate Expression as Variable

XML

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC" xmlnsSmiley Surprisedmgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="test1" name="test1" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${assignee}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
<dataObject id="assignee" name="assignee" itemSubjectRef="xsd:string">
<extensionElements>
<activiti:value>LineManagerCompletionService</activiti:value>
</extensionElements>
</dataObject>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_test1">
<bpmndi:BPMNPlane bpmnElement="test1" id="BPMNPlane_test1">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="280.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="910.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="520.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="315.0" y="227.0"></omgdi:waypoint>
<omgdi:waypoint x="520.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="625.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="910.0" y="227.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

Failure Trace

org.activiti.engine.ActivitiIllegalArgumentException: Delegate expression ${assignee} did neither resolve to an implementation of interface org.activiti.engine.impl.pvm.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate
at org.activiti.engine.impl.bpmn.behavior.ServiceTaskDelegateExpressionActivityBehavior.execute(ServiceTaskDelegateExpressionActivityBehavior.java:100)

5 REPLIES 5

gdharley
Elite Collaborator
Elite Collaborator

A delegateExpression, as the name implies, must implement the JavaDelegate or ActivitiBehavior interface.

Obviously the ${assignee} expression doent resolve to such a class.

I am not sure what your ${assignee} class actually resolves to so cannot offer a solution, but you may consider using an Expression rather than a DelegateExpression.

Thanks,

Greg

anandvivek
Champ in-the-making
Champ in-the-making

assignee is my process variable and I have set the value of assignee to "LineManagerCompletionService"

<dataObject id="assignee" name="assignee" itemSubjectRef="xsd:string">
<extensionElements>
<activiti:value>LineManagerCompletionService</activiti:value>
</extensionElements>
</dataObject>

 

 

When I put DelegateExpression = ${LineManagerCompletionService}. It works.(Case 1 in my example)

But when I put DelegateExpression = ${assignee} i get this error. (Case 2 in my example)

 

Basically I want to execute the service task based on process variable value, which can be initialized at the start of process.  So that I dont have to create multiple workflow defn only because my service task is different.

gdharley
Elite Collaborator
Elite Collaborator

Yeah, what you need to do is use the working syntax:

<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${LineManagerCompletionService}">

And add the assignee as an input property. Simple enough to do and the users guide has examples as well as the test suite.

Greg

anandvivek
Champ in-the-making
Champ in-the-making

Hi Greg,

Thanks a lot for reply. Not sure if I explained my problem correctly.

I am NOT trying to do field injection. My problem is quite different.

For e.g. I have more than one Class which implements JavaDelegate.

@Named("LineManagerCompletionService1")
public class LineManagerCompletionService1 implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("******Saving LM Decision=====1******");
}

}

@Named("LineManagerCompletionService2")
public class LineManagerCompletionService2 implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("******Saving LM Decision=====2******");
}

}

@Named("LineManagerCompletionService3")
public class LineManagerCompletionService3 implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("******Saving LM Decision====3******");
}

}

And I want to execute any of the different  beans(bean name can be provided as input parameter at start of the workflow) from same JAVA Service tasks without modifying my process defn.

I hope this makes this clear now. Also can you please point me where its exactly mentioned in User Guide?