作用:项目中审批流程的定义,原理:程序员通过流程定义语言预先定义好工作流程,通过API把流程图部署到数据库,API启动流程(比如请假)、API驱动流程一步步向下执行,最终结束.
activiti数据库环境
- act_id_*:引擎内部的人员与组信息表,一般不用,项目会有自己的
- act_re_*:流程部署表,固定的数据,一般不会变
- act_ru_*:流程运行时数据表,频繁变化,但是数据量很少。因为当流程结束后,清空
- act_hi_*:流程历史信息表,会比较大,历史流程实例、历史任务、任务审批记录,审批附件表
- act_ge_*:二进制表,也是流程部署相关的信息
安装插件
- 建议在eclipse下使用
- eclipse安装:安装artifacts
- assignee:执行任务的单个人员,此处一般使用表达式
- candidate users:候选人列表,逗号分隔或使用表达式
- 排它网关exclusiveGateway:记得写条件
- IDEA安装:在Plugins中搜索或使用离线安装actibpm
核心对象
- ProcessEngine:入口对象,相当于mybatis的SqlSession会话对象
- RepositoryService:管理流程定义,用来部署流程,删除流程部署,查询流程部署
- RuntimeService:流程执行服务类,启动流程,驱动流程向下执行
- TaskService:任务服务类,获取个人待办等
- HistoryService:历史信息服务类,查询流程历史信息,跟踪流程实例运行情况
- ProcessDefinition:流程定义对象,可以获得资源文件
- ProcessInstance:流程实例对象,当流程被启动后,对应有一条流程实例数据,一个流程实例下有多个任务
使用
基础配置
添加jar包
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.22.0</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.22.0</version> </dependency>
|
配置activity.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="databaseSchemaUpdate" value="true" /> </bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration" /> </bean> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /> </beans>
|
画出bpmn图和转存一份png图
IdentityService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| @Autowired private IdentityService is;
public void add_user(){ String user1 = "zhangSan"; User user = is.newUser(user1); '可设置,可不设置' user.setFirstName("A"); user.setLastName("Z"); is.saveUser(user); }
public void add_mgr(){ User mgr1 = is.newUser("mgr1"); User mgr2 = is.newUser("mgr2"); is.saveUser(mgr1); is.saveUser(mgr2); }
public void add_fzGroup(){ User fz1 = is.newUser("fz1"); User fz2 = is.newUser("fz2"); is.saveUser(fz1); is.saveUser(fz2); '创建组' Group fz = is.newGroup("fz"); is.saveGroup(fz); }
public void add_memerShip(){ '把fz1和fz2添加到fz组中' is.createMembership("fz1","fz"); is.createMembership("fz2","fz"); }
|
RepositoryService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Autowired private ProcessEngine pe; @Autowired private RepositoryService rs;
public void test_pe(){ System.out.println(pe); }
public void test_rs(){ DeploymentBuilder deployment = rs.createDeployment(); deployment.name("请假申请"); deployment.addClasspathResource("bpmn/leaveProcess.bpmn"); deployment.addClasspathResource("bpmn/leaveProcess.png"); Deployment deploy = deployment.deploy(); }
public void test_dq(){ DeploymentQuery dq = rs.createDeploymentQuery(); List<Deployment> list = dq.list(); System.out.println(list); }
public void test_de(){ rs.deleteDeployment("1"); }
|
RuntimeService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| @Autowired private RuntimeService rs; @Autowired private TaskService ts;
@Test public void del() { String pid = "7501"; rs.deleteProcessInstance(pid,null); }
@Test public void startProcess(){ String loginUser = "zhangSan"; String processDefKey = "myProcess"; Map<String,Object> args = new HashMap<>(); args.put("loginUser",loginUser); ProcessInstance pi = rs.startProcessInstanceByKey(processDefKey, args); System.out.println(pi.getId()); }
@Test public void leaveApplyTask(){ String loginUser = "zhangSan"; String pid = "20001"; int days = 3; Task task = ts.createTaskQuery().processInstanceId(pid).taskAssignee(loginUser).singleResult(); Map<String,Object> args = new HashMap<>(); args.put("days",days); ts.complete(task.getId(),args); }
@Test public void getTask(){ String loginUser = "mgr1"; String processDefKey = "myProcess"; List<Task> list = ts.createTaskQuery().processDefinitionKey(processDefKey).taskCandidateOrAssigned(loginUser).list(); for (Task task : list) { String processInstanceId = task.getProcessInstanceId(); String id = task.getId(); String name = task.getName(); Date createTime = task.getCreateTime(); System.out.println(processInstanceId+"==="+id+"==="+name+"==="+createTime); } }
@Test public void claim(){ String loginUser = "mgr1"; String taskId = "22503"; ts.claim(taskId,loginUser); }
@Test public void mgrProve(){ String taskId = "22503"; String instanceId = "20001"; String content = "我是经理,审批通过!"; ts.addComment(taskId,instanceId,content); Map<String,Object> args = new HashMap<>(); args.put("approve",true); ts.complete(taskId,args); }
|
HistoryService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @Autowired private HistoryService hs; @Autowired private TaskService ts;
public void getHistoryTask(){ String processInstanceId = "20001"; HistoricTaskInstanceQuery hq = hs.createHistoricTaskInstanceQuery(); hq.processInstanceId(processInstanceId); hq.orderByHistoricTaskInstanceEndTime(); hq.desc(); List<HistoricTaskInstance> list = hq.list(); for (HistoricTaskInstance historicTaskInstance : list) { String taskId = historicTaskInstance.getId(); String name = historicTaskInstance.getName(); String assignee = historicTaskInstance.getAssignee(); Date endTime = historicTaskInstance.getEndTime(); System.out.println(taskId+"==="+name+"==="+assignee+"==="+endTime); } }
|
获取任务进度和转存图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| @Autowired private RepositoryService rs;
@Autowired private HistoryService hs;
@Autowired private ProcessEngine pe;
@Test public void getDeployPng() throws Exception { String deploymentId = "2501"; List<String> names = rs.getDeploymentResourceNames(deploymentId); String imageName = null; for (String name : names) { if (name.indexOf(".png") >= 0) { imageName = name; } }
if (imageName != null) { File f = new File("e:/" + imageName); InputStream in = rs.getResourceAsStream(deploymentId, imageName);
FileUtils.copyInputStreamToFile(in, f); } }
@Test public void getHiTask() { String pid = "20001"; HistoricProcessInstance hpi = hs.createHistoricProcessInstanceQuery().processInstanceId(pid).singleResult();
HistoricActivityInstanceQuery q = hs.createHistoricActivityInstanceQuery().processInstanceId(pid);
List<HistoricActivityInstance> list = q.list();
List<String> highLightedActivitis = new ArrayList<String>(); for (HistoricActivityInstance h : list) { highLightedActivitis.add(h.getActivityId()); }
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity) rs.getProcessDefinition(hpi.getProcessDefinitionId()); List<String> highLightFlows=getRedFlows(definitionEntity, list);
BpmnModel bpmnModel = rs.getBpmnModel(hpi.getProcessDefinitionId()); ProcessEngineConfiguration c = pe.getProcessEngineConfiguration(); ProcessDiagramGenerator pdg = c.getProcessDiagramGenerator(); InputStream in = pdg.generateDiagram(bpmnModel, "bmp", highLightedActivitis, highLightFlows, "宋体", "宋体", null, null, 1.0);
try { OutputStream os = new FileOutputStream("e:/aa.bmp"); int r; byte[] bs = new byte[2048]; while ((r = in.read(bs)) != -1) { os.write(bs, 0, r); } os.close(); } catch (Exception e) { e.printStackTrace(); } }
private List<String> getRedFlows(ProcessDefinitionEntity processDefinitionEntity, List<HistoricActivityInstance> historicActivityInstances) { List<String> highFlows = new ArrayList<String>(); for (int i = 0; i < historicActivityInstances.size() - 1; i++) { ActivityImpl activityImpl = processDefinitionEntity .findActivity(historicActivityInstances.get(i).getActivityId()); List<ActivityImpl> sameStartTimeNodes = new ArrayList<ActivityImpl>(); ActivityImpl sameActivityImpl1 = processDefinitionEntity .findActivity(historicActivityInstances.get(i + 1).getActivityId()); sameStartTimeNodes.add(sameActivityImpl1); for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) { HistoricActivityInstance activityImpl1 = historicActivityInstances.get(j); HistoricActivityInstance activityImpl2 = historicActivityInstances.get(j + 1); if (activityImpl1.getStartTime().equals(activityImpl2.getStartTime())) { ActivityImpl sameActivityImpl2 = processDefinitionEntity .findActivity(activityImpl2.getActivityId()); sameStartTimeNodes.add(sameActivityImpl2); } else { break; } } List<PvmTransition> pvmTransitions = activityImpl.getOutgoingTransitions(); for (PvmTransition pvmTransition : pvmTransitions) { ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition.getDestination(); if (sameStartTimeNodes.contains(pvmActivityImpl)) { highFlows.add(pvmTransition.getId()); } } } return highFlows; }
|
相关文章
数据库连接池
SpringIOC
Junit和Spring
Tomcat
Servlet
Request,Response和ServletContext
Cookie和Session
JSP和EL和Jstl
Filter和Listener
Mybatis