Skip to content

Custom Delegates

Custom Delegates allows external Java code to be executed when certain events occur during process execution. These can be used to access and wire business logic into the process.

Java Delegate

A custom delegate can be created by attaching a Java Delegate to a BPMN Service Task.

To create a class that can be called during process execution, the class must extend the com.ifsworld.fnd.bpa.config.TransactionalDelegate abstract Java class and provide the logic in the doExecute method. When the process arrives at the service task, it will execute the logic defined in the doExecute method.

Note that the classes referenced in the process definition are NOT instantiated during deployment. Some minor validation happens at deployment to verify the given class exists and is an extension of the TransactionalDelegate class. But no logic is checked. During the process execution, an instance of the class will be created when the execution arrives at the point where the class is used for the first time. If the class has been moved and cannot be found, a ProcessEngineException will be thrown.

The following is an example of a Java class that changes the process variable input string to uppercase.

public class ToUppercase extends TransactionalDelegate {
    public void doExecute(ExecutionWrapper execution) {
        CamundaAttributeSet camAttrSet = execution.getCamundaAttributeSet();
        String var = (String) camAttrSet.getValue("input");
        var = var.toUpperCase();
        camAttrSet.setVariable(“input”, var);
    }
}

Service Task that used the ToUpperCase Delegate

Below is the XML generated for the service task:

<bpmn:serviceTask id="Task\_0shq7da" name="To Uppercase"  camunda:class="com.ifsworld.fnd.bpa.process.ToUppercase">  
      <bpmn:incoming>SequenceFlow\_0zqi4k7</bpmn:incoming>  
</bpmn:serviceTask>

For more information, please refer to the Camunda Documentation Link.

Execution Listener

Execution listeners can be used to execute external Java code when certain events occur during process execution. The events that can be captured at the start or end of an activity.

To create a listener class that can be called during process execution, the class must extend the com.ifsworld.fnd.bpa.config.TransactionalListener abstract Java class and provide the logic in the doNotify method. When the process arrives at the service task, it will execute the logic defined in the doNotify method before the execution of service task or after the execution of service task depending on how you have configure the listener.

Note that the classes referenced in the execution listener are NOT instantiated during deployment. Some minor validation happens at deployment to verify the given class exists and is an extension of the TransactionalListener class. But no logic is checked. During the process execution, an instance of the class will be created when the execution arrives at the point where the class is used for the first time. If the class has been moved and cannot be found, a ProcessEngineException will be thrown.

The following is an example of a Java class that changes the process variable input string to uppercase before starting the execution of a service task.

public class ToUppercase extends TransactionalListener {
    public void doNotify(ExecutionWrapper execution) {
        CamundaAttributeSet camAttrSet = execution.getCamundaAttributeSet();
        String var = (String) camAttrSet.getValue("input");
        var = var.toUpperCase();
        camAttrSet.setVariable(“input”, var);
    }
}

Service Task that used the ToUpperCase Listener

Below is the XML generated for the service task:

<bpmn:serviceTask id="Activity_0od6rv1" name="To UpperCase">
  <bpmn:extensionElements>
    <camunda:executionListener class="com.ifsworld.fnd.bpa.process.ToUppercase" event="start" />
  </bpmn:extensionElements>
  <bpmn:incoming>Flow_0q8lg1a</bpmn:incoming>
  <bpmn:outgoing>Flow_01qndiv</bpmn:outgoing>
</bpmn:serviceTask>

For more information, please refer to the Camunda Documentation Link.