Camunda SpringBoot Starter -2

Güvenç Kazancı
turkcell
Published in
4 min readAug 30, 2021

Introduction

In the first article of this series, I mentioned about how to;

  • start a camunda springboot project
  • secure the rest api
  • configure a database for camunda
  • deploy bpmn files
  • use camunda service beans

Here is a link for Camunda SpringBoot Starter -1.

In this second article, I will tell about how to;

  • develop Java service tasks
  • develop Java execution listeners
  • inject fields

Java Service Tasks

Service tasks are the components which helps us call Java backend processes inside a bpmn file. This can be achieved by creating Java classes which implements JavaDelegate interface. This interface comes with a method called execute. This method needs to be implemented by the developer and this is the place where you need to code your business logic.

Execute method provides a delegateExecution object as an input. This object gives us the ability to get and set variables in the running process instance. A part from accessing the variables, this object can get you any information related to running process instance such as root process id, jobs, tasks, activity name, etc.

Let’s open the camunda modeler and use this class in a service task. If you are not already aware of camunda modeler, you can download it from here.

I just created a dummy service task to log some variable to the console. Camunda will initiate a new object of that class type and run the execute method.

What if I need to use a spring service in that class such as a database service to persist some information ?

The answer is easy. Camunda provides us the ability to turn this class into a bean and use it as a DelegateExpression. We put “@Named” annotation at the top of our class to name our service task.

Now, to be able to use this we need to make another change in the bpmn file. Go to your service task and select Delegate Expression instead of Java Class. And replace class name with the bean name as below.

Be careful that this Java delegate object is a singleton bean and camunda will not initiate any new objects of that class anymore. So if you used private local variables in that class and try to change its value in the execute method, you could have issues because these variables can be overwritten by other threads.

Java Execution Listeners

Execution listeners allow you to execute external Java code similar to the Java delegate classes. The difference is that execution listeners are attached to the events of bpmn components.

For instance, they can be attached to “take event” of a transition or “start/end event” of a gateway.

Check below example code which logs the transition passed. See that when you implement ExecutionListener interface, you should implement “notify” method. You can also set some variables into your class in the bpmn file. This is called “field injection”.

Now to use this in the bpmn file, open camunda modeler and choose a transition and go to Listeners tab. As stated before, these listeners can be attached to any component event given in the bpmn specifications.

Similar to the java delegates, execution listeners can also be designed as singleton beans and used the same way.

Run Sample Flow

Let’s gather all these samples in a single bpmn and start a process instance and see the result.

I use below command to start an instance.

And here is the console output to see that execution listener and java delegates work as expected.

Sum Up

Here is the github link where you can find the project covering all the content mentioned in this post. You can check my following posts related to camunda and springboot.

--

--

Güvenç Kazancı
turkcell

Associate Principal Developer @ Turkcell Technology