Search Tutorials


Spring Boot + Camunda Service Task Expression Example | JavaInUse

Spring Boot + Camunda Service Task - 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 Expression Delegate Example. 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
When using Delegate Expression and Java Delegate, the service java class has to implement JavaDelegate interface and override execute method in which we define our logic. In this tutorial we will be implementing service task using expression. When using expression, the service class does not need to implement the JavaDelegate interface and override execute method. We can define the logic in any method we want.
Spring Boot + Camunda BPMN Expression Service Task

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 Expression Service Task
Previously for camunda bpmn we had specified the service task as a delegate expression.
Spring Boot + Camunda BPMN Delegate Expression Service Task
Now we will make use of expression. Now instead of using delegate expression for specifying the service task we will be making use of expression where we specify the bean name along with the method name. So select the implementation as a delegate expression. The expression will be of the format - ${beanName.methodName(execution)}. The execution variable is automatically available in the expression context in Camunda. In our case the bean name will be the java class name in camel case along with the method name which is ${confirmationEmail.sendMail(execution)}
Spring Boot + Camunda BPMN Expression Service Task
Create a new class named ConfirmationEmailExpression. This will be similar to the previous ConfirmationEmailExpressionDelegate class we had created.
package com.example.workflow.delegate.mail;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

@Component
public class ConfirmationEmail {
	public void sendMail(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 - fifth-javainuse. Copy this fifth.bpmn in the resources folder. Also we modify the spring boot application to make use of the fifth.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 - fifth-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("fifth-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 - Expression Example