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

vire7777
Champ in-the-making
Champ in-the-making
I m sorry guys but nothing explains how to solve my problem in the user guide as there is just a really little sentence for "activiti:delegateExpression"

In addition, as i told, when i use :
activiti:delegateExpression="${myVar}"

It doesn't enter my method
@Override
   public void execute(DelegateExecution arg0) throws Exception {
      int a = 1;
   }

But when i use :
activiti:delegateExpression="${myVar.execute()}"

It enters my other method :
@Override
   public void execute() throws Exception {
      int a = 2;
   }

In all cases i have the error message :
GRAVE: Error while closing command context
org.activiti.engine.ActivitiException: Delegate expression ${myVar.execute()} (or ${myVar} for the first example) did not resolve to an implementation of interface org.activiti.engine.impl.pvm.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate


Then, i ll try with "activiti:expression" but really i would prefer doing it with the delegateExpression…

frederikherema1
Star Contributor
Star Contributor
A delegateExpression can be used when the expression resolves to an instance of a JavaDelegate-implementing class. Nothing more, nothing less. If it doesn't work, it means there is something wrong with your class. What "JavaDelegate" are you implementing?

Can you post you java-class and the bpmn20.xml?

vire7777
Champ in-the-making
Champ in-the-making
So here's my code :

Here's the Bpmn20 :
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:smileysurprised:mgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:smileysurprised:mgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="Examples">
  <process id="MyTest" name="MyTest">
    <startEvent id="theStart" name="Start"></startEvent>
    <serviceTask id="javaService" name="Java service invocation" activiti:class="org.activiti.examples.bpmn.test.manager2.SetterTest">
      <extensionElements>
        <activiti:field name="text">
          <activiti:string>Success</activiti:string>
        </activiti:field>
      </extensionElements>
    </serviceTask>
    <serviceTask id="javaService2" name="Java service invocation" activiti:delegateExpression="${testVariable}"></serviceTask>
    <userTask id="usertask1" name="User Task" activiti:candidateGroups="user"></userTask>
    <endEvent id="theEnd" name="End"></endEvent>
    <sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="javaService"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="javaService" targetRef="javaService2"></sequenceFlow>
    <sequenceFlow id="flow4" name="" sourceRef="usertask1" targetRef="theEnd"></sequenceFlow>
    <sequenceFlow id="flow5" name="" sourceRef="javaService2" targetRef="usertask1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_MyTest">
    <bpmndi:BPMNPlane bpmnElement="MyTest" id="BPMNPlane_MyTest">
      <bpmndi:BPMNShape bpmnElement="theStart" id="BPMNShape_theStart">
        <omgdc:Bounds height="35" width="35" x="30" y="200"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="javaService" id="BPMNShape_javaService">
        <omgdc:Bounds height="55" width="105" x="105" y="190"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="javaService2" id="BPMNShape_javaService2">
        <omgdc:Bounds height="55" width="105" x="250" y="190"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55" width="105" x="395" y="190"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="theEnd" id="BPMNShape_theEnd">
        <omgdc:Bounds height="35" width="35" x="540" y="200"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="65" y="217"></omgdi:waypoint>
        <omgdi:waypoint x="105" y="217"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="210" y="217"></omgdi:waypoint>
        <omgdi:waypoint x="250" y="217"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="500" y="217"></omgdi:waypoint>
        <omgdi:waypoint x="540" y="217"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="355" y="217"></omgdi:waypoint>
        <omgdi:waypoint x="395" y="217"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

here's the first class :

package org.activiti.examples.bpmn.test.manager2;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;

public class SetterTest implements JavaDelegate {

private static final String VARIABLE_NAME = "testVariable";
private Expression text;

public void execute(DelegateExecution execution) {
  String var = (String) text.getValue(execution);

  if ("Success".equals(var)) {
   ManagerTest manager = new ManagerTest();
   execution.setVariable(VARIABLE_NAME, manager);
  }
}

}

Here's the second class :

package org.activiti.examples.bpmn.test.manager2;

import java.io.Serializable;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class ManagerTest implements Serializable, JavaDelegate {

private static final long serialVersionUID = 1L;

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

All i want is : going to my second class and set "a" to 1 Smiley Wink

vire7777
Champ in-the-making
Champ in-the-making
Ok ! you can clause this thread !
I don't know why but now it works just fine although i haven't done a thing…
Something strange happens sometimes in computers that i really can't understand

Anyway, i don't have the problem now.
So thanks a lot for all Smiley Happy

frederikherema1
Star Contributor
Star Contributor
Glad it worked out for you…

vire7777
Champ in-the-making
Champ in-the-making
just a last question (promise, this one would be quick) :

Every time i launch my activiti-explorer project with tomcat, it tries to re-create the database tables.
Then i have to delete them each time else i have many errors like "the table xxxx already exists"
How to stop it ?

frederikherema1
Star Contributor
Star Contributor
Please read the user guide, sections http://activiti.org/userguide/index.html#databaseUpgrade, http://activiti.org/userguide/index.html#configurationRoot and http://activiti.org/userguide/index.html#databaseConfiguration where you can find the right properties to fiddle with to prevent this…

vire7777
Champ in-the-making
Champ in-the-making
^^
Thanks a lot for all !
I have no other question for the moment Smiley Wink