Saturday, April 9, 2016

Provide a error/success message to the client while executing BPMN flow

Let's say we have a web application with a form, when we fill the form and click on submit button, it executes the BPMN workflow, then we want to give a success or error message to the user whether that particular action was succeeded or not.

BPMN process:

1. Start event (Give required information)
2. Allocate Consultant (Save data in database)
3. Send email (Send email to relevant parties)
4. Change status (get human interaction to continue the process)
5. another task....

How this executes;

When we start this process, it creates process instance, execute allocate consultant, send email and return response with process instance details, when it hits the user task.
But that response does not contain any information about whether those executed tasks succeeded or not.


Let's say when it executes allocate consultant task, it invokes database create operation; if it was failed, we want to give error message, else need to give success message.

So, we can have ErrorBoundryEvent on allocate consultant task; when an error occurred, ErrorBoundryEvent will trigger and continue the error handling flow and returned the process instance details

My trick is, when I start the instance I set a variable error_message = "No_ERROR", if it goes success flow that variable will not changed, if it goes error flow, I will change the variable in set error message script task.
Then when the process instance returned, I make another call and check the value of that error_message variable. and based on that I provide success or error message to the user.

Get the value of error_message variable:

Happy path:
When we start the process instance, we pass variable "errorMessage" with value eg: "NO_ERROR"
execute allocate consultant service task, send email and then when it hits the change status user task, it will return the process instance to the client
Then we do second rest call to check the value of errorMessage.

Error path:
When we start the process instance, we pass variable "errorMessage" with value eg: "NO_ERROR"
if it hit a database error at allocate consultant service task, it will go to the set error message script task, script task will set an error message to the variable errorMessage.
Then bps process instance will return to the client.
Then we do second rest call to check the value of errorMessage.

Here, in error handling flow, I have used TimerCatchingEvent, after the set error message script task, to wait 1 minute before end the process.

If process instance ends right after setting variable in script task, then when we do the second call there's no instance to get the value of variables. So then we have to make a rest call to BPMN historic data. But if the process goes in happy path then we have to make a rest call to BPMN runtime data. in two cases, we have to do two different calls, and we don't know which one we should call.
So we added intermediate timer catching event in between script task and end event to keep the process instance without killing for 1 minute. during that time we can do the second call and get the value of errorMessage variable from BPMN runtime.

Friday, April 8, 2016

Invoking REST API using BPMN

You can use BPMN Service Task and configure it as following to invoke a REST API

Example with text input and text output:

Example with JSON input and JSON output mapping and registry based URL:

You can find implementation of class here

Currently it supports GET and POST only. If you need support for other HTTP methods you have to implement custom java service task similar to REST Task implementation that supports rest of REST API functionalities.

How to see the execution flow of BPMN in WSO2 BPS

The latest version of WSO2 BPS 3.5.1, does not provide a mechanism to see the execution flow of BPS process. you can understand the flow enabling debug logs of acitivi engine.

Go to <BPS_HOME>/repository/conf/ and add the following

But it provides the current state of execution flow with the instance view of WSO2 BPS

login to WSO2 BPS
Go to Main->Manage -> Instances ->BPMN


WSO2 BPS documentation