工作流activiti 6 学习笔记十 服务任务
【摘要】 服务任务调用java程序有三类方法:activiti:class , activiti:delegateExpression,activiti:expression
服务任务调用java程序有三类方法:activiti:class , activiti:delegateExpression,activiti:expression
第一种实现:activiti:class
需要指定的类需要实现 JavaDelegate 或者ActivityBehavior,这个属性已经使用过很多次了,这种方法的一个弊端就是类固化到了代码里面,不够灵活。
bpmn 文件
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="User Task"></userTask>
<serviceTask id="servicetask1" name="Service Task" activiti:class="org.scf.act.api.task.serviceTask.ActivitiClassDelegate"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
Delegate 类
package org.scf.act.api.task.serviceTask;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class ActivitiClassDelegate implements JavaDelegate{
@Override
public void execute(DelegateExecution execution) {
System.out.println("这是一个service task应用的java类"+execution.getId());
}
}
测试类:
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("serviceTask_class.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId());
System.out.println(pi.getId());
运行结果:
这是一个service task应用的java类275006
275005
第二种实现:activiti:delegateExpression
此方法相对动态,能在启动流程时根据业务需要设置不同的业务处理类
bpmn文件
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="User Task"></userTask>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${myDelegate}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
delegate类
package org.scf.act.api.task.serviceTask;
import java.io.Serializable;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class MyDelegate implements JavaDelegate,Serializable {
@Override
public void execute(DelegateExecution execution) {
System.out.println("这是一个service task应用的delegate expression java类"+execution.getId());
}
}
测试类
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("serviceTask_delegateExpression.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
Map<String ,Object> map = new HashMap<String,Object>();
map.put("myDelegate", new MyDelegate());
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId(),map);
System.out.println(pi.getId());
运行结果;
这是一个service task应用的delegate expression java类277509
277505
第三类:activiti:expression
这个配置就非常强大了,可以直接引入java bean,赋值,取值,业务处理都可以实现。不需要实现delegate接口。
bpmn文件
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="User Task"></userTask>
<serviceTask id="servicetask1" name="Service Task" activiti:expression="${myBean.print(execution)}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
<serviceTask id="servicetask2" name="Service Task" activiti:expression="${execution.setVariable('myName',myBean.myName)}"></serviceTask>
<sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
<sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="usertask1"></sequenceFlow>
</process>
java bean
package org.scf.act.api.task.serviceTask;
import java.io.Serializable;
import org.activiti.engine.runtime.Execution;
public class MyBean implements Serializable {
private String myName="scf";
public String getMyName() {
return myName;
}
public void print(Execution exe) {
System.out.println("调用print方法,流程id为"+exe.getId());
}
}
测试代码:
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("serviceTask_expression.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
Map<String ,Object> map = new HashMap<String,Object>();
map.put("myBean", new MyBean());
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId(),map);
System.out.println(pi.getId());
// 打印设置的变量
System.out.println(runS.getVariable(pi.getId(), "myName"));
运行结果:
调用print方法,流程id为282509
282505
scf
代码解释:
第一个个taskservice 设置的时打印方法,打印执行流的id
第二个taskservice 设置全局变量给myName,设置的值是 javabean 里面的myName 变量。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)