有时由于业务需求需要,流程处理到某个节点时,需要有一些业务逻辑的处理,此时流程中需要添加业务逻辑功能。比如业务:
<process id="test1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent"/>
<serviceTask activiti:class="cn.maitian.form.activiti.delegate.MyServiceTask" activiti:exclusive="true" id="_3" name="ServiceTask">
<extensionElements>
<activiti:field name="field1">
<activiti:string>activiti:string>
activiti:field>
<activiti:field name="field2">
<activiti:expression>${age}activiti:expression>
activiti:field>
extensionElements>
serviceTask>
<userTask activiti:assignee="zyk" activiti:exclusive="true" id="_4" name="审批">
userTask>
<endEvent id="_5" name="EndEvent"/>
<sequenceFlow id="_9" sourceRef="_2" targetRef="_4"/>
<sequenceFlow id="_10" sourceRef="_4" targetRef="_3"/>
<sequenceFlow id="_11" sourceRef="_3" targetRef="_5"/>
process>
在这个类中写一些业务处理逻辑等操作
activiti:class=“cn.maitian.form.activiti.delegate.MyServiceTask”
@Slf4j
@Data
public class MyServiceTask implements JavaDelegate {
//定义一些属性,流程中可以指定这些属性信息值
private Expression field1;
private Expression field2;
@Override
public void execute(DelegateExecution execution) {
//从流程中获取值。值可以是流程中定义个字符串,也可以获取变量值
Object value1=field1.getValue(execution);
//通过expression 表达式从流程变量中获取值
Object value2 = field2.getValue(execution);
log.info("流程 {}-{}-{} 执行到ServiceTask,处理业务数据.",
execution.getProcessInstanceBusinessKey()
,execution.getProcessInstanceId(),execution.getCurrentActivityId());
System.out.println("value1 = " + value1);
System.out.println("value2 = " + value2);
}
}
@Test
public void complete(){
String instanceId="5e551be9-e119-11eb-ad8c-34f39ab07ea4";
//部署流程
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/test1.bpmn")
.name("请假流程")
.deploy();
log.info("部署流程成功:Id:{},Key:{},Name:{}",deployment.getId(),deployment.getKey(),deployment.getName());
//开启流程
ProcessInstance instance = runtimeService.startProcessInstanceByKey("test1");
log.info("流程开启:{}",instance.getId());
instanceId=instance.getId();
//审批流程
Map<String,Object> variables=new HashMap<>();
variables.put("assignee","jl1)");
variables.put("age",23);
Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
taskService.complete(task.getId(),variables);
log.info("流程{} - {} 任务已完成。",task.getProcessInstanceId(),task.getId());
}
//打印输出
value1 = jake
value2 = 23
使用Class配置时,这个类实现接口JavaDelegate ,但是这个类的创建是由Activiti完成的,Spring IOC不能注入Bean,不受Spring管理。如何让Spring管理,并且IOC可注入其他业务Bean呢?
activiti:class="cn.maitian.form.activiti.delegate.MyServiceTask"
注意:
<serviceTask activiti:exclusive="true" activiti:expression="#{myServiceBean.executionMethod(execution,assignee)}" id="_3" name="ServiceTask">
serviceTask>
@Component("myServiceBean")
public class MyServiceBean {
//这个参数名称,不能随便起:
// 第一个参数默认是:execution DelegateExecution 类型,获取当前流程数据
// 参数可以多个,但是参数名称assignee 要不是变量名称,要不是Spring IOC容器管理的bean ,这里可以是beanName
public void executionMethod(DelegateExecution execution,String assignee){
//从execution 中获取变量信息
Object age = execution.getVariable("age");
Object assignee1 = execution.getVariable("assignee");
System.out.println("assignee1 = " + assignee1);
System.out.println("age = " + age);
//执行业务逻辑等操作。。。
//执行业务逻辑后,可设置变量,传递到流程中,流程判断变量值,从而判断分支
execution.setVariable("nextAssignee","向流程中设置新变量信息");
}
}
myServiceBean 是SpringBean 名称
<serviceTask activiti:exclusive="true" activiti:expression="#{myServiceBean.getSetVariable)}" id="_3" name="ServiceTask">
serviceTask>
@Component("myServiceBean")
public class MyServiceBean {
public void getSetVariable(){
//无参数,只调用业务逻辑
System.out.println(" 无参数,只调用业务逻辑" );
}
}
获取bean名为myServiceBean的myField字段的值,实质是调用getMyField()方法。
<serviceTask activiti:exclusive="true" activiti:expression="#{myServiceBean.myField)}" id="_3" name="ServiceTask">
serviceTask>
@Component("myServiceBean")
@Data
public class MyServiceBean {
private boolean myField;
}
delegateExpression 是实现接口ActivityBehavior 的Spring 管理的Bean。注意此时是UML表达式 ${}
<serviceTask activiti:exclusive="true" activiti:delegateExpression="${myActivityBehavior}" id="_3" name="ServiceTask">
serviceTask>
@Component("myActivityBehavior")
public class MyActivityBehavior implements ActivityBehavior {
@Override
public void execute(DelegateExecution execution) {
System.out.println("execution = " + execution);
}
}