Activiti的Java服务任务(ServiceTask)属性用法

Java服务任务是自动任务,不用人为,调用外部Java类。

Type为Class的情况

开始演示

  • Type选class
  • Class填com.plancode.ThrowErrorDelegate
  • Result Variable Name不允许填,因为只有type为Expression才有效
  • Fields为参数列表,这些参数就是通过ThrowErrorDelegate类的Expression属性注入的,我这里定义了两个参数,fieldA的type为String是静态参数写死的,另一个参数fieldB的type为Expression是表达式,里面可以使用流程变量动态给ThrowErrorDelegate类的Expression属性赋值
    (1)流程图

(2)ThrowErrorDelegate类代码

package com.plancode;
import org.activiti.engine.delegate.*;
import org.springframework.stereotype.Service;
public class ThrowErrorDelegate implements JavaDelegate {
    private Expression fieldA;
    private Expression fieldB;
    public void execute(DelegateExecution execution) throws Exception {
    String result = fieldA.getValue(execution).toString() + fieldB.getValue(execution).toString();
        execution.setVariable("resultVar", result);
    }
}

(3)流程启动代码

Map variableMap = new HashMap();
variableMap.put("fieldB","valueB");
ProcessInstance pi = runtimeService.startProcessInstanceByKey("myProcess_1", variableMap);
Object resultVar = historyService.createHistoricVariableInstanceQuery().processInstanceId(pi.getId()).variableName("resultVar").singleResult().getValue();

(4)结果
在流程启动代码中的resultVar的值为:valueAvalueB

Type为Delegate Expression的情况

开始演示

  • Type选Delegate Expression
  • Delegate Expression填${throwErrorDelegate},注入spring管理的bean
  • Result Variable Name不允许填,因为只有type为Expression才有效
  • Fields为参数列表,这些参数就是通过ThrowErrorDelegate类的Expression属性注入的,我这里定义了两个参数,fieldA的type为String是静态参数写死的,另一个参数fieldB的type为Expression是表达式,里面可以使用流程变量动态给ThrowErrorDelegate类的Expression属性赋值

(2)流程图

(2)ThrowErrorDelegate类代码,多了一个@Service注解
```
package com.plancode;
import org.activiti.engine.delegate.*;
import org.springframework.stereotype.Service;
@Service
public class ThrowErrorDelegate implements JavaDelegate {
private Expression fieldA;
private Expression fieldB;
public void execute(DelegateExecution execution) throws Exception {
String result = fieldA.getValue(execution).toString() + fieldB.getValue(execution).toString();
execution.setVariable("resultVar", result);
}

top Created with Sketch.