One useful feature of jBPM is the ability to use subprocesses to break down complex processes into smaller, more manageable pieces. In this article, we’ll look at how to use subprocesses with jBPM 7.
First, let’s define what a subprocess is in the context of jBPM. A subprocess is a self-contained workflow that you can call from the same process or from another process. This allows you to reuse common tasks or activities within multiple processes, making it easier to manage and maintain your business processes.
You can define a subprocess from the designer UI by choosing the “subprocesses” icon which contains several types of subprocesses:
In this article we will cover two subprocess types: Reusable and Embedded subprocesses. There are a few key differences between these types of processes in terms of:
- Reusability: As the name suggests, you can use a reusable sub-processes in multiple process definitions. On the other hand you can use an embedded sub-process only within a single process definition.
- Maintenance: Reusable sub-processes are stored in separate process files. Instead, to modify an embedded subprocess you must modify directly the main process definition.
- Execution: A Reusable sub-processes will execute as a separate process instances. Instead, an embedded sub-process will execute as part of the main process instance. This can affect the way that variables and data are passed between the main process and the sub-process.
An example of Embedded subprocess
One of the benefits of using embedded sub-processes in jBPM is that they allow you to modularize and reuse parts of your process definitions. This can help you create more efficient and maintainable process flows, as you can easily reuse the sub-processes in multiple process definitions.
For example, here is a Process definition which contains an embedded subprocess:
As you can see, the “subprocess-embedded” is a module of the “embedded” Process definition. The variables defined in the embedded subprocess, by default, are local to the subprocess. Let’s demonstrate it.
Firstly, in the “Task1” node we will set the variable “name”:
kcontext.setVariable("name", "joe"); System.out.println("[embedded][Task1] Name is: " +kcontext.getVariable("name"));
Then, in the Task-In-SubProcess node, we will modify the variable:
Finally, in the “Task2” node we will print the variable “name”:
System.out.println("[embedded][Task2] Name is: " +kcontext.getVariable("name"));
if you try to run the above process, you will see that the Process variable “name” is distinct from the Embedded Process variable with the same name:
[embedded][Task1] Name is: joe [embedded][Task-In-Subprocess] Name is: frank [embedded][Task2] Name is: joe
However, to inject Process variables into the embedded subprocess, define the variable in the Process Variables panel of the Embedded subprocess:
An example of Reusable subprocess
Reusable sub-processes are stored in separate process files. If you add one from the Toolbar, you will see it appears collapsed within the parent process.
For example, here is a Process which will invoke our “embedded” Process definition as Reusable Process:
Next, to link the Sub-Process node with a reusable subprocess, select the target Process in the “Called Element” Panel:
One important aspect of reusable subprocesses is that you can pass argument to the Called Element. For example, we will pass the Process variable “passParam” to the SubProcess. The variable will be named “inputReuse” in the target subprocess.
Next, to test it, you output the value of “inputReuse” in the embedded Process:
Clearly, you need to inject an initial value for the variable “passParam” in the Parent Process:
Finally, by running the Parent Process, you can check the output of the embedded Process Task1, which prints the value of “inputReuse”:
Advanced Implementation parameters
A Reusable Subprocess has some additional options that can:
- Independent: If true, the sub-process will start as an independent process. If false, the active sub-process is canceled when the parent process terminates.
- Abort Parent: If true, non-independent reusable sub-processes can abort the parent process when there is an error during the execution of the called process instance.
- Wait for completion: If true, the On Exit Action will not execute until the sub-process instance terminates..
- Is Async: If true, the SubProcess will execute asynchronously.
- Multiple Instance: You can use it to execute the sub-process elements a specified number of times.
You can find the source code for this tutorial on GitHub: https://github.com/fmarchioni/mastertheboss/tree/master/jbpm/subprocess-demo1