Introduction to Activiti with Spring

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

1. Overview

Simply put, Activiti is a workflow and Business Process Management platform.

We can get started quickly by creating a ProcessEngineConfiguration (typically based on a configuration file). From this, we can obtain a ProcessEngine – and through the ProcessEngine, we can execute workflow & BPM operations.

The API provides various services that can be used to access and manage processes. These services can provide us information about the history of processes, what’s currently running and the processes that are deployed but not running yet.

The services can also be used to define the process structure and manipulate the state of the process, i.e. run, suspend, cancel, etc.

Here, runtimeService.startProcessInstanceByKey(“my-process”) starts the execution of the process whose key is “my-process”. runtimeService.createProcessInstanceQuery().count() will get us the number of process instances.

Every time we hit the path “/start-process”, a new ProcessInstance will be created and we’ll see an increment in the count of the currently running processes.

3. Playing with Processes

Now that we have a running process in Activiti using Spring Boot let’s extend the above example to demonstrate how we can access and manipulate the process.

3.1. Get the List of Tasks for a Given ProcessInstance

We have two user tasks A and B. When we start a process, it’ll wait for the first task A to be completed and then will execute task B. Let’s create a handler method that accepts requests to view the tasks related to a given processInstance.

The objects, like Task, cannot be sent as a response directly and hence we need to create a custom object and convert the Task to our custom object. We’ll call this class TaskRepresentation:

Here, taskService.createTaskQuery().processInstanceId(processInstanceId).list() uses TaskService and gets us the list of tasks related to the given processInstanceId. We can see that when we start running the process we created, we will get the task A by making a request to the method we just defined:

taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult() creates a query on the task service and gives us the task of the given processInstance. This is the UserTask A. The next line taskService.complete(task.getId) completes this task.
Hence, now the process has reached the end and the RuntimeService doesn’t contain any ProcessInstances. We can see this using the JUnit test case:

4. Conclusion

In this article, we went through the overview of using the Activiti API with Spring Boot. More information about the API can be found in the user guide. We also saw how to create a process and execute various operations on it using Activiti services.

Spring Boot makes it easy to use as we don’t need to worry about creating the database, deploying the processes or creating the ProcessEngine.

Keep in mind the integration of Activiti with Spring Boot is still in the experimental phase and it is not yet supported by Spring Boot 2.

As always, implementation of all the examples we saw can be found over on GitHub.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Hello
I’m a bit new to activiti. I have this scenario for a simple leave application where for an employee leave to be successiull approved by both supervisor and HR manager. All users and their roles are managed by spring boot application with spring security.

My issue

– Activiti has its own user management and groups. How do I make use of spring security with activiti? snippet will be helpful.