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.

4 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

3 Likes

Sir,i did how you explained but in the tasklist iam getting this error" An error happened while submitting the task form : Cannot submit task form 62a901f8-e352-11ed-b8f0-ee63d78323cc: Unknown property used in expression: #{TestingBear}. Cause: Cannot resolve identifier ‘TestingBear’"…

Can you upload the model and show your java code

package com.truviq.camunda;

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

import javax.inject.Named;

@Named(“TestingBear”)
public class Example implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
System.out.println(“hi”);
// Business Logic
}
}

this is my javacode

external.bpmn (3.1 KB)

“I have configure delegate expression like that ,you can see in the picture.”…I’m missing some point pls let me know!

How are you deploying the process?

Hi @KOTHARI_SRIDEVI ,

To trigger your delegate class with Camunda Delegate Expression, make sure you have the following:

  1. You delegate class annotated with @Component or @Service so that Spring boot creates a bean for the class.
  2. From camunda modeler use the camelCase Notation for example, if your class is TestingBear, in delegateExpression field you should provide it as ${testingBear}

Bonus:

@Component
class TestingBear
{

public void method(DelegateExecution execution )
{

}
}

In the delgeate expression of your service task you can give it as:

${testingBear.method}

For this kind of expression:

${testingBear.method}

I have got this error:
org.camunda.bpm.engine.impl.javax.el.PropertyNotFoundException: Could not find property method in class com.example.TestingBear

I have found the definiton of Delagate Expression:
The attribute camunda:delegateExpression is used for expressions which evaluate to a delegate object.
[Expression Language | docs.camunda.org]

I think it means, at the end, it should be a JavaDelegate object referene, and always the execute() called.

@Niall Is there a new Delagate Expression syntax definition somewhere else?