Search Tutorials


Spring Boot + Camunda Service Task Java Example | JavaInUse

Spring Boot + Camunda Service Task Java 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 BPM Script Task Example. In this tutorial we had created script tasks for order and payment workflow. We make use of script tasks if the logic to be implemented is simple, self contained and no interaction with external systems is required. However if the logic is complex and needs to interact with various systems we make use of service tasks. We will now be creating a new service task which would send mails to users if the payment for the order has been successfull or not. This service task will be a java class.
Spring Boot + Camunda BPMN Service Task
A service task in Camunda is a task type that executes automated work, typically by calling external services, executing code, or integrating with other systems. It's represented by a gear icon in BPMN diagrams.
Implementation Types:
  • Java Delegate
  • Expression
  • Delegate Expression
  • External Service Task
  • Connector

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 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 3 + Camunda Script Task example tutorial.
We will be modifying the project to add the java service class for sending mail confirmations.
Spring Boot + Camunda BPMN Service Task Maven Example
The Process Order and Make Payment script tasks made use of the javascript files order.js and payment.js to define the tasks. In the order.js class we storing the total order value orderTotal and in the payment.js we were storing the paymentStatus value. In the java service class we will making use of these variables. The Java Service class will be as follows. We define a class named ConfirmationEmailDelegate which implements JavaDelegate interface, which is Camunda's way of implementing service tasks.
It overrides the execute method required by the JavaDelegate interface.
Retrieves two process variables using execution.getVariable():
  • orderTotal: Contains the transaction amount
  • paymentStatus: Contains the status of the payment
Using System.out.println() it simulates sending an email (in a real application, this would be replaced with actual email sending logic).
We now modify the TestController class to use the bpmn with id - third-javainuse
package com.example.delegate;

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

	public class ConfirmationEmailDelegate 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"));
	 }
}	 




So previous we had created a bpmn diagram as follows-
Spring Boot + Camunda BPMN Modeler
Next we will be modifying the camunda diagram to add the service task as follows. In the details we specify the service type as a java class. Also we specify the fully qualified name of the service task class.
Spring Boot + Camunda BPMN Modeler Service Task
We rename this bpmn file as third.bpmnn with id - third-javainuse. Copy this third.bpmn in the resources folder. Also we modify the spring boot application to make use of the third.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 - third-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("third-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 Example