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.
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:
Also the script task it made use of inline javascript. Here the script task it printed some text to the 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.
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.
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-
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 -
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-
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