cancel
Showing results for 
Search instead for 
Did you mean: 

Service parameters and return value

mhw
Champ in-the-making
Champ in-the-making
Hi, I am trying to interact with a service facade using XML only. I am stuck with how to pass parameters from outside expressions.

I have the following process:
<serviceTask id="javaService" name="Read voltage"
      activiti:expression="#{ServiceDelegate.execute()}"
      activiti:resultVariable="voltage">
      <extensionElements>
         <activiti:field name="serviceName">
            <activiti:string>READ_VOLTAGE</activiti:string>
         </activiti:field>
      </extensionElements>
   </serviceTask>
   <sequenceFlow id="flow2" sourceRef="exclusiveGw" targetRef="theTask1">
      <conditionExpression xsi:type="tFormalExpression">{voltage == 12.1}</conditionExpression>
    </sequenceFlow>

Now I want to access the filed "serviceName" from the execute-Method. Unlike realized with "activiti:class" the field is not injected. But I have to use "activiti:expression" since I need the "activiti:resultVariable".

Could anybody please help?

regards, Michael
10 REPLIES 10

trademak
Star Contributor
Star Contributor
Hi,

You can simply pass the variables in the method call.
So you can use #{ServiceDelegate.execute(yourVariableName)}It's also possible to pass the whole execution instance by using #{ServiceDelegate.execute(execution)}
Best regards,

mhw
Champ in-the-making
Champ in-the-making
Hi trademak,

thanks for your answer. I understand, that I can pass an own variable. But how do I pass the execution? With the "activiti:class"-call the execution is magically (this is what I dont understand) passed. With "activiti:expression" and the same jul-snippet the execution is a null pointer.

How would you initialize the "execution"-variable here:
#{ServiceDelegate.execute(execution)}
Best regards,
Michael

trademak
Star Contributor
Star Contributor
Hi Michael,

Not sure if understand your questions, but the ServiceDelegate class (or whatever class it's wired to) execute method should look similar to:

public void execute(DelegateExecution execution) {
The #{ServiceDelegate.execute(execution)} line will pass the DelegateExection instance to this method.

Best regards,

mhw
Champ in-the-making
Champ in-the-making
Hi trademak,

thank you for your time again. You are right again, execution is passed. The null pointer comes not from execution parameter.
My delegate class:
public class ServiceDelegate implements JavaDelegate, Serializable {

public void execute(DelegateExecution execution) throws Exception {
  String serviceNameString = (String) serviceName.getValue(execution);

  if(serviceNameString.equals("READ_VOLTAGE")){
  …
The serviceName field is null. With my "activiti:class" process the serviceName is injected with:
public void setText(Expression serviceNameExpression){
  this.serviceName = serviceNameExpression;
}
With this process part passing the serviceName works :
  <serviceTask id="javaServiceReadVoltage" name="Read voltage"
   activiti:class="com.bosch.wfe.activity.delegate.ServiceDelegate">
   <extensionElements>
    <activiti:field name="serviceName">
     <activiti:string>READ_VOLTAGE</activiti:string>
    </activiti:field>
   </extensionElements>
  </serviceTask>
With this process part it is _not_ working:
<serviceTask id="javaService" name="Read voltage"
  activiti:expression="${ServiceDelegate.execute(execution)}"
  activiti:resultVariable="voltage">
  <extensionElements>
   <activiti:field name="serviceName">
    <activiti:string>READ_VOLTAGE</activiti:string>
   </activiti:field>
  </extensionElements>
</serviceTask>

Perhaps this is the wrong approach. What I want is: passing a variable to a service and getting a return value(which I can use in a gateway). But with XML only (because the editors want to be flexible). Hopefully I made my problem clear now.

Best regards,
Michael

mhw
Champ in-the-making
Champ in-the-making
The serviceName field is null. With my "activiti:class" process the serviceName is injected with:
public void setText(Expression serviceNameExpression){
  this.serviceName = serviceNameExpression;
}
This was not true. Service parameter is injected without this method. I must have been confused.

Now I have this workflow:


<serviceTask id="javaServiceReadVoltage" name="Read voltage"
   activiti:class="com.bigcompany.wfe.activiti.delegate.ServiceDelegate">
  <extensionElements>
   <activiti:field name="serviceName">
    <activiti:string>READ_VOLTAGE</activiti:string>
   </activiti:field>
  </extensionElements>
 
</serviceTask>

    <sequenceFlow id="flow3" sourceRef="javaServiceReadVoltage" targetRef="exclusiveGw" />
   
    <exclusiveGateway id="exclusiveGw" name="Exclusive Gateway" />
   
    <sequenceFlow id="flow4" sourceRef="exclusiveGw" targetRef="theTask1">
      <conditionExpression xsi:type="tFormalExpression">${ServiceResult == 12.0}</conditionExpression>
    </sequenceFlow>
The process variable "ServiceResult" is set within the delegate service:
   double result = ServiceFacade.readVoltage();
   execution.setVariable("ServiceResult", result);
But this solution is based on the "knowledge" that the variable name is "ServiceResult". If the editor has no possibility to look in the source this has to be documentated elsewhere.
Therefore this is no solution for us.
Please, somebody: What can we do to name the result variable as an editor?

Best regards,

Michael

jbarrez
Star Contributor
Star Contributor
This is inheritly so for the Activiti engine, as it is a Java worflow engine in the first place.
There is currently no way of providing process variables in the process.

mhw
Champ in-the-making
Champ in-the-making
This is inheritly so for the Activiti engine, as it is a Java worflow engine in the first place.
There is currently no way of providing process variables in the process.
Thank you for the clarification. I was confused because "activiti:resultVariable" let one introduce such a process variable in the process. I was wondering why this seems forbidden with an "activiti:class"-Element.

erny18031
Champ in-the-making
Champ in-the-making
This is inheritly so for the Activiti engine, as it is a Java worflow engine in the first place.
There is currently no way of providing process variables in the process.
Thank you for the clarification. I was confused because "activiti:resultVariable" let one introduce such a process variable in the process. I was wondering why this seems forbidden with an "activiti:class"-Element.

I was just wondering about the same thing. Input to the service task is easy using "Expressions" in code and "activiti:field" in bpm20.xml. The reverse should also be possible, e.g.

  <serviceTask id="serviceTask1" name="Service Taks" activiti:class="org.example.activi.servicetasks.MyService">
    <extensionElements>
      <activiti:field name="param1"><activiti:expression>${whatever}</activiti:expression></activiti:field>
      <activiti:field name="param2" activiti:resultVariable="processVariable1" />
  </serviceTask>

Regards.
Erny

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Why should this be possible? What do you mis?