Search Tutorials


Spring Boot + Camunda BPMN Hello World Example | JavaInUse

Spring Boot + Camunda BPMN Hello World Example

In this tutorial we will be implementing Spring Boot 3 + Camunda BPM example. We will be looking at what is BPM, why should we use it. Camunda BPM (Business Process Management) is a lightweight, open-source platform for workflow and process automation.

BPM

Business Process Management (BPM) is like creating a detailed recipe book for how a company handles its day-to-day operations. Imagine you're running a customer service department in a big retail store. You deal with lots of customer complaints every day, and you want to make sure each one is handled efficiently and consistently. This is where BPM comes in. With BPM, we can create a clear, step-by-step process that everyone follows.
BPM for customer service department in a big retail store

Why use BPM instead of vanila code for development

The most important advantage of using BPM is the visual representation of the entire workflow it offers. Both technical and non technical stakeholders can better understand, give inputs and modify the BPM. BPM helps bridge the gap between business needs and IT implementation. Business analysts and developers can better interact and implement the business requirements.

Video

This tutorial is explained in the below Youtube Video.


Implementation

Camunda Modeler

We will first be downloading Camunda Modeler.
Spring Boot + Camunda BPMN - Download BPMN
Once downloaded and unzipped, open the Camunda Modeler.exe
Spring Boot + Camunda BPMN - Download BPMN
Create a bpmn diagram named first.bpmn. In this diagram we have
  • Start Event
  • Task
  • End Event
Specify the task as a script task and specify the script to print "Hello JavaInUse". The Task we will be specifying as a script task which will print "Hello JavaInUse" in the console.
Spring Boot + Camunda BPMN - Script Task

Spring Boot + Camunda BPMN - Script Task
For this bpmn specify the id as javainuse-first. In order to use this bpmn in the spring boot application we will make use of this id.
Spring Boot + Camunda BPMN Modeler

Spring Boot + Camunda

We will be creating a Spring Boot + Camunda BPMN project as follows-
Spring Boot + Camunda BPMN Hello World Example
Using the Camunda Initializr we will be creating a new spring boot project with camunda dependencies as follows-
Spring Boot + Camunda BPMN camunda initializr




At the end of this tutorial we will be having a spring boot maven project as follows-
Spring Boot + Camunda BPMN Maven Project
The pom.xml will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.workflow</groupId>
  <artifactId>javainuse-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>3.2.2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <dependency>
        <groupId>org.camunda.bpm</groupId>
        <artifactId>camunda-bom</artifactId>
        <version>7.21.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.bpm</groupId>
      <artifactId>camunda-engine-plugin-spin</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-all</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>3.2.2</version>
      </plugin>
    </plugins>
  </build>

</project>
As we will be executing javaScript in camunda script tasks, we need to add an additional nashorn-core dependency.
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.workflow</groupId>
  <artifactId>javainuse-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>3.2.2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <dependency>
        <groupId>org.camunda.bpm</groupId>
        <artifactId>camunda-bom</artifactId>
        <version>7.21.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.bpm</groupId>
      <artifactId>camunda-engine-plugin-spin</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-all</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
	
	 <dependency>
      <groupId>org.openjdk.nashorn</groupId>
      <artifactId>nashorn-core</artifactId>
      <version>15.4</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>3.2.2</version>
      </plugin>
    </plugins>
  </build>

</project>
Recent Camunda versions mandate History Time To Live (TTL) settings. TTL determines how long process instance data and variables are retained in the database before automatic deletion. Add the following to the application.yaml
camunda:
  bpm:
    generic-properties:
      properties:
        historyTimeToLive: 8
Next we will be creating a RestController named TestController. The controller uses Camunda's Process Engine API to interact with the workflow engine. It demonstrates how to start a specific process programmatically within a web application context. The actual process logic is defined in the BPMN file we created previously with the id "first-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("first-javainuse");
		instance.executeWithVariablesInReturn();
		return "Executed Camunda BPMN";
	}
}
Start the spring boot application. In the browser use the url - localhost:8080/executetask
Spring Boot + Camunda BPMN REST Controller
We can see that in the console, Hello JavaInUse gets printed
Spring Boot + Camunda BPMN Console

Download Source Code

Download it -
Spring Boot 3 + Camunda Example