一、总览
service 名称 |
service作用 |
RepositoryService |
activiti 的资源管理类 |
RuntimeService |
activiti 的流程运行管理类 |
TaskService |
activiti 的任务管理类 |
HistoryService |
activiti 的历史管理类 |
ManagerService |
activiti 的引擎管理类 |
样例xml
<?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="evection" name="出差申请单" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <userTask id="usertask1" name="创建请假单" activiti:assignee="zhansan"></userTask> <userTask id="usertask2" name="经理审批" activiti:assignee="lisi"></userTask> <userTask id="usertask3" name="总经理审批" isForCompensation="true" activiti:assignee="wangwu"></userTask> <userTask id="usertask4" name="财务审批" activiti:async="true" activiti:assignee="xiaoming"></userTask> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow> <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="usertask4"></sequenceFlow> <sequenceFlow id="flow5" sourceRef="usertask4" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_evection"> <bpmndi:BPMNPlane bpmnElement="evection" id="BPMNPlane_evection"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="510.0" y="60.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="475.0" y="132.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="475.0" y="250.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3"> <omgdc:Bounds height="55.0" width="105.0" x="475.0" y="370.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4"> <omgdc:Bounds height="55.0" width="105.0" x="475.0" y="480.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="510.0" y="600.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="527.0" y="95.0"></omgdi:waypoint> <omgdi:waypoint x="527.0" y="132.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="527.0" y="187.0"></omgdi:waypoint> <omgdi:waypoint x="527.0" y="250.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="527.0" y="305.0"></omgdi:waypoint> <omgdi:waypoint x="527.0" y="370.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="527.0" y="425.0"></omgdi:waypoint> <omgdi:waypoint x="527.0" y="480.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="527.0" y="535.0"></omgdi:waypoint> <omgdi:waypoint x="527.0" y="600.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
png图片
文件位置
二、RepositoryService
是activiti的资源管理类,提供了管理和控制流程发布包和流程定义的操作。使用工作流建模工具设计的业务流程图需要使用此service将流程定义文件的内容部署到计算机。除了部署流程定义以外还可以:查询引擎中的发布包和流程定义。暂停或激活发布包,对应全部和特定流程定义。 暂停意味着它们不能再执行任何操作了,激活是对应的反向操作。获得多种资源,像是包含在发布包里的文件, 或引擎自动生成的流程图。获得流程定义的pojo版本, 可以用来通过java解析流程,而不必通过xml。
1.部署一个流程
// 1.获取ProcessEngine对象 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 2.获取RepositoryService进行部署操作 RepositoryService repositoryService = engine.getRepositoryService(); // 3.使用RepositoryService进行部署操作 Deployment deploy = repositoryService.createDeployment() .addClasspathResource("bpmn/evection.bpmn") .addClasspathResource("bpmn/evection.png").name("出差申请流程") .deploy(); // 输出部署流程 System.out.println("流程部署的ID:"+deploy.getId()); System.out.println("流程部署的名称:"+deploy.getName());
2.获取一个流程
//获取工作流引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //获取资源管理服务 RepositoryService repositoryService = engine.getRepositoryService(); //定义资源管理查询 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey("evection") .orderByProcessDefinitionVersion() .desc() .list(); //遍历资源管理 for (ProcessDefinition processDefinition : list) { System.out.println("流程定义的ID:"+processDefinition.getId()); System.out.println("流程定义的key:"+processDefinition.getKey()); System.out.println("流程定义的name:"+processDefinition.getName()); }
3.删除一个流程
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = engine.getRepositoryService(); //根据ID删除定义流程 act_re_deployment id repositoryService.deleteDeployment("5001");
4.根据流程ID获取流程部署文件
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
/** * 读取数据库中的资源文件 */ @Test public void test10() throws IOException { //获取ProcesssEngine对象 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //获取RepositoryService对象 RepositoryService repositoryService = engine.getRepositoryService(); //获得部署对象 List<ProcessDefinition> evection = repositoryService.createProcessDefinitionQuery() .processDefinitionKey("evection").list(); ProcessDefinition definition = evection.get(0); //获取流程部署的id String deploymentId = definition.getDeploymentId(); //获取png图片 InputStream pngINput = repositoryService.getResourceAsStream(deploymentId, definition.getDiagramResourceName()); //获取bpmn InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, definition.getResourceName()); //文件操作 File filePng = new File("d:/evection.png"); File fileBpmn = new File("d:/evection.bpmn"); FileOutputStream pngOut = new FileOutputStream(filePng); FileOutputStream bpmnOut = new FileOutputStream(fileBpmn); IOUtils.copy(pngINput, pngOut); IOUtils.copy(bpmnInput, bpmnOut); pngINput.close(); pngOut.close(); bpmnInput.close(); bpmnOut.close(); }
三、RuntimeService
Activiti的流程运行管理类。可以从这个服务类中获取很多关于流程执行相关的信息。
1.启动一个流畅实例
// 1、创建ProcessEngine对象 ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine(); // 2、获取RuntimeService对象 RuntimeService runtimeService = defaultProcessEngine.getRuntimeService(); // 3、输出相关流程实例信息 ProcessInstance evection=runtimeService.startProcessInstanceByKey("evection"); System.out.println("流程定义的ID:"+evection.getProcessDefinitionId()); System.out.println("流程实例的ID"+evection.getId()); System.out.println("当前活动的ID"+evection.getActivityId());
2.启动一个业务流程实例
/** * 启动流程实例,添加businessKey */ @Test public void test01() { //1.获取ProcesEngine对象 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //2.获取RuntimeService对象 RuntimeService runtimeService = engine.getRuntimeService(); //3.启动流程实例 ProcessInstance evection = runtimeService.startProcessInstanceByKey("evection", "1001"); //4.输出流程实例 System.out.println(evection.getBusinessKey()); }
四、TaskService
Activiti的任务管理类。可以从这个类中获取任务的信息。
String assignee="zhansan"; //获取工作流引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //任务查询,获取TaskService对象 TaskService taskService = engine.getTaskService(); //构造查询条件 List<Task> list = taskService.createTaskQuery().processDefinitionKey("evection") .taskAssignee(assignee).list(); //输出查询内容 for (Task task : list) { System.out.println("流程实例ID:"+task.getProcessDefinitionId()); System.out.println("任务ID:"+task.getId()); System.out.println("任务负责人:"+task.getAssignee()); System.out.println("任务名称"+task.getName()); }
//获取工作流引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //任务查询,获取TaskService对象 TaskService taskService = engine.getTaskService(); //任务查询,获取TaskService对象 List<Task> list = taskService.createTaskQuery() .processDefinitionKey("evection") .taskAssignee("zhansan").list(); //完成一条任务 taskService.complete(list.get(0).getId());
五、HistoryService
Activiti的历史管理类,可以查询历史信息,执行流程时,引擎会保存很多数据(根据配置),比如流程实例启动时间,任务的参与者, 完成任务的时间,每个流程实例的执行路径,等等。 这个服务主要通过查询功能来获得这些数据。
1.查询历史
/** * 流程历史信息查看 */ @Test public void test11(){ //获取默认流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); //获取历史管理服务 HistoryService historyService = engine.getHistoryService(); //获取查询 HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery(); //构造查询条件 instanceQuery.processDefinitionId("evection:1:4"); instanceQuery.orderByHistoricActivityInstanceStartTime().desc(); //获取查询结果 List<HistoricActivityInstance> list = instanceQuery.list(); for (HistoricActivityInstance tem : list) { System.out.println(tem.getActivityId()); System.out.println(tem.getActivityName()); System.out.println(tem.getActivityType()); System.out.println(tem.getAssignee()); System.out.println(tem.getProcessDefinitionId()); System.out.println(tem.getProcessInstanceId()); System.out.println("----------------------"); } }
#语句 select RES.* from ACT_HI_ACTINST RES WHERE RES.PROC_DEF_ID_ = ? order by START_TIME_ desc LIMIT ? OFFSET ? #参数 evection:1:4(String), 2147483647(Integer), 0(Integer)
六、ManagementService
Activiti的引擎管理类,提供了对 Activiti 流程引擎的管理和维护功能,这些功能不在工作流驱动的应用
程序中使用,主要用于 Activiti 系统的日常维护