Search Tutorials


Spring Boot + Camunda Script Task Example | JavaInUse

Spring Boot + Camunda BPMN Script Task 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 Hello World Example. In the tutorial we had implemented a simple bpmn script task that output some text to the console. In this tutorial we will be looking at the script task in more detail. We will be creating script tasks of more practical scenario like an order and payment workflow.
Spring Boot + Camunda BPMN for order workflow

Video

This tutorial is explained in the below Youtube Video.

A script task in Camunda is a type of task in a business process model that allows you to execute a piece of code or script directly within the process flow. It's a way to add custom logic or perform specific operations without needing to create a separate Java class or service. Script tasks can be written in various scripting languages supported by Camunda, such as JavaScript, Groovy, or Python. They are useful for simple operations like data manipulation, calculations, or making decisions based on process variables. When the process reaches a script task, it executes the script and then moves on to the next task. You can use script tasks to:
  • Modify process variables
  • Perform calculations
  • Make decisions based on conditions
  • Interact with external systems
  • Log information

Implementation

Download the source code we had implemented in Spring Boot 3 + Camunda BPMN hello world example tutorial. So previous we had created a bpmn diagram as follows-
Spring Boot + Camunda BPMN Modeler
Also the script task it made use of inline javascript. Here the script task it printed some text to the console.
Spring Boot + Camunda BPMN Console
Next instead of inline script we will make use of external script. For this in resource folder create a new javascript file named test.js as follows -
print("Hello JavaInUse");
Then in the bpmn diagram select the script task -> In script type select External Resource -> Name the resource as test.js.
Spring Boot + Camunda BPMN External Resource
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 Taskscript

Spring Boot + Camunda BPMN External Resource Console




Global Variables

Global variables in a script task in Camunda refer to variables that are accessible throughout the entire process instance. These variables can be read and modified by different tasks within the process, including script tasks. Sometimes we may need to pass information between 2 script tasks. This is done using global variables. We will create a new bpmn diagram named second.bpmn having id as second-javainuse which will have 2 script tasks as follows-
Spring Boot + Camunda BPMN External Resource Console
  • Calculate Order Total(Task 1) - The first task calculates the total amount of an order, applies a discount if applicable, and stores the result in a global variable called "orderTotal".
  • Process Payment (Task 2) - The second task retrieves the "orderTotal" global variable, simulates a payment process, and sets another global variable "paymentStatus" based on the result.
Order Task script -
Spring Boot + Camunda BPMN Order Task
var System = Java.type('java.lang.System');

var orderItems = [
    { name: "Laptop", price: 1000 },
    { name: "Mouse", price: 20 },
    { name: "Keyboard", price: 50 }
];

var totalAmount = 0;
for (var i = 0; i < orderItems.length; i++) {
    totalAmount += orderItems[i].price;
}

if (totalAmount > 1000) {
    totalAmount *= 0.9;  // 10% discount
}

execution.setVariable("orderTotal", totalAmount);

System.out.println("Order total calculated: $" + totalAmount);
Payment Task script-
Spring Boot + Camunda BPMN Payment Task
var System = Java.type('java.lang.System');

var orderTotal = execution.getVariable("orderTotal");

var paymentSuccessful = (Math.random() < 0.8);  // 80% chance of success

if (paymentSuccessful) {
    System.out.println("Payment of $" + orderTotal + " processed successfully.");
    execution.setVariable("paymentStatus", "SUCCESS");
} else {
    System.out.println("Payment of $" + orderTotal + " failed. Please try again.");
    execution.setVariable("paymentStatus", "FAILED");
}
In the TestController specify the bpmn id as second-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("second-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 Script Task Example