Flowable Notes for Beginners

Flowable Notes for Beginners

1. Flowable is a set of process and tasks inside it.

Example snippet (as per bpmn20.xml):

Below snippet is only for understanding on how the process and tasks will be created in flowable for any bpmn process.

Explanation:

  • couponupload is process id/key created to upload the coupons for a particular shop.
  • This process will have set of tasks in order to complete the whole process, here totally 2 process, one is store already partnered or not and whether the category already available or not.

[xml]

<process id=”couponupload” name=”Coupon Upload BPMN Process” isExecutable=”true”>
<userTask id=”store-already-partnered” name=”Check whether the store is already partnered or not” flowable:assignee=”${ storeName }” flowable:candidateGroups=”InternalMaintenanceTeam” flowable:dueDate=”${ storeCreationDueDate }”>
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler=”http://flowable.org/modeler”><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>

<userTask id=”category-already-available” name=”Check whether the particular shops category is available or not” flowable:assignee=”${ categoryName }” flowable:candidateGroups=”InternalMaintenanceTeam”>
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler=”http://flowable.org/modeler”><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>

</process>

[/xml]

2. Installing Flowable and configuring with Apache Tomcat

You have to download flowable-6.4.0(latest while writing this article) and download the apache tomcat, then move all the below five war files to webapps then do tomcat restart for the deployments.

  1. flowable-admin.war
  2. flowable-idm.war
  3. flowable-modeler.war
  4. flowable-rest.war
  5. flowable-task.war

Then you will be able to see the list of flowable wars deployed in the manager of apache tomcat in this url
http://localhost:8080

Url to access:
http://localhost:8080/flowable-admin/#/engine

Default username and password:
username: admin
password: test

Upload your bpmn20.xml file in flowable-modeler, probably in this page http://localhost:8080/flowable-modeler

Note: Apache tomcat may be asking you to enter username and password for manager-gui/console, for that you have to do the below simple step.

Go to apache/conf/tomcat-users.xml file and paste the below snippet, (please take the backup of tomcat-users.xml file  before modifying anything)

[xml]

<?xml version=’1.0′ encoding=’utf-8′?>
<tomcat-users>
<role rolename=”manager-gui”/>
<user username=”root” password=”root” roles=”manager-gui”/>
</tomcat-users>

[/xml]

 

For more details read the below link,

Tomcat 7, 8, 9 Default password setting guidelines.

 

3. RuntimeService is a class available to start bpmn process.

[java]

@Autowired
private RuntimeService runtimeService;

[/java]

Here RuntimeService is autowired and started the flowable process using startProcessInstanceByKey method,

[java]

runtimeService.startProcessInstanceByKey(“couponupload”,processMeta);

[/java]

Note: couponupload is the process id mentioned in the xml above(or the Model key as per the flowable diagram created, which looks similar to the below popup), processMeta can be passed to process as maps.

4. TaskService is also a class available in flowable to initiate / play / work around with the flowable tasks.

[java]

@Autowired
private TaskService taskService;

[/java]

Fetching the list of tasks for a particular assignee:

[java]

// this just fetches the list of tasks by the assignee name.

List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).orderByDueDateNullsLast().desc().list();

[/java]

5. Deleting a process with process_id

Process_id can be taken from the “act_ru_task” table “PROC_INST_ID_” column

[java]

@Autowired
private RuntimeService runtimeService;

runtimeService.deleteProcessInstance(processInstanceId, “test”);

@DeleteMapping(value = “/process/{processInstanceId}”)
public boolean deleteLmImplementationByProcess(@PathVariable(“processInstanceId”) String processInstanceId) {

runtimeService.deleteProcessInstance(processInstanceId, “test”);

return true;
}

[/java]

 

6. How to get the process varialbles saved back from flowable tables

//Here executionid, is the task key id.

[java]

Map<String, Object> processVariables = runtimeService.getVariables(task.getExecutionId());

String taskDefKey = StringUtils.trimToEmpty(task.getTaskDefinitionKey());

// this returns the coupons process variables saved.

processVariables.get(“coupons”);

[/java]

7. How to create a diagram in flowable ?

Flowable Modeler can be used to create any new BPMN process flowable diagrams. Please make sure flowable installation is completed properly before trying to open the below link,

http://localhost:8080/flowable-modeler/#/editor/

Process -> Create Process

We have different flows like manual task, end event, choice gateway, timer trigger, intermediate events etc.

Along with that you will also find a different tasks like user task/Script task/Business rule task/Receive task/Manual task/Mail task/Camel task/Http task/Mule task/Send task/Shell task/Decision task.

Mainly we need to fill the fields for every tasks like id(unique to identity and process the task), assignee to take the flow properly with respect to the allowed flowable users, Delegate expression like sending/notifying some updates to involved groups and category(this one can be reused to identify if different flows are using the same functionality).

8. How to configure a Delegate services in Flowable ?

In the flowable, we may require sms/email notifications sometime, this can be configured using JavaDelegate provided in the flowables.

This will be triggered when delegate expression enabled in the BPMN processes.

[java]

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

@Component(“smsDelegate”)
public class SMSDelegate implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) {
// your business goes here.
}

}

[/java]

9. Working with Execution Listeners:

If you want to listen to some task and do some other task then execution listeners are good option in flowable.

Here in this case smsListener task will be listened here and executes the notify method, so that if anything else needs to be run in parallel with sms tasks then this can be utilized effectively in flowable.

[java]

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

@Component(“smsListener”)
public class SmsListener implements ExecutionListener {

@Override
public void notify(DelegateExecution execution) {
// your business goes here
}
}

[/java]

 

10. How to create Dynamic Tasks in Flowable and Map against particular process ID?

[code lang=”java”]
@Autowired
TaskService taskService;

Task flowableDynamicTask = taskService.newTask();
flowableDynamicTask.setName(“MyDynamicFlowableTaskName”);
flowableDynamicTask.setAssignee(“Assignee Name”);
taskService.saveTask(flowableDynamicTask);
[/code]

If you like to map this dynamic flowable task agains any process, then before saving / calling .saveTask() method set the process Id Also for the flowable dynamic tasks.

 

11. How to trigger a particular task or process in asynchronous way ?

When we are doing some synchronous calls and waiting for some kind of asynchronous responses from any external systems, then triggering the task after that async call is really a important stuff to know and work to allow the flowable to flow when we are using flowable BPMN.

Here you can consider passing your process instance id and activity id to your synchronous call itself, because these both are mandatory to get the flowable execution object to call trigger method as it internally takes the execution ID.

[code lang=”java”]

@Autowired
RuntimeService runtimeService;

public void triggerFlow(){

// pass YOURPROCESSINSTANCEID and YOURACTIVITYID to get the flowable execution object

Execution execution = runtimeService.createExecutionQuery().processInstanceId(YOURPROCESSINSTANCEID)
.activityId(YOURACTIVITYID).singleResult();

runtimeService.trigger(execution.getId());

}

[/code]

 

12. How to start the flowable BPMN process from your main java class ?

This below main method will load the database from your localhost and tries to load the coupon_publish_workflow.xml bpmn from src/main/resources/flowable-processes folder.

And inside the sample xml it has lot of process and subprocesses, we have to give the main process key that is coupon_publish_start in the runtimeservice.startProcessInstanceByKey method to start the BPMN process from your own created xml BPMN.

[code lang=”java”]

public static void main(String[] args) {
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl(“jdbc:mysql://localhost:3306/flowable”).setJdbcUsername(“root”).setJdbcPassword(“root”)
.setJdbcDriver(“com.mysql.jdbc.Driver”)
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

ProcessEngine processEngine = cfg.buildProcessEngine();

RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource(“flowable-processes\\coupon_publish_workflow.xml”)
.deploy();

ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).singleResult();
System.out.println(“Found process definition : ” + processDefinition.getName());

RuntimeService runtimeService = processEngine.getRuntimeService();

// refer the key in coupon_publish_workflow.xml file.
ProcessInstance pi = runtimeService.startProcessInstanceByKey(“coupon_publish_start”);
}

[/code]

 

 

13. How to trigger or set the process variables for a particular process using flowable rest ?

Below is the link which can be used to set the process variable against the particular process definition key.

Here admin is username and test is password, which is directly mentioned in the below url and the same can be hit using Postman.

http://admin:test@localhost:8080/flowable-rest/service/runtime/process-instances

[I assume you have already completed flowable setup steps and able to access localhost:8080 and flowable stuffs]

Sample Body Request:

[code]
{ “processDefinitionKey”: “your_flowable_process_definition_key”,

“returnVariables”: true,
“variables”: [{
“name”: “your_variable”,
“value”: “value”}
]}
[/code]

Leave a Reply