cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to get a variable set in service task

gouthr
Champ in-the-making
Champ in-the-making
I am new to Activiti and Java. I am trying to create a simple unit test to invoke a Java service task from Activiti engine. Below is a code snippet:

ServiceTask.java:


package test;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class Servicetask implements JavaDelegate {
   public void execute(DelegateExecution execution) {
   
      System.out.println("execution id " + execution.getId());
   
      String empname = (String) execution.getVariable("Empname");
      
      System.out.println("received employee name " + empname);
      
      empname = empname.toUpperCase();
       execution.setVariable("input", empname);
    }

}

JUNIT test:
package org.activiti.designer.test;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
import java.io.FileInputStream;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;

public class ProcessTestLeaveProcess {

   private String filename = "C:\\junoWsp3\\SimpleLeave-Process\\src\\main\\resources\\diagrams\\SimpleLeaveProcess.bpmn";

   @Rule
   public ActivitiRule activitiRule = new ActivitiRule();

   @Test
   public void startProcess() throws Exception {
      RepositoryService repositoryService = activitiRule.getRepositoryService();
      repositoryService.createDeployment().addInputStream("SimpleLeaveProcess.bpmn20.xml",
            new FileInputStream(filename)).deploy();
      RuntimeService runtimeService = activitiRule.getRuntimeService();
      Map<String, Object> variableMap = new HashMap<String, Object>();
      variableMap.put("Empname", "Goutham");
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess", variableMap);
      assertNotNull(processInstance.getId());
      System.out.println("id " + processInstance.getId() + " "
            + processInstance.getProcessDefinitionId());

      String empnameBold = (String) runtimeService.getVariable(processInstance.getId(), "input");
      assertNotNull(empnameBold);

      System.out.println("Name in bold: " + empnameBold);
   }
}

When I run the above code, the last println line isn't displayed. What am I missing here?

Thanks!
4 REPLIES 4

trademak
Star Contributor
Star Contributor
Hi Goutham,

It's hard to say without seeing your process definition, but I'm expecting that the process instance has finished after executing the start process instance method. So you would have to use the HistoryService to retrieve the variable value instead of using the RuntimeService.

Best regards,

gouthr
Champ in-the-making
Champ in-the-making
Thanks for the response Tijs! Below is the process definition. Please let me know if this helps.

<?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="leaveProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start">
      <extensionElements>
        <activiti:formProperty id="Empname" name="Empname" type="string"></activiti:formProperty>
      </extensionElements>
    </startEvent>
    <serviceTask id="servicetask1" name="Service Task" activiti:class="test.Servicetask"></serviceTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leaveProcess">
    <bpmndi:BPMNPlane bpmnElement="leaveProcess" id="BPMNPlane_leaveProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="220.0" y="280.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="450.0" y="280.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="255.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="300.0" y="297.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="405.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="450.0" y="297.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

gouthr
Champ in-the-making
Champ in-the-making
<?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="leaveProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start">
      <extensionElements>
        <activiti:formProperty id="Empname" name="Empname" type="string"></activiti:formProperty>
      </extensionElements>
    </startEvent>
    <serviceTask id="servicetask1" name="Service Task" activiti:class="test.Servicetask"></serviceTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leaveProcess">
    <bpmndi:BPMNPlane bpmnElement="leaveProcess" id="BPMNPlane_leaveProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="220.0" y="280.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="450.0" y="280.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="255.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="300.0" y="297.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="405.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="450.0" y="297.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

gouthr
Champ in-the-making
Champ in-the-making
Thanks Tijs! You were right. The process instance had finished after executing the start process instance method. I will look into the HistoryService.