Flowable API
Jmix BPM uses the Flowable process engine. Your code works with processes through the Flowable API.
Process Engine Services
The process engine provides its API as services. The following table lists the Flowable services and shows which of them Jmix BPM uses. For details on the Flowable services, see The Process Engine API and services in the Flowable documentation.
| Flowable service | What it does | Used by Jmix BPM |
|---|---|---|
|
Deploys processes, queries process definitions, suspends and activates them. |
|
|
Starts process instances, reads and changes process variables, sends messages and signals. |
Yes. See Starting a Process Programmatically. |
|
Queries, claims, assigns and completes user tasks. |
Yes, as |
|
Queries the data of running and finished process instances, tasks and variables. |
Yes. See History. |
|
Queries and runs jobs, such as timers and asynchronous continuations. |
Yes. See Job Executor. |
|
Migrates process instances to another version of the process definition. |
Yes, for instance migration. |
|
Changes properties of a deployed process definition without a new deployment. |
No. |
|
Works with Flowable start and task forms. |
No. Jmix BPM has its own process forms. |
|
Manages Flowable users and groups. |
No. Jmix BPM takes users from the application and user groups from the User Groups view. |
Jmix BPM adds its own services in the io.jmix.bpm.service package:
| Jmix BPM service | What it does |
|---|---|
|
The task service of the engine. It extends |
|
Returns all user groups, the groups of a user and the users of a group. |
|
Evaluates an expression for a process execution or with the given variables. |
|
Returns the extension properties of a BPMN element in a process definition. See Extension Properties. |
|
Sends an email. The Send Email Task uses it. Available when the project has the Email add-on. |
The services are Spring beans, so you can inject them into your beans:
@Component
public class OrderApprovalService {
@Autowired
private RuntimeService runtimeService;
In code that isn’t a Spring bean, get the services from the process engine:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
BpmTaskService bpmTaskService = (BpmTaskService) processEngine.getTaskService();
Flowable DMN API
For the API of the DMN engine, see DMN API in the Flowable documentation.
Examples
Starting a Process Programmatically
When the user clicks Start process, the following detail view saves the order and starts the order approval process for it:
@Autowired
private RuntimeService runtimeService;
@Subscribe("startProcessBtn")
public void onStartProcessBtnClick(final ClickEvent<JmixButton> event) {
closeWithSave().then(() -> { (1)
Order order = getEditedEntity();
Map<String, Object> processVariables = new HashMap<>();
processVariables.put("order", order); (2)
runtimeService.startProcessInstanceByKey("order-approval", (3)
order.getNumber(), (4)
processVariables);
});
}
| 1 | Starts the process after the order is saved. The order process variable keeps only the id of the entity, and the process loads the order by this id. |
| 2 | Puts the order to the order process variable. |
| 3 | order-approval is the process definition key. |
| 4 | The order number becomes the business key of the process instance. |
Getting a List of User Tasks
The following method returns the active tasks of the order approval process that are assigned to the current user:
@Autowired
private TaskService taskService;
@Autowired
private CurrentAuthentication currentAuthentication;
public List<Task> getMyApprovalTasks() {
return taskService.createTaskQuery() (1)
.processDefinitionKey("order-approval") (2)
.taskAssignee(currentAuthentication.getUser().getUsername()) (3)
.active()
.orderByTaskCreateTime()
.desc()
.list();
}
| 1 | Creates a task query. |
| 2 | Selects the tasks of the order-approval process. |
| 3 | Selects the tasks assigned to the current user. |
Getting a List of Process Instances
The following method returns the active instances of the order approval process for an order. It finds them by the business key that the detail view above sets:
@Autowired
private RuntimeService runtimeService;
public List<ProcessInstance> getActiveApprovals(Order order) {
return runtimeService.createProcessInstanceQuery() (1)
.processDefinitionKey("order-approval") (2)
.processInstanceBusinessKey(order.getNumber()) (3)
.active()
.list();
}
| 1 | Creates a process instance query. |
| 2 | Selects the instances of the order-approval process. |
| 3 | Selects the instances with the order number as the business key. |
To find process instances by a process variable, use variableValueEquals() instead of processInstanceBusinessKey().
Using BpmTaskService
BpmTaskService extends TaskService with the completeTaskWithOutcome() method, which completes a user task with an outcome. Gateways can route the process by the outcome, see User Task Outcomes.
The following method completes an order approval task with the approve outcome:
@Autowired
private BpmTaskService bpmTaskService;
public void approve(Task task, String comment) {
Map<String, Object> processVariables = new HashMap<>(); (1)
processVariables.put("comment", comment);
bpmTaskService.completeTaskWithOutcome(task.getId(), "approve", processVariables); (2)
}
| 1 | completeTaskWithOutcome() adds the outcome to this map, so don’t pass an immutable map, such as Map.of(). |
| 2 | approve is the outcome id. |
Working with Process Definitions via the API
RepositoryService deploys and manages process definitions.
To deploy a BPMN file from the classpath:
public Deployment deployOrderProcess() {
return repositoryService.createDeployment()
.addClasspathResource("processes/order-process.bpmn20.xml")
.deploy();
}
Each deployment creates a new version of the process definition. To find the latest version by the process definition key:
public ProcessDefinition findLatestDefinition(String processDefinitionKey) {
return repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey)
.latestVersion()
.singleResult();
}
A suspended process definition can’t start new process instances. To suspend a process definition and activate it again:
public void suspend(ProcessDefinition processDefinition) {
repositoryService.suspendProcessDefinitionById(processDefinition.getId());
}
public void activate(ProcessDefinition processDefinition) {
repositoryService.activateProcessDefinitionById(processDefinition.getId());
}
When you deploy a BPMN file with diagram data, the engine also generates an image of the process diagram. To get it:
public InputStream getDiagram(ProcessDefinition processDefinition) {
return repositoryService.getProcessDiagram(processDefinition.getId());
}
Setting and Updating a Business Key
To set the business key of a process instance, pass it to startProcessInstanceByKey(), as the detail view above does. To change it later, call updateBusinessKey() of RuntimeService:
public void updateBusinessKey(ProcessInstance processInstance, Order order) {
runtimeService.updateBusinessKey(processInstance.getId(), order.getNumber());
}