Java Delegate Vs Expressions in Service task

Hi,
Am new to camunda, while going through the service tasks came across Java delegate, Delegate Expression and Expressions. Just want to know which one is preferred over others. And the benefit of it.
If I use Java Delegate / delegate Expressions then the implementation should be sub class of JavaDelegate interface, by implementing execute method. If we go with this, then i would be writing lots of delegate classes.
If I go with spring bean Expressions, then I can invoke the method directly from the process, there I see the number of classes used can be minimal.
So, want to know when to go for Delegates and when to go with Expression.

1 Like

Hi @Silambarasan_S
Welcome to the forum and thanks for the question.

If your business logic is written in Java then you’re likely to want to use the implementation type of Java Classor Delegate Expression in both cases they would be calling a Java class which implements a JavaDelegate The difference is mainly based on how they are called.

If you have a class like this:

package com.example.workflow;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

import javax.inject.Named;

@Named("TestingBear")
public class TestingFun implements JavaDelegate {
    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        // Business Logic
    }
}

Java Class would be called through the package name package com.example.workflow.TestingFun

Delegate Expression If you’re using beans you can call the class by it’s name, in the above example the bean has a @Named property so you can just call it by the name e.g. #{TestingBear}

Usually Delegate Expression is preferred because it becomes much easier to manage things like refactoring and replacing business logic. So i would tend go with that.

3 Likes

Hi @Silambarasan_S,

in addition to Niall’s explanation,

Expressions are good to reuse existing beans.

If you already have beans with methods that you want to use as implementation for service tasks, you can go with them.

Hope this helps, Ingo

2 Likes

Thank you so much for the response @Niall & @Ingo_Richtsmeier.

In performance wise , will there be any difference? If my bean is singleton, then the Delegate classes would perform better or something like that?

Hi @Silambarasan_S,

No. It could be only a few processor cycles depending how the framework calls the method.

Hope this helps, Ingo

1 Like

thanks @Ingo_Richtsmeier. This really helps.

The main difference between “Java Class” and “Delegate Expression” is the instantiation of the delegates IMO. WIth Java Class, the engine will instantiate the delagates, hence you’ll have almost no possibilities to set up the objects. If you use delegate expressions, you can set up, wire etc. your delegates as you like (using e.g. Spring and its wiring capabilities) and then use them.

I’d never use “Java Class” since it has no advantages over “Delegate Expression” IMO.

4 Likes

In the case of “Java Class” why does the engine instantiate the delegate?
If you declare the Java Class as a @Component (a spring bean essentially), shouldn’t Spring Boot handle the Bean instantiation, and then the process engine just invokes it.

Hi @nmanitaras,

if you implement a service task with a java class, the process engines uses Java reflection to call the execute() method of this class and didn’t look for any other annotations.

Hope this helps, Ingo

2 Likes