Search Tutorials


Spring Boot + Camunda Service Task Delegate Expression Example | JavaInUse

Spring Boot + Camunda Service Task - Delegate Expression Example

Camunda BPM (Business Process Management) is a lightweight, open-source platform for workflow and process automation. In previous tutorial we implemented Spring Boot 3 + Camunda Service Task Java Delegate Example. In this tutorial we had looked at what are service tasks and when we should use them.
Implementation Types:
  • Java Delegate
  • Expression
  • Delegate Expression
  • External Service Task
  • Connector
We had implemented a service task using java delegate for sending confirmation mails with order details.
In this tutorial we will be implementing service task using expression delegate.
Spring Boot + Camunda BPMN Delegate Expression Service Task

Video

This tutorial is explained in the below Youtube Video.

Spring Boot Camunda Tutorials

Spring Boot + Camunda Hello World Example. Spring Boot + Camunda Script Task Example. Spring Boot + Camunda Service Task - Java Delegate Example. Spring Boot + Camunda Service Task - Delegate Expression Example. Spring Boot + Camunda Service Task - Expression Example.

Implementation

Download the source code we had implemented in Spring Boot + Camunda Service Task - Java Delegate Example.
The maven project we will be creating is as follows-
Spring Boot + Camunda BPMN Delegate Expression Service Task
Previously for camunda bpmn we had specified the service task as fully qualified java class. But suppose if due to some reason we change the package of the java delegate class. In such a scenario the service task will not get called correctly. Obviously the solution for this would be to modify the correct the fully qualified java class named in camunda. But suppose there are multiple such changes then it will be difficult to modify these changes in camunda.
Spring Boot + Camunda BPMN Java Service Task
This is why we should use delegate expression instead of the fully qualified java class name. Create a new class named ConfirmationEmailExpressionDelegate. This will be similar to the previous ConfirmationEmailDelegate class we had created.
package com.example.workflow.expression.delegate;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class ConfirmationEmailExpressionDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) throws Exception {
		System.out.println("Sending mail that transaction of order of amount " + execution.getVariable("orderTotal")
		+ " is a " + execution.getVariable("paymentStatus"));
		}
}




Now instead of using fully qualified java class name for specifying the service task we will be making use of delegate expression where we specify the bean name. So select the implementation as a delegate expression. The delegate expression will be of the format - ${beanName}. In our case the bean name will be the java class name in camel case which is ${confirmationEmailExpressionDelegate}
Spring Boot + Camunda BPMN Delegate Expression Service Task
Also since we are specifying the bean name, we add the Component annotation to the ConfirmationEmailExpressionDelegate class. Now even if we change the package name of the ConfirmationEmailExpressionDelegate class, the service task will still get called correctly as we are calling it by the bean name.
package com.example.workflow.expression.delegate;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

@Component
public class ConfirmationEmailExpressionDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) throws Exception {
		System.out.println("Sending mail that transaction of order of amount " + execution.getVariable("orderTotal")
		+ " is a " + execution.getVariable("paymentStatus"));
		}
}
We rename this bpmn file as fourth.bpmnn with id - fourth-javainuse. Copy this fourth.bpmn in the resources folder. Also we modify the spring boot application to make use of the fourth.bpmn we just created.
Spring Boot + Camunda BPMN Modeler Service Task
Also in the controller class we make the change to use the bpmn diagram with id - fourth-javainuse
package com.example.workflow;

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.runtime.ProcessInstantiationBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@GetMapping("/executetask")
	public String execute() {

		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		ProcessInstantiationBuilder instance = engine.getRuntimeService().createProcessInstanceByKey("fourth-javainuse");
		instance.executeWithVariablesInReturn();
		return "Executed Camunda BPMN";
	}
}
Start the spring boot application and then go to the url - http://localhost:8080/executetask. If we now run the program we get the output as follows.
Spring Boot + Camunda BPMN Order and Payment Task

Download Source Code

Download it -
Spring Boot 3 + Camunda Service Task - Delegate Expression Example