cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup a Service Task class via El Expression

asdfg195
Champ in-the-making
Champ in-the-making
I am using Activiti 5.18.0.  My process has a slightly complicated error handling that needs to go around every Service Task. Instead of replicating that everywhere, I want to create a subprocess that does the error handling around a simple Service Task. I am passing in the fully qualified Class name using the input parameters of a Call Activity to create a process variable 'taskClass'. I have verified that the variable is being set correctly with the fully qualified class name.

I created a test Class for the purpose of this experiment. The code is as below.

//package statement

import java.io.Serializable;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogTask implements Serializable,JavaDelegate {
    private static final long serialVersionUID = -6489042542454599211L;
    private static final Logger LOG = LoggerFactory.getLogger(LogTask.class);

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        LOG.info("Hello World");
    }
}

My original attempt was to see if I could use the EL expression for the class name

<serviceTask id="servicetask1" name="Service Task" activiti:class="#{taskClass}"></serviceTask>

That yields the following error

java.lang.ClassNotFoundException: #{taskClass}

So it looks like the class option will not resolve from the process variable. My next thought was to try using a delegate expression. I updated my process to the following

<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="#{taskClass}"></serviceTask>
I got the following error while trying to run the service task

Caused by: org.activiti.engine.ActivitiIllegalArgumentException: Delegate expression #{taskClass} did neither resolve to an implementation of interface org.activiti.engine.impl.pvm.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate

My class implements JavaDelegate so I'm guessing that the delegate expression does not resolve from process variable. Does anyone know a way in which I can make this work because it would vastly simplify my process? I am using the programmatic configuration since my lead does not like Spring and therefore, I cannot rely on any Spring mechanisms.
2 REPLIES 2

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

I would say that class name is not expression

public class ClassDelegate extends AbstractBpmnActivityBehavior implements TaskListener, ExecutionListener, SubProcessActivityBehavior {
 
  protected String className;

Change it to the expression and make pull request.

Regards
Martin

asdfg195
Champ in-the-making
Champ in-the-making
I decided to go a different route and go with Reflections to solve my issue. Here is my code in case someone wants to try this in the future


public class ServiceTaskDelegate implements Serializable,ActivityBehavior {

  private static final long serialVersionUID = -2093463145176121341L;

  @Override
  public void execute(ActivityExecution execution) throws Exception {
    String className = execution.getVariable("taskClass", String.class);
   
    Class<?> clazz = Class.forName(className);
    Constructor<ActivityBehavior> constructor = (Constructor<ActivityBehavior>) clazz.getConstructor();
    ActivityBehavior delegate = constructor.newInstance();
    delegate.execute(execution);
  }
}