首先给大家看一下我的流程图:
流程文件leaveBill.bpmn
<?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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="LeaveBill" name="LeaveBillProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="提交申请" activiti:assignee="${inputUser}" activiti:formKey="workflowAction_audit.action"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="经纪人审批" activiti:formKey="workflowAction_audit.action">
<extensionElements>
<activiti:taskListener event="create" class="cn.itcast.ssh.utils.MangerTaskHandlerCandidateUsers"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<userTask id="usertask3" name="老总审批" activiti:assignee="王中军" activiti:formKey="workflowAction_audit.action"></userTask>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_LeaveBill">
<bpmndi:BPMNPlane bpmnElement="LeaveBill" id="BPMNPlane_LeaveBill">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="290.0" y="60.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="230.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="310.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="290.0" y="400.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="307.0" y="95.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="140.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="307.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="230.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="307.0" y="285.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="310.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="307.0" y="365.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="400.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
---------好了进入正题,我这个例子主要是两个地方表现了两种动态指定任务人的例子;
一个就是提交申请,usertask1节点的动态设置办理人
---<userTask id="usertask1" name="提交申请" activiti:assignee="${inputUser}" activiti:formKey="workflowAction_audit.action"></userTask>
-------- activiti:assignee="${inputUser}" 设置任务人,表达式使用类似于el表达式的写法,使用activiti:assignee节点,指定办理人。这里需要动态传入参数来指定
-----------我程序中,在提交申请启动运行请假实例时的代码片段
/**更新请假状态,启动流程实例,让启动的流程实例关联业务*/
@Override
public void saveStartProcess(WorkflowBean workflowBean) {
//1:获取请假单ID,使用请假单ID,查询请假单的对象LeaveBill
Long id = workflowBean.getId();
LeaveBill leaveBill = leaveBillDao.findLeaveBillById(id);
//2:更新请假单的请假状态从0变成1(初始录入-->审核中)
leaveBill.setState(1);
//3:使用当前对象获取到流程定义的key(对象的名称就是流程定义的key)
String key = leaveBill.getClass().getSimpleName();
/**
* 4:从Session中获取当前任务的办理人,使用流程变量设置下一个任务的办理人
* inputUser是流程变量的名称,
* 获取的办理人是流程变量的值
*/
Map<String, Object> variables = new HashMap<String,Object>();
variables.put("inputUser", SessionContext.get().getName());//表示惟一用户,通过流程变量inputUser,指定当前登录让人为节点办理人
/**
* 5: (1)使用流程变量设置字符串(格式:LeaveBill.id的形式),通过设置,让启动的流程(流程实例)关联业务
(2)使用正在执行对象表中的一个字段BUSINESS_KEY(Activiti提供的一个字段),让启动的流程(流程实例)关联业务
*/
//格式:LeaveBill.id的形式(使用流程变量)
String objId = key+"."+id;
variables.put("objId", objId);
//6:使用流程定义的key,启动流程实例,同时设置流程变量,同时向正在执行的执行对象表中的字段BUSINESS_KEY添加业务
//数据, 同时让流程关联业务
runtimeService.startProcessInstanceByKey(key,objId,variables);
}
二、动态指定多人处理节点任务 使用了自己实现监听的方式。我想让在经纪人审核这个节点上,不止指定一个人来处理,这个节点的处理人有可能会变。
---bpmn文件节点明细
<userTask id="usertask2" name="经纪人审批" activiti:formKey="workflowAction_audit.action">
<extensionElements>
<activiti:taskListener event="create" class="cn.itcast.ssh.utils.MangerTaskHandlerCandidateUsers"></activiti:taskListener>
</extensionElements>
</userTask>
-----下面是我的实现类
- package cn.itcast.ssh.utils;
- import java.util.Arrays;
- import org.activiti.engine.delegate.DelegateTask;
- import org.activiti.engine.delegate.TaskListener;
- import cn.itcast.ssh.domain.Employee;
- public class MangerTaskHandlerCandidateUsers implements TaskListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @Override
- public void notify(DelegateTask delegateTask) {
- System.out.println("进入MangerTaskHandlerCandidateUsers=========");
- /**从新查询当前用户,再获取当前用户对应的领导*/
- Employee employee = SessionContext.get();
- //当前用户
- String name = employee.getName();
- System.out.println("当前登录人========"+name);
- String[] empLoyees = {"冯小刚经纪人","范冰冰经纪人","冯小刚"};
- delegateTask.addCandidateUsers(Arrays.asList(empLoyees));//完成多处理人的指定
- System.out.println("节点任务人========冯小刚经纪人,范冰冰经纪人,冯小刚");
- }
}
------------以上就是动态设置多用户任务人的例子,这里我是手写的demo,实际项目中我们可以在notify(DelegateTask delegateTask)中,通过查询固定数据表或者缓存来,实现灵活的任务人设置。这是我的个人拙见啊。
再啰嗦一下,如果是设置用户组的话,这里应该是delegateTask.addCandidateGroup("某组");
设置流程变量值:delegateTask.setVariable("inputName", "小明");//inputName流程变量名