工作流activiti技术的研究和学习记录第三天

前两天主要是学习activiti(www.zipin168.com)工作流的一些API和基本知识,今天主要是学习activiti中提供几个service服务知识。

一.管理流程定义

流程图如附件。

bpmn文件如下

<?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: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="helloworld" name="helloworldProcess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="提交申请" activiti:assignee="张三"></userTask>
    <userTask id="usertask2" name="审批【部门经理】" activiti:assignee="李四"></userTask>
    <userTask id="usertask3" name="审批【总经理】" activiti:assignee="王五"></userTask>
    <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="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_helloworld">
    <bpmndi:BPMNPlane bpmnElement="helloworld" id="BPMNPlane_helloworld">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="330.0" y="20.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="330.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="295.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="295.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="295.0" y="290.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="347.0" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="347.0" y="100.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="347.0" y="155.0"></omgdi:waypoint>
        <omgdi:waypoint x="347.0" y="200.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="347.0" y="255.0"></omgdi:waypoint>
        <omgdi:waypoint x="347.0" y="290.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="347.0" y="345.0"></omgdi:waypoint>
        <omgdi:waypoint x="347.0" y="380.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

说明:流程定义文档有两部分组成:

1)      bpmn文件

流程规则文件。在部署后,每次系统启动时都会被解析,把内容封装成流程定义放入项目缓存中。Activiti框架结合这个xml文件自动管理流程,流程的执行就是按照bpmn文件定义的规则执行的,bpmn文件是给计算机执行用的

2)      展示流程图的图片

在系统里需要展示流程的进展图片,图片是给用户看的

部署流程定义(classpath路径加载文件)

public void deploymentProcessDefinition() {
  Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
    .createDeployment()// 创建一个部署对象
    .name("helloworld入门程序")// 添加部署的名称
    .addClasspathResource("diagrams/helloworld.bpmn")// 从classpath的资源中加载,一次只能加载一个文件
    .addClasspathResource("diagrams/helloworld.png")// 从classpath的资源中加载,一次只能加载一个文件
    .deploy();// 完成部署
  System.out.println("部署ID:" + deployment.getId());// 1
  System.out.println("部署名称:" + deployment.getName());// helloworld入门程序
 }

说明:

1)                                先获取流程引擎对象:在创建时会自动加载classpath下的activiti.cfg.xml

2)                                首先获得默认的流程引擎,通过流程引擎获取了一个RepositoryService对象(仓库对象)

3)                                由仓库的服务对象产生一个部署对象配置对象,用来封装部署操作的相关配置。

4)                                这是一个链式编程,在部署配置对象中设置显示名,上传流程定义规则文件

5)                                向数据库表中存放流程定义的规则信息。

6)                                这一步在数据库中将操作三张表:

a)                        act_re_deployment(部署对象表)

存放流程定义的显示名和部署时间,每部署一次增加一条记录

b)                        act_re_procdef(流程定义表)

存放流程定义的属性信息,部署每个新的流程定义都会在这张表中增加一条记录。

注意:当流程定义的key相同的情况下,使用的是版本升级

c)                         act_ge_bytearray(资源文件表)

存储流程定义相关的部署信息。即流程定义文档的存放地。每部署一次就会增加两条记录,一条是关于bpmn规则文件的,一条是图片的(如果部署时只指定了bpmn一个文件,activiti会在部署时解析bpmn文件内容自动生成流程图)。两个文件不是很大,都是以二进制形式存储在数据库中

还有另外一种部署方式(文件压缩成zip形式):

public void deploymentProcessDefinition_zip() {
  InputStream in = this.getClass().getClassLoader()
    .getResourceAsStream("diagrams/helloworld.zip");
  ZipInputStream zipInputStream = new ZipInputStream(in);
  Deployment deployment = processEngine.getRepositoryService()
    .createDeployment().name("流程定义")
    .addZipInputStream(zipInputStream).deploy();// 指定zip格式的文件完成部署
  System.out.println("部署ID:" + deployment.getId());//
  System.out.println("部署名称:" + deployment.getName());//
 }

查看流程定义

public void findProcessDefinition() {
  List<ProcessDefinition> list = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
    .createProcessDefinitionQuery()// 创建一个流程定义的查询
    /** 指定查询条件,where条件 */
    // .deploymentId(deploymentId)//使用部署对象ID查询
    // .processDefinitionId(processDefinitionId)//使用流程定义ID查询
    // .processDefinitionKey(processDefinitionKey)//使用流程定义的key查询
    // .processDefinitionNameLike(processDefinitionNameLike)//使用流程定义的名称模糊查询

    /** 排序 */
    .orderByProcessDefinitionVersion().asc()// 按照版本的升序排列
    // .orderByProcessDefinitionName().desc()//按照流程定义的名称降序排列

    /** 返回的结果集 */
    .list();// 返回一个集合列表,封装流程定义
  // .singleResult();//返回惟一结果集
  // .count();//返回结果集数量
  // .listPage(firstResult, maxResults);//分页查询
  if (list != null && list.size() > 0) {
   for (ProcessDefinition pd : list) {
    System.out.println("流程定义ID:" + pd.getId());// 流程定义的key+版本+随机生成数
    System.out.println("流程定义的名称:" + pd.getName());// 对应helloworld.bpmn文件中的name属性值
    System.out.println("流程定义的key:" + pd.getKey());// 对应helloworld.bpmn文件中的id属性值
    System.out.println("流程定义的版本:" + pd.getVersion());// 当流程定义的key值相同的相同下,版本升级,默认1
    System.out.println("资源名称bpmn文件:" + pd.getResourceName());
    System.out.println("资源名称png文件:" + pd.getDiagramResourceName());
    System.out.println("部署对象ID:" + pd.getDeploymentId());
    System.out.println("#########################################################");
   }
  }
 }

说明:

1)                                流程定义和部署对象相关的Service都是RepositoryService

2)                                创建流程定义查询对象,可以在ProcessDefinitionQuery上设置查询的相关参数

3)                                调用ProcessDefinitionQuery对象的list方法,执行查询,获得符合条件的流程定义列表

4)                                由运行结果可以看出:

KeyName的值为:bpmn文件process节点的idname的属性值

5)                                key属性被用来区别不同的流程定义。

6)                                带有特定key的流程定义第一次部署时,version1。之后每次部署都会在当前最高版本号上加1

7)                                Id的值的生成规则为:{processDefinitionKey}:{processDefinitionVersion}:{generated-id}, 这里的generated-id是一个自动生成的唯一的数字

8)                                重复部署一次,deploymentId的值以一定的形式变化

删除流程定义

public void deleteProcessDefinition(){
  String deploymentId="101";//部署的id
  /**
   * 不带级联的删除
   *    只能删除没有启动的流程,如果流程启动,就会抛出异常
   */
     //processEngine.getRepositoryService().deleteDeployment(deploymentId);
  /**
   * 级联删除
   *    不管流程是否启动,都能可以删除
   */
  processEngine.getRepositoryService().deleteDeployment(deploymentId, true);
  System.out.println("删除成功!");
 }

说明:

1)        因为删除的是流程定义,而流程定义的部署是属于仓库服务的,所以应该先得到RepositoryService

2)        如果该流程定义下没有正在运行的流程,则可以用普通删除。如果是有关联的信息,用级联删除。项目开发中使用级联删除的情况比较多,删除操作一般只开放给超级管理员使用。

获取流程定义文档的资源(查看流程图附件)

public void viewPic() throws IOException{
  /**将生成图片放到文件夹下*/
  String deploymentId = "401";
  //获取图片资源名称
  List<String> list = processEngine.getRepositoryService()//
      .getDeploymentResourceNames(deploymentId);
  //定义图片资源的名称
  String resourceName = "";
  if(list!=null && list.size()>0){
   for(String name:list){
    if(name.indexOf(".png")>=0){
     resourceName = name;
     System.out.println("resourceName:"+resourceName);
    }
   }
  }
  //获取图片的输入流
  InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);
  //将图片生成到D盘的目录下
  File file = new File("D:/"+resourceName);
  //将输入流的图片写到D盘下
  FileUtils.copyInputStreamToFile(in, file);
 }

1)        deploymentId为流程部署ID

2)        resourceNameact_ge_bytearray表中NAME_列的值

3)        使用repositoryServicegetDeploymentResourceNames方法可以获取指定部署下得所有文件的名称

4)        使用repositoryServicegetResourceAsStream方法传入部署ID和资源图片名称可以获取部署下指定名称文件的输入流

5)        最后的有关IO流的操作,使用FileUtils工具的copyInputStreamToFile方法完成流程流程到文件的拷贝,将资源文件以流的形式输出到指定文件夹下

猜你喜欢

转载自bonait.iteye.com/blog/2188822