How to create Activiti Message Event with Spring Boot Example

In this tutorial, we’re gonna look at Activiti Message Event Example with Spring Boot.

Related Articles:
Introduction to Activiti – a Java BPM Engine
Activiti Message Event with Spring Boot Example

I. Activiti Message Event Overview

A Message Event is a named message that always directs towards a single receiver.

To define Message Event, we use messageEventDefinition element. For example:

<message id="messageId" name="messageName" />

<process id="simpleProcess" name="Simple Process">

	<startEvent id="start_one">
		<messageEventDefinition messageRef="messageId" />
	</startEvent>
	...
</process>

Activiti Engine is not concerned with receiving messages. We have to implement our own infrastructure to deal with received messages. Then we can decide what to do with them.

In the example, we use message to trigger the start of a Process Instance. These are some methods (from Runtime Service):


ProcessInstance startProcessInstanceByMessage(String messageName);
ProcessInstance startProcessInstanceByMessage(String messageName, Map processVariables);
ProcessInstance startProcessInstanceByMessage(String messageName, String businessKey, Map processVariables);

II. Practice

1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.4.RELEASE
– Spring Boot: 1.5.3.RELEASE

2. Project Overview

activiti-message-event-process
We can start Process by one of two messages: messageOne and messageTwo. Before end Event, a Notification will throw a message in System Console.

activiti-message-event-structure

– Process definition will be written under XML format in process/process.bpmn20.xml file.
MyService provides service methods for Controller such as: startProcess(), getTasks(), completeTask(). Those functions can be done with help of some @Autowired objects which are instances of these classes:
+ RuntimeService
+ TaskService
MyController is a REST Controller which has request mapping methods for RESTful requests such as: /startprocess, /tasks, /completetask.
pom.xml contains dependencies for:
+ Activiti Spring Boot Starter Basic
+ Spring Boot Starter Web
+ H2 database: Activiti Engine needs a database to store its data. By default, it will connect to an in-memory H2 database.
+ Groovy: scripting engine for running Script Task in process.

Notes: We must add Groovy Jar file (groovy-all-2.4.10.jar) to make it work.

3. Step by step

3.1 Create Spring Boot project

Using Spring Tool Suite/Eclipse to create Project and add Dependencies to pom.xml file:

<dependency>
	<groupId>org.codehaus.groovy</groupId>
	<artifactId>groovy-all</artifactId>
</dependency>

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

<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-spring-boot-starter-basic</artifactId>
	<version>5.22.0</version>
</dependency>

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

– Add Groovy Jar file (groovy-all-2.4.10.jar) as Referenced Library.
(Groovy: scripting engine for running Script Task in process)

3.2 Define Process

Under src/main/resources, create processes folder and add process.bpmn20.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
	xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples">

	<message id="message_one" name="messageOne" />
	<message id="message_two" name="messageTwo" />

	<process id="simpleProcess" name="Simple Process">
		<startEvent id="start_one">
			<messageEventDefinition messageRef="message_one" />
		</startEvent>
		<sequenceFlow sourceRef="start_one" targetRef="theTask" />

		<userTask id="theTask" name="Task" activiti:assignee="${person}">
			<documentation>
				Do the task.
			</documentation>
		</userTask>
		<sequenceFlow sourceRef="theTask" targetRef="exclusiveGw" />

		<startEvent id="start_two">
			<messageEventDefinition messageRef="message_two" />
		</startEvent>
		<sequenceFlow sourceRef="start_two" targetRef="exclusiveGw" />

		<exclusiveGateway id="exclusiveGw" name="Exclusive Gateway" />
		<sequenceFlow sourceRef="exclusiveGw" targetRef="notification"></sequenceFlow>

		<scriptTask id="notification" name="Finish Notification"
			scriptFormat="groovy">
			<script>
				println 'finish Process, started by ' + message
			</script>
		</scriptTask>
		<sequenceFlow sourceRef="notification" targetRef="theEnd" />

		<endEvent id="theEnd" />
	</process>

</definitions>

3.3 Create Service Class

 getTasks(String assignee) {
		return taskService.createTaskQuery().taskAssignee(assignee).list();
	}

	public void completeTask(String taskId) {
		taskService.complete(taskId);
	}

}

3.4 Create Controller Class


package com.javasampleapproach.activiti.messageevent.controller;

import java.util.List;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.activiti.messageevent.service.MyService;

@RestController
public class MyController {

	@Autowired
	private MyService myService;

	@RequestMapping(value = "/startprocess/{message}")
	public String startProcessInstance(@PathVariable("message") String message, @RequestParam String assignee) {
		return myService.startProcess(message, assignee);
	}

	@RequestMapping(value = "/tasks/{assignee}")
	public String getTasks(@PathVariable("assignee") String assignee) {
		List tasks = myService.getTasks(assignee);
		return tasks.toString();
	}

	@RequestMapping(value = "/completetask")
	public String completeTask(@RequestParam String id) {
		myService.completeTask(id);
		return "Task with id " + id + " has been completed!";
	}

}

3.5 Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results by open your browser and send request:

Request 1: Start Process by MessageOne and assign tasks to John
localhost:8080/startprocess/messageOne?assignee=John


Process started

Request 2: show tasks of John
http://localhost:8080/tasks/John


[Task[id=11, name=Task]]

Request 3: complete Task
http://localhost:8080/completetask?id=11


Task with id 11 has been completed!

And System Console shows:


finish Process, started by messageOne

Request 4: Start Process by MessageTwo and assign tasks to John
localhost:8080/startprocess/messageTwo?assignee=John


Process started

And System Console shows:


finish Process, started by messageTwo

III. Source Code

SpringActivitiMessageEvent



By grokonez | May 17, 2017.

Last updated on May 5, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*