Are there some benefits to use expression delegate over java class in camunda
The class attribute for the Service Task is the FQN of the class that implements JavaDelegate . When this is used Flowable will instantiate the class. A delegate expression is an expression that resolves a bean that implements JavaDelegate .
Delegation means hand over the responsibility for a particular task to another class or method. It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.
A Java Delegate is a simple Java class that implements the Camunda JavaDelegate interface.
DelegateExecution. getSuperExecution() In case this delegate execution is the process instance execution and this process instance was started by a call activity, this method returns the execution which executed the call activity in the super process instance.
Only thing I can imagine using expression delegates over Java classes is that, whenever my custom code class required to load from spring beans using dependency injection, specially where you want to control the lifecycle of delegation instance by your application, not Camunda runtime engine.
In simple words, by using expression delegates, we can initialize the delegate class as we want using spring DI without letting Camunda engine to initialize it. And take all advantages of DI.
The scenario is that we want to load our delegation class from spring beans, where it requires runtime dependencies to be injected before our delegation code runs. So, we can use spring beans to initialize other object dependencies and create a bean referencing the delegation class injecting other initialized beans. For example,
<serviceTask id="paymentTask" camunda:delegateExpression="${myPaymentBean}" />
In above, myPaymentBean
is resolved from spring and it will be an instance of JavaDelegate
which has already initialized with required dependencies through dependency injection (DI). We used DI, because our business logics needs to be tested thoroughly before going into production and easier to write automated tests on top of them.
If you use Java classes, then the instance initialization part will be done by the camunda engine, and there is a restriction that there must be a public default constructor. This is very limited since most of practical applications have dependencies and must be already initialized before the code. Either you have to make your dependencies as singletons (which is I never recommend) or use factories to create instances before code (which is inefficient and harder to test).
Hope this clarifies your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With