Camunda Team Blog

Scripting Everywhere in camunda BPM

Written by Sebastian Menski on , under Execution category.
With the last camunda BPM 7.2.0-alpha3 release we heavily extended the scripting support of the camunda BPM platform. We started to improve scripting support with the 7.2.0-alpha1 release and now we think we are done. It is now your turn to start using the different scripting languages and help us to polish this feature for our final release. To get an detailed overview of the scripting support please visit our user guide.

Where can I use scripts? 

Everywhere but to be more specific you can use scripts as:
  • Script tasks (of course)
  • Execution listener
  • Task listener
  • Condition of sequence flows
  • Input output mapping (added in 7.2.0-alpha2)
The implementation of the scripting support in camunda BPM is designed to be independent from BPMN which will allow us to also support scripting in our new CMMN implementation right from the beginning.

Why should I use scripts?

First of all scripting is fun. But besides that there are also more convincing reasons to use scripting in camunda BPM.

With the extended scripting support it is easy to prototype complete processes including the business logic. Also the sharing of such prototypes is simple, because the whole code can be embedded inside the process. So only one file has to be exchanged and discussed. In combination with the BPMN model API it allows you to create mock processes with business logic directly inside your code.

Another advantage is that now also non-Java programmers can use camunda BPM to execute their processes without writing Java code. Or if you already have an non-Java code base you can now use it inside camunda BPM without porting it to Java.

We also allow you to reuse scripts by using the camunda:resource attribute. Which loads scripts from external resources. Either the scripts can be part of your deployment or reside in the classpath of your application. To get more information on this please see the corresponding section in the user guide.

Let there be scripts

To demonstrate the new feature I created a small example process without a single line of Java code. The following process simulates a dice game in a casino.
The first process element is a script task which should welcome the new player.

We use a input parameter mapping to randomly select our new player. Therefore a groovy script is evaluated and the result of the last script line is saved to the local variable player. Inside the script task than another groovy script just welcomes the newly select player with a kind message.

At the start of the exclusive gateway we roll the dice. To do so we use an execution listener for the start event of the gateway.

The execution listener is another script which saves the result as a process variable so it can be used to evaluate the following sequence flow conditions. The variable is set with the help of the execution variable which corresponds to the DelegateExecution interface.

The conditions of the sequence flow could be expression language but this is a blog post about scripting so lets use a script for that.

Both groovy scripts just evaluate the process variable set by the exclusive gateway.

The last action is to inform the system which user task should be executed. For that we add a task listener to both user tasks for the create event.

The task listeners are again groovy scripts which uses the task variable to print the task name and the event. The task variable corresponds to the DelegateTask interface.

So that's it. A whole process without a single line of Java code. To see the complete BPMN xml have a look at this gist. Or if you want directly start playing with it you can clone a test project here.

Now it's your turn. Have fun and start scripting. And if you have any remarks our if you miss a features please let us know.