cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a variable JavaServiceTask ?

vire7777
Champ in-the-making
Champ in-the-making
Hi people Smiley Happy

I'm creating a new workflow with our cool activiti stuff and wondering if that's possible to create a javaServiceTask with a variable activiti class to be used in multiple workflow instance with different goals. For example, the first workflow instance would initialize "myVariable" to call MyFirstClass.java in my javaServiceTask, and, at the contrary, the second instance would call "MySecondClass.java" (for example, because the creating users don't have the same rights, so the called classes are not the same or because i want to create a tool to generate workflow templates but where you can specify in a previous task what you want this template do).

In the userGuide, i've seen something so similar for an other task type :

<businessRuleTask id="businessRuleTask" activiti:class="${MyRuleServiceDelegate}" />‍

And i was trying to do something close for my javaServiceTask :

<serviceTask id="javaService2" name="Java service invocation" activiti:class="${testVariable}"></serviceTask>‍

But it generates two errors in my bpmn20 validation :

Multiple annotations found at this line:
   - cvc-attribute.3: The value '${testVariable}' of attribute 'activiti:class' on element 'serviceTask' is not valid with respect to its type, 'null'.
   - cvc-pattern-valid: Value '${testVariable}' is not facet-valid with respect to pattern '([a-z]{2,3}(\.[a-zA-Z][a-zA-Z_$0-9]*)*)\.([A-Z][a-zA-Z_$0-9]*)' for type '#AnonType_class'.

Then… does anyone has an idea how to do it ?
Thanks for your time Smiley Happy
17 REPLIES 17

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
You can try if it works when turning of validation. Might be that it is ' just' a problem in the xsd

vire7777
Champ in-the-making
Champ in-the-making
Nope ! it told me :

GRAVE: Error while closing command context
org.activiti.engine.ActivitiException: couldn't instantiate class ${testVariable}

Well maybe that's impossible

frederikherema1
Star Contributor
Star Contributor

activiti:class="${testVariable}"

activiti:class should be a string literal, containing a class-name… If you want to have a dynamic JavaDelegate, take a look at activiti:expression en activiti:delegateExpression in the user guide.

vire7777
Champ in-the-making
Champ in-the-making
I ve seen them but don't know how to use them (maybe i don't really understand)… It seems they don't call for java classes…

Could you just give me an example of a very simple workflow with two javaServiceTask ?
- The first javaServiceTask would call a class with activiti:class where you create a process variable "toto" which contains "org.activiti.MyClass". Like :

public class SetterTest implements JavaDelegate {

private static final String VARIABLE_NAME = "toto";

public void execute(DelegateExecution execution) {
  String var = "org.activiti.MyClass";
  execution.setVariable(VARIABLE_NAME, var);
}
}
- And the second javaServiceTask would call the toto variable class.

If you can do it, you would resolve my problem because i'm really out of ideas right now Smiley Happy

frederikherema1
Star Contributor
Star Contributor
What are you trying to do? Add a POJO as a variable to your process, and call it later on?

In that case, add a POJO instance as a process-variable (make sure it implements Serializable) instead of the classname. Afterwards, use activiti:delegateExpression="${myVariable.doStuff()}" (supposing the POJO has a doStuff method).

Please note, using a serializable as variable works, but not sure what your use case would be…

vire7777
Champ in-the-making
Champ in-the-making
Woooowww, so cool :=)
It works as it enters my code but after executing it, i have an error !

GRAVE: Error while closing command context
org.activiti.engine.ActivitiException: Delegate expression ${myVariable.execute()} did not resolve to an implementation of interface org.activiti.engine.impl.pvm.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate

That s strange because my class MyClass.java which is stored in myVariable implements serializable (as you told me) and JavaDelegate…
Where is the problem so ?

One other question : If i want to pass process or task parameters to my method doStuff(), can i do it by creating them in the task or process and just call : activiti:delegateExpression="${myVariable.doStuff(param1, param2, …)}" or do i have to do something different ?

frederikherema1
Star Contributor
Star Contributor
don't do the ${myVariable.execute()}, just point to the delegate: ${myVariable} and it will work…

vire7777
Champ in-the-making
Champ in-the-making
Already done and i have the same error message. In addition, it doesn't enter the method "execute" however when i wrote : ${myVariable.test()}, it enters the method "test".

public class ManagerTest implements Serializable, JavaDelegate {

/**
  *
  */
private static final long serialVersionUID = 1L;

@Override
public void execute(DelegateExecution arg0) throws Exception {
  int a = 1;
}

        public void test() throws Exception {
  int a = 1;
}
}

I m really sorry for all that work i m giving to you, i really feel as a newb :'(

frederikherema1
Star Contributor
Star Contributor
Please consult the user guide regarding JavaDelegates. If you're pointing to a JavaDelegate instance, use activiti:delegateExpression="${myVar}". When using alternative method, use activiti:expression="${myVar.test()}"