Acitiviti7基本使用- 2、Servcie服务接口介绍

简介: Acitiviti7基本使用- 2、Servcie服务接口介绍

一、总览

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 系统的日常维护

相关文章
|
存储 SQL 关系型数据库
MySQL高级篇——索引失效的11种情况
索引优化思路、要尽量满足全值匹配、最佳左前缀法则、主键插入顺序尽量自增、计算、函数导致索引失效、类型转换(手动或自动)导致索引失效、范围条件右边的列索引失效、不等于符号导致索引失效、is not null、not like无法使用索引、左模糊查询导致索引失效、“OR”前后存在非索引列,导致索引失效、不同字符集导致索引失败,建议utf8mb4
MySQL高级篇——索引失效的11种情况
|
3月前
|
存储 人工智能 文字识别
从零开始打造AI测试平台:文档解析与知识库构建详解
AI时代构建高效测试平台面临新挑战。本文聚焦AI问答系统知识库建设,重点解析文档解析关键环节,为测试工程师提供实用技术指导和测试方法论
|
7月前
|
安全 Java 数据安全/隐私保护
Spring Security: 深入解析 AuthenticationSuccessHandler
本文深入解析了 Spring Security 中的 `AuthenticationSuccessHandler` 接口,它用于处理用户认证成功后的逻辑。通过实现该接口,开发者可自定义页面跳转、日志记录等功能。文章详细讲解了接口方法参数及使用场景,并提供了一个根据用户角色动态跳转页面的示例。结合 Spring Security 配置,展示了如何注册自定义的成功处理器,帮助开发者灵活应对认证后的多样化需求。
218 2
|
架构师 Java 关系型数据库
工作流框架--Activiti6.0(一)
工作流框架--Activiti6.0(一)
364 0
|
8月前
|
人工智能 数据可视化 数据库
优化成本与效率:低代码平台构建企业级应用的技术探析
低代码平台通过可视化开发、拖拽式编程等方式,显著降低软件开发门槛,提升效率。其核心功能包括数据增删改查、图表创建、业务逻辑配置及自定义公式支持等,满足企业多场景需求。同时,平台内置丰富插件生态,涵盖AI融合、实时流处理、数据安全等领域,助力企业实现个性化功能与高效管理。无论是小型团队还是大型企业,低代码平台正逐步成为技术发展的实用助手,推动数字化转型与创新。
|
安全 NoSQL Java
SpringBoot集成Activiti5.22在线流程设计器
SpringBoot集成Activiti5.22在线流程设计器
661 0
SpringBoot集成Activiti5.22在线流程设计器
|
机器学习/深度学习 人工智能 数据可视化
什么是共性技术?它在项目管理中如何发挥关键作用?
在现代技术驱动的项目中,共性技术成为提升生产力、资源效率和创新的关键工具。本文探讨了共性技术在项目管理中的作用,包括标准化流程、提高协作效率、资源优化、降低风险和加速项目交付,并推荐了板栗看板、JIRA和Asana等项目管理工具,助力团队高效协作与管理。
355 7
|
存储 安全 Java
发现 XSS 漏洞?别急!SpringBoot这招轻松搞定!
在SpringBoot中,发现XSS(跨站脚本)漏洞时,可以通过一系列措施来轻松搞定这些安全问题。XSS攻击允许攻击者在受害者的浏览器中注入恶意脚本,这些脚本可以窃取用户的敏感信息、劫持用户会话或进行其他恶意操作。以下是一些在SpringBoot中修复XSS漏洞的有效方法
2052 7
|
Java Spring
【亲测有效完结bug】org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exce
【亲测有效完结bug】org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exce
1481 0
下一篇
开通oss服务