Journal

Using Activiti Workflow With Spring Boot 3

A practical guide on integrating Activiti's workflow engine with Spring Boot for seamless business process automation. Learn to embed BPMN 2.0-based workflows, simplify configuration with starters, and orchestrate complex processes within a

Hogy Pitersan Melsy
Hogy Pitersan Melsy

Learning and creating through code, but also values reflection and journaling as a way to grow. Writing helps me capture ideas, track progress, and make sense of my thoughts. This journal is a space where I share pieces of my journey—sometimes about tech.

A practical guide on integrating Activiti's workflow engine with Spring Boot for seamless business process automation. Learn to embed BPMN 2.0-based workflows, simplify configuration with starters, and orchestrate complex processes within a robust microservice architecture. This article provides a step-by-step walkthrough, from setup to deployment.

Create Spring Boot Workflow With Activiti

What is a Workflow?

A workflow is a sequence of connected tasks, steps, and activities that are carried out in a specific order to achieve a defined outcome. It provides a structured, repeatable, and organized way to get work done, guiding a process from its initiation to its completion. Workflows can be simple, linear checklists or complex, diagrammed processes involving multiple people, departments, and systems.

Key Components of a Workflow:

  • Inputs: The resources, information, or tasks that start the process.

  • Transformation: The series of actions, decisions, and operations that process the inputs.

  • Output: The final result or outcome of the workflow.

  • Actors: The people or automated systems responsible for performing each task

How Workflows are Used

By defining a clear workflow, organizations can achieve greater efficiency, consistency, and visibility. Instead of relying on ad-hoc or disorganized methods, a workflow ensures that every task is performed correctly and in the right sequence.

Examples of workflows are found in almost every industry and department, including:

  • Invoice Processing: A workflow might track an invoice from its receipt to verification, approval, and finally, payment.

  • Employee Onboarding: A workflow can manage all the necessary steps for a new hire, such as sending the offer letter, setting up IT access, and arranging training.

  • Customer Support: When a customer submits a ticket, a workflow can automatically route it to the appropriate support agent and guide the process of finding a solution.

  • Content Creation: A workflow can outline the steps from content ideation and creation to editing, approval, and publishing

Benefits of Using Workflows

Implementing effective workflows offers numerous benefits for an organization:

  • Improved Efficiency: By standardizing tasks, workflows eliminate manual effort and reduce bottlenecks.

  • Consistency and Quality: A defined process ensures that tasks are completed with the same level of quality every time, reducing errors.

  • Increased Visibility: Workflows provide a clear overview of the process, allowing managers to track progress and identify potential issues.

  • Accountability: Each step has a designated owner, making it clear who is responsible for which tasks.

  • Repeatability: Workflows are designed to be repeatable, which is crucial for scalable and reliable operations

Activiti Workflow

Activiti is a lightweight, open-source Business Process Management (BPM) and workflow platform written in Java, featuring a fast and reliable BPMN 2.0 process engine that automates complex business processes involving human tasks and service calls. By defining processes using the standard BPMN 2.0 notation, organizations can visually diagram and manage workflows for consistent execution. For more information, visit https://www.activiti.org/

Spring Boot Activiti Integration

Integrating Activiti with Spring Boot offers an efficient and rapid way to orchestrate business process management (BPM) within Java-based applications. By combining Activiti's powerful process automation capabilities with Spring Boot's auto-configuration and ease of use, developers can build production-ready microservices with embedded workflow functionality.

Key Benefits of Activiti and Spring Boot Integration

  • Simplified Configuration: By simply adding the activiti-spring-boot-starter dependency, Spring Boot automatically configures the Activiti process engine, including the creation of a DataSource and ProcessEngine, eliminating much of the complex manual setup.

  • Microservice-Based Development: This combination is ideal for microservice architectures. Activiti can be used to connect various microservices, managing complex and long-running business processes that may involve interactions between different services.

  • Human Workflow Integration: The integration makes it easy for developers to incorporate tasks that require human interaction (e.g., approvals, data validation) into a workflow defined using the BPMN 2.0 standard.

  • Ease of Development: Developers can define their business processes using BPMN files (.bpmn20.xml) and place them in the src/main/resources/processes directory. Spring Boot will automatically detect and deploy these process definitions when the application starts.

  • Separation of Concerns: This integration allows for a clear separation between the business process logic (managed by Activiti) and the functional implementation code (managed by Spring Boot). The Java code for process steps, such as calling a service or performing validation, can be implemented as Spring components (@Component or Service) and invoked from within the Activiti process.

  • Integration with the Broader Spring Ecosystem: Activiti integrates seamlessly with other components in the Spring ecosystem, such as Spring Security for identity and authorization management and Spring Integration for orchestrating work between different systems

How to Integrate Spring Boot with Activiti

The integration of Activiti and Spring Boot provides a powerful foundation for building process-driven applications, allowing developers to focus on core business logic rather than complex infrastructure configuration.

Add some dependency for Activiti

Pom.xml

<dependency>
  <groupId>org.activiti</groupId>
  <artifactId>activiti-spring-boot-starter</artifactId>
  <version>${activiti.version}</version>
</dependency>

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

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

Config your database connection

Application.properties

# application.properties
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://your-ip:port/db-name
spring.datasource.username=your-db-username
spring.datasource.password=your-db-password

# Hikari
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.pool-name=your-application-name

don't forget to set hikari auto-commit=true if you using JPA HikariCP, this config to makesure activiti create the coresponding table, or you can try add spring.jpa.hibernate.ddl-auto=none to application.properties

Configurations

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class DataSourceConfiguration {

    @Autowired Environment environment;

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        //Database config
        config.setDriverClassName(environment.getProperty("spring.datasource.driverClassName"));
        config.setJdbcUrl(environment.getProperty("spring.datasource.url"));
        config.addDataSourceProperty("user", environment.getProperty("spring.datasource.username"));
        config.addDataSourceProperty("password", environment.getProperty("spring.datasource.password"));

        //Hikari config
        config.setMaximumPoolSize(Integer.parseInt(environment.getProperty("spring.datasource.hikari.maximum-pool-size")));
        config.setMinimumIdle(Integer.parseInt(environment.getProperty("spring.datasource.hikari.minimum-idle")));
        config.setIdleTimeout(Integer.parseInt(environment.getProperty("spring.datasource.hikari.idle-timeout")));
        config.setConnectionTimeout(Integer.parseInt(environment.getProperty("spring.datasource.hikari.connection-timeout")));
        config.setMaxLifetime(Integer.parseInt(environment.getProperty("spring.datasource.hikari.max-lifetime")));
        config.setAutoCommit(Boolean.parseBoolean(environment.getProperty("spring.datasource.hikari.auto-commit")));
        config.setPoolName(environment.getProperty("spring.datasource.hikari.pool-name"));

        return new HikariDataSource(config);
    }
}

Create your sample workflow (BPMN Process)

create xml file on folder resources/processes

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             targetNamespace="http://www.activiti.org/test">
    <process id="startWorkflow" name="My Process">
        <startEvent id="startEvent"/>
        <sequenceFlow sourceRef="startEvent" targetRef="serviceTask"/>
        <serviceTask id="serviceTask" name="Workflow Process"
                     activiti:class="com.bcal.o2obackoffice.workflow.WorkflowProcess"/>
        <sequenceFlow sourceRef="serviceTask" targetRef="endEvent"/>
        <endEvent id="endEvent"/>
    </process>
</definitions>