Camunda workflow and decision automation platform part 2

Posted By :Abhishek Kumar |30th March 2020

Hi welcome back to the Camunda blog again in the last blog, I had explained the Camunda working through the  Camunda open API  now I am going to explain the working of Camunda the code i.e explaining how we can start the task and execute the task through the java code.
Delegation Code gives us a way to execute external Java code, scripts, or evaluate expressions when certain tasks occur during process execution.
There are four types of Delegation Code but I will explain the  few Delegation Code in this Blog:
 1. Java Delegates will be attached to a BPMN Service Tasks.
 2. Delegate Variable Mapping can be attached to a Call events
 3. Execution Listeners can be attached to any event within the normal token flow
 4. Task Listeners can process the events within the user task lifecycle, e.g., creation or completion of a user task.
We can create a generic Delegation Code and configure this via the BPMN 2.0 XML using and this is called Field Injection.

Java Delegate

Now we are going to create a Java class and in that, we can use the process variable to change the String lowercase to uppercase. For that this class needs to implement the JavaDelegate interface, which requires us to implement the execute(DelegateExecution) method.  This is the operation that will be called by the camunda engine and which needs to contain the business logic

public class ToUppercase implements JavaDelegate {

    public void execute(DelegateExecution execution) throws Exception {
      String var = (String) execution.getVariable("input");
      var = var.toUpperCase();
      execution.setVariable("input", var);

Field Injection

We have also injected the value of the field to delegate class. 
The following types of injection are supported:
Fixed string values

<serviceTask id="javaServices"
               name="Java service invocation"
        <camunda:field name="texts" stringValue="Hello World" />

The class ToUpperCaseFieldInjectsed has a field text which is of type org.camunda.bpm.engine.delegate.Expression. To get the task text.getValue(execution), the configured string value Hello World will be returned.
Alternatively, for longs texts 

<serviceTask id="javaService"
               name="Java service invocation"
      <camunda:field name="texts">
            Hello World
    </extensionElements> </serviceTask>

We can also injected expressions and resolves them using the current DelegateExecution.

public class ReverseStringsFieldInjected implements JavaDelegate {

    private Expression texts1;
    private Expression texts2;

    public void execute(DelegateExecution execution) {
      String value1 = (String) texts1.getValue(execution);
      execution.setVariable("var1", new StringBuffer(value1).reverse().toString());

      String value2 = (String) texts2.getValue(execution);
      execution.setVariable("var2", new StringBuffer(value2).reverse().toString()  }  }

Delegate Variable Mapping
DelegateVariableMapping interface.

public class DelegatedVarMapping implements DelegateVariableMapping {

  public void mapInputVariables(DelegateExecution execution, VariableMap variables) {
    variables.putValue("inputVar", "inValue");

  public void mapOutputVariables(DelegateExecution execution, VariableScope subInstance) {
    execution.setVariable("outputVar", "outValue");

Execution Listener

Execution listeners are used for the execution of external Java code and expressions when certain events occur during process execution. The events that can be captured are:
Start and end of a process instance.
Taking a transition.
Start and end of an activity.
Start and end of a gateway.
Start and end of intermediate events.

<process id="executionListenersProcess">
          class="org.camunda.bpm.examples.bpmn.executionlistener.ExampleExecutionListenerOne" />
    <startEvent id="theStart" />
    <sequenceFlow sourceRef="theStart" targetRef="firstTask" />
    <userTask id="firstTask" />
    <sequenceFlow sourceRef="firstTask" targetRef="secondTask">
          <camunda:script scriptFormat="groovy">
            println execution.eventName
    <userTask id="secondTask">
        <camunda:executionListener expression="${myPojo.myMethod(execution.eventName)}" event="end" />

    <sequenceFlow sourceRef="secondTask" targetRef="thirdTask" />

    <userTask id="thirdTask" />

    <sequenceFlow sourceRef="thirdTask" targetRef="theEnd" />

    <endEvent id="theEnd" />


The listener is Java-class and should implement the ExecutionListener interface. When the event occurs the method notify(DelegateExecution execution) is called


public class ExampleExecutionListenerOne implements ExecutionListener {

    public void notify(DelegateExecution execution) throws Exception {
      execution.setVariable("variableSetInExecutionListener", "firstValue");
      execution.setVariable("eventReceived", execution.getEventName());

Task Listener

Used for custom java logic or expression occurrence of a certain task-related event
It will only be used in the task definition as a child element of a user task.

<userTask id="myTasks" name="My Tasks" >
      <camunda:taskListener event="createed" class="MyTaskCreateListener" />

class: the class implements the TaskListener interface.

public class MyTaskCreateListener implements TaskListener {

  public void notify(DelegateTask delegateTask) {
    // Custom logic goes here


Field Injection on Listener

 <process id="executionListenersProcess">
      <camunda:executionListener class="org.camunda.bpm.examples.bpmn.executionListener.ExampleFieldInjectedExecutionListener" event="start">
        <camunda:field name="fixedValues" stringValue="Yes, I am " />
        <camunda:field name="dynamicValues" expression="${myVar}" />

    <startEvent id="theStart" />
    <sequenceFlow sourceRef="theStart" targetRef="firstTask" />

    <userTask id="firstTasks" />
    <sequenceFlow sourceRef="firstTasks" targetRef="theEnd" />

    <endEvent id="theEnd" />

The actual listener implementation may look as follows:

public class ExampleFieldInjectedExecutionListener implements ExecutionListener {
    private Expression fixedValue;
    private Expression dynamicValue;
   public void notify(DelegateExecution execution) throws Exception {
   String value =fixedValue.getValue(execution).toString() +
      execution.setVariable("var", value);

Thats it for now.

Abhishek Kumar

About Author

Abhishek Kumar

Abhishek kumar is a very bright resource and has gone from strength to strength. He is proactive and has demonstrated the ability to resolve complex issues

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us