本篇主要是Activiti工作流框架的学习记录,以及对于该框架的基本使用和一些浅显的理解:
1)基本概念与名词理解
- 个人任务:由某一个人专项负责办理的任务为个人任务(他人无权限干预或操作);
- 组任务:多个人作为候选人,都可以办理的任务(如果在涉及到的真实项目中一般情况下会与角色管理中的角色组进行同步,方便操作);
2)知识点1:候选人操作(现在的操作因为处于学习阶段都是暂时静态数据)
- 工作流程图,手动静态添加候选人
- 重要代码片段:
- 初期准备代码片段:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//部署流程
@Test
public void test1() {
DeploymentBuilder deploymentBuilder =
processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("com/yq/activiti/groupTask/bxlc.bpmn");
deploymentBuilder.addClasspathResource("com/yq/activiti/groupTask/bxlc.png");
deploymentBuilder.deploy();
}
//启动流程实例
@Test
public void test2() {
//清理数据
String processDefintionKey = "bxlc";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefintionKey);
System.out.println(processInstance.getId());
}
- 首先根据上面所绘制的流程图,当执行到节点2的时候才会出现候选人,所以现在首先,走过第一个节点任务:(一般步骤:先查询任务,再执行任务)
//查询个人任务
@Test
public void test3() {
TaskQuery query = processEngine.getTaskService().createTaskQuery();
List<Task> list = query.list();
query.taskAssignee("张三");
for (Task task : list) {
System.out.println(task.getId()+"-->"+task.getName());
}
}
//办理个人任务
@Test
public void test4() {
//当前办理的任务还是个人任务,办理完成后走到了候选组任务
String taskId = "304";
processEngine.getTaskService().complete(taskId);
}
- 接着根据上面所绘制的流程图,当前已经执行到节点任务2,所以现在开始办理当前任务:(同样的先进行查询,但是当前在还没进行任务拾取(PS:也就是通俗理解为分配的意思)的情况下,不能够进行办理,只能够查询到当前任务的进程,以及现在user1可以进行办理的流程项;)
//查询组任务
@Test
public void test5() {
TaskQuery query = processEngine.getTaskService().createTaskQuery();
//组任务查询
query.taskCandidateUser("user1");
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getId() + "-->" + task.getName());
}
}
//拾取组任务
@Test
public void test6() {
String taskId = "2802";
String userId = "user1";
processEngine.getTaskService().claim(taskId, userId);
}
- 当然,在拾取了任务之后可能会有很多原因造成该任务该员工无法完成,所以需要退回任务:
//退回组任务
@Test
public void test7() {
String taskId = "2802";
processEngine.getTaskService().setAssignee(taskId, null);
}
3)候选组操作(重点,项目如果选择该框架,那必然会使用此知识点):
- 画流程图:
代码操作:
同样显示基本部署,代码完全相同:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//部署流程
@Test
public void test1() {
DeploymentBuilder deploymentBuilder =
processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("com/yq/activiti/groupTask/bxlc2.bpmn");
deploymentBuilder.addClasspathResource("com/yq/activiti/groupTask/bxlc2.png");
deploymentBuilder.deploy();
}
- 创建组,操作数据库中的表:ac_id_group
@Test
public void test2() {
//实际项目中group表中的数据和系统表角色保持一致
Group group = new GroupEntity();
group.setId("财务人员组");
group.setName("财务人员组");
processEngine.getIdentityService().saveGroup(group);
}
- 创建用户,操作的表ac_id_user
//保证与实际项目系统中的t_user以及ac_id_user表保证同步操作
@Test
public void test3() {
User user = new UserEntity();
//仅仅只是创建出了用户与实际的组关系还没有体现
user.setId("002");
user.setFirstName("小王");
processEngine.getIdentityService().saveUser(user);
}
- 将用户加入到组中
@Test
public void test4() {
String userId = "002";
String groupId = "财务人员组";
processEngine.getIdentityService().createMembership(userId, groupId);
}
- 第一个任务流程节点的办理:
//启动流程实例
@Test
public void test5() {
String processDefinitionKey = "bxlc";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
}
@Test
public void test6() {
TaskQuery query = processEngine.getTaskService().createTaskQuery();
List<Task> list = query.list();
//个人任务查询
query.taskAssignee("张三");
for (Task task : list) {
System.out.println(task.getId()+"-->"+task.getName());
}
}
//办理个人任务
@Test
public void test7() {
String taskId = "3804";
processEngine.getTaskService().complete(taskId);
}
- 查询组任务
@Test
public void test8() {
TaskQuery query = processEngine.getTaskService().createTaskQuery();
//组任务查询
// query.taskCandidateGroup("财务人员组");
query.taskCandidateUser("001");
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getId() +"-->"+ task.getName());
}
}
- 拾取组任务
@Test
public void test9() {
String taskId = "3902";
processEngine.getTaskService().claim(taskId, "002");
}
- 办理任务
@Test
public void test10() {
String taskId = "4102";
processEngine.getTaskService().complete(taskId);
}
- 执行监听器
监听的是execution流程(实例)执行过程中开始事件和结束时间
使用方法:
- 按照框架的要求编写一个监听器类,实现接口ExecutionListener
- 注册监听器
//自定义一个监听器类
public class MyExecutionListener1 implements ExecutionListener{
private static final long serialVersionUID = 1L;
@Override
public void notify(DelegateExecution execution) throws Exception {
String processInstanceId = execution.getProcessInstanceId();
String eventName = execution.getEventName();
// Set<String> variableNames = execution.getVariableNames();
Map<String,Object> variables = execution.getVariables();
String processDefinitionId = execution.getProcessDefinitionId();
System.out.println(processInstanceId);
System.out.println(processDefinitionId);
System.out.println("自定义的监听器,监听到事件:"+eventName);
Set<String> keySet = variables.keySet();
for (String string : keySet) {
System.out.println(variables.get(string));
}
}
}
- 任务监听器
public class MyTaskListener1 implements TaskListener {
private static final long serialVersionUID = 1L;
@Override
public void notify(DelegateTask delegateTask) {
String id = delegateTask.getId();
String name = delegateTask.getName();
String assignee = delegateTask.getAssignee();
System.out.println("任务【"+ id + "】,【" + name + "】被创建" +
"负责人【" + assignee + "】");
}
}
public class ExclusiveGetwayTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//部署流程定义
@Test
public void test1() {
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("com/yq/activiti/getway/exclusiveGetway/bxlc.bpmn");
deploymentBuilder.deploy();
}
//启动流程实例
@Test
public void test2() {
String processDefinitionKey = "bxlc";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
}
//办理任务,同时设置流程变量
@Test
public void test3() {
String taskId = "5904";
Map<String , Object> variables = new HashMap<String, Object>();
variables.put("bxje", 6500);
processEngine.getTaskService().complete(taskId,variables);
System.out.println();
}
}
2、并行网关
public class ParllaeGetwayTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//部署流程
@Test
public void test1() {
DeploymentBuilder deploymentBuilder =
processEngine.getRepositoryService().createDeployment();
deploymentBuilder
.addClasspathResource("com/yq/activiti/getway/paralleGetway/gwlc.bpmn");
deploymentBuilder.deploy();
}
//产生两条任务
@Test
public void test2() {
String processEngineKey = "gwlc";
ProcessInstance instance = processEngine.getRuntimeService().startProcessInstanceByKey(processEngineKey);
System.out.println(instance.getId());
}
//办理任务
@Test
public void test3() {
String taskId = "6502";
processEngine.getTaskService().complete(taskId);
}
}