/**
* 个人任务分配
*
* 方式一、 直接写死的这里就不说了
*
* 方式二、使用流程变量 ${XXX}或#{XXX},列如:#{userId}
* 设置流程变量,使用流程变量来指定任务的办理人,对应bpmn文件中#{userId},变量值要是String的
*
* 方式三、使用类,动态指定任务的办理人
*
*
* @author lenovo
*
*/
方式二、
<?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="personalTask" name="personalTask" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="审批" activiti:assignee="#{userId}"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_personalTask">
<bpmndi:BPMNPlane bpmnElement="personalTask" id="BPMNPlane_personalTask">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="350.0" y="20.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="315.0" y="130.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="350.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="367.0" y="55.0"></omgdi:waypoint>
<omgdi:waypoint x="367.0" y="130.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="367.0" y="185.0"></omgdi:waypoint>
<omgdi:waypoint x="367.0" y="260.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 方式二
*/
// 部署流程定义(classpath)
@Test
public void deploymentProcessDefinition_inputStream() {
InputStream bpmn = this.getClass().getResourceAsStream("personalTask.bpmn");
InputStream png = this.getClass().getResourceAsStream("personalTask.png");
Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的sevices
.createDeployment()// 创建一个部署对象
.name("个人任务分配")// 添加部署名称
.addInputStream("personalTask.bpmn", bpmn).addInputStream("personalTask.png", png).deploy();
System.out.println("部署ID:" + deployment.getId());
System.out.println("部署名称:" + deployment.getName());
}
// 启动流程实例
@Test
public void deploymentProcessInstance() {
String key = "personalTask";
/**启动流程实例同时,设置流程变量,使用流程变量来指定任务的办理人,对应bpmn文件中#{userId}*/
HashMap<String, Object> variables=new HashMap<String, Object>();
variables.put("userId", "周芷若");
ProcessInstance pi = processEngine.getRuntimeService()// 与正在执行的流程是咧和执行对象相关的services
.startProcessInstanceByKey(key,variables);// 使用流程定义的key启动流程实例key对呀的helloworld.bnpm文件里的id的属性值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}
// 查询当前人的个人任务
@Test
public void findMyPersonalTask() {
String assingnee = "周芷若";
List<Task> list = processEngine.getTaskService()// 与正在执行的任务管理相关的services
.createTaskQuery()// 创建任务查询对象
/** 查询条件(where部分) */
.taskAssignee(assingnee)// 指定个人任务查询,指定
// .taskCandidateUser(candidateUser)//组任务的班里人查询
// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
// .processInstanceId(processInstanceId)//使用流程实例额ID查询
// .executionId(executionId)//使用执行对象ID查询
/** 排序 */
.orderByTaskCreateTime().asc()// 创建时间升序排序
/** 返回结果集 */
// .singleResult()//返回唯一结果集
// .count()//返回结果集的数量
// .listPage(firstResult, maxResults)//分页查询
.list();
if (list != null && list.size() > 0) {
for (Task task : list) {
System.out.println("任务ID:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务的创建时间:" + task.getCreateTime());
System.out.println("任务的办理人:" + task.getAssignee());
System.out.println("流程实例ID:" + task.getProcessInstanceId());
System.out.println("执行对象ID:" + task.getExecutionId());
System.out.println("流程定义ID:" + task.getProcessDefinitionId());
}
}
}
// 完成我的任务
@Test
public void completeMyPersonalTask() {
// 任务ID
String taskId = "160005";
processEngine.getTaskService()// 与正在执行的任务管理相关的services
.complete(taskId);
System.out.println("任务完成,任务ID:" + taskId);
}
方式三、类的方式
<?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="classTask" name="classTask" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="User Task">
<extensionElements>
<activiti:taskListener event="create" class="com.ljw.f_personalTask.TaskListenerImpl"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_classTask">
<bpmndi:BPMNPlane bpmnElement="classTask" id="BPMNPlane_classTask">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="300.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="380.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="530.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="335.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="380.0" y="177.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="485.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="530.0" y="177.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
创建类:public class TaskListenerImpl implements TaskListener {
package com.ljw.f_personalTask;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
@SuppressWarnings("serial")
public class TaskListenerImpl implements TaskListener {
/**
* 用来指定任务的办理人
*/
@Override
public void notify(DelegateTask delegateTask) {
/**指定个人任务的办理人,也可以指定组任务的办理人*/
//个人通过类去查询数据库,将下一个任务的办理人查询获取,然后通过setAssignee()的方法指定任务办理人
delegateTask.setAssignee("灭绝师太");
System.out.println("TaskListenerImpl:灭绝师太");
}
}
具体操作如下
/**
* 方式三
*/
// 部署流程定义(classpath)
@Test
public void deploymentProcessDefinition_inputStream3() {
InputStream bpmn = this.getClass().getResourceAsStream("classTask.bpmn");
InputStream png = this.getClass().getResourceAsStream("classTask.png");
Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的sevices
.createDeployment()// 创建一个部署对象
.name("个人任务分配")// 添加部署名称
.addInputStream("classTask.bpmn", bpmn).addInputStream("classTask.png", png).deploy();
System.out.println("部署ID:" + deployment.getId());
System.out.println("部署名称:" + deployment.getName());
}
// 启动流程实例
@Test
public void deploymentProcessInstance3() {
//流程定义的key
String key = "classTask";
ProcessInstance pi = processEngine.getRuntimeService()// 与正在执行的流程是咧和执行对象相关的services
.startProcessInstanceByKey(key);// 使用流程定义的key启动流程实例key对呀的helloworld.bnpm文件里的id的属性值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}
// 查询当前人的个人任务
@Test
public void findMyPersonalTask3() {
String assingnee = "张翠山";
List<Task> list = processEngine.getTaskService()// 与正在执行的任务管理相关的services
.createTaskQuery()// 创建任务查询对象
/** 查询条件(where部分) */
.taskAssignee(assingnee)// 指定个人任务查询,指定
// .taskCandidateUser(candidateUser)//组任务的班里人查询
// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
// .processInstanceId(processInstanceId)//使用流程实例额ID查询
// .executionId(executionId)//使用执行对象ID查询
/** 排序 */
.orderByTaskCreateTime().asc()// 创建时间升序排序
/** 返回结果集 */
// .singleResult()//返回唯一结果集
// .count()//返回结果集的数量
// .listPage(firstResult, maxResults)//分页查询
.list();
if (list != null && list.size() > 0) {
for (Task task : list) {
System.out.println("任务ID:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务的创建时间:" + task.getCreateTime());
System.out.println("任务的办理人:" + task.getAssignee());
System.out.println("流程实例ID:" + task.getProcessInstanceId());
System.out.println("执行对象ID:" + task.getExecutionId());
System.out.println("流程定义ID:" + task.getProcessDefinitionId());
}
}
}
// 完成我的任务
@Test
public void completeMyPersonalTask3() {
// 任务ID
String taskId = "177504";
processEngine.getTaskService()// 与正在执行的任务管理相关的services
.complete(taskId);
System.out.println("任务完成,任务ID:" + taskId);
}
//可以分配个人任务从一个人到另一个人(认领任务)
@Test
public void setAssineeTask() {
String taskId="177504";
//指定的办理人
String userId="张翠山";
processEngine.getTaskService().setAssignee(taskId, userId);
}