欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【工作流】jbpm(三)——开发流程

程序员文章站 2022-07-04 22:58:25
...

一、前言

      在上一篇博客中,小编向大家介绍了jbpm的环境搭建以及一些服务,的确这些服务我们确实经常使用。所以在这篇博客小编就正式带大家走进jbpm,通过一个实战Demo来领略一下。

二、开发步骤

  • 业务分析,画流程图

  • 开发人员,根据活动图建立jpdl流程定义

  • 开发人员,编写代码

  • 用户,发起流程实例

  • 用户,审批流程

三、开发Demo

3.1 流程定义文件和配置文件

【工作流】jbpm(三)——开发流程

      在src目录下面建立Jbpm,new –> JBPM4 Process Definition,建立如下的流程图:

【工作流】jbpm(三)——开发流程

<?xml version="1.0" encoding="UTF-8"?>

<process name="qingjia" key="test" xmlns="http://jbpm.org/4.4/jpdl">
   <start name="start1" g="263,53,48,48">
      <transition name="to 申请请假" to="申请请假" g="-46,-20"/>
   </start>
   <end name="end1" g="272,351,48,48"/>
   <task assignee="张三" name="申请请假" g="205,143,92,52">
      <transition name="to 部门经理审批" to="部门经理审批" g="-46,-20"/>
   </task>
   <task assignee="李四" name="部门经理审批" g="333,251,92,52">
      <transition name="to 总经理审批" to="总经理审批" g="-45,-20"/>
   </task>
   <task assignee="王五" name="总经理审批" g="491,308,92,52">
      <transition name="to end1" to="end1" g="-45,-20"/>
   </task>
</process>

      jbpm.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>

<jbpm-configuration>

  <import resource="jbpm.default.cfg.xml" />
  <import resource="jbpm.businesscalendar.cfg.xml" />
  <import resource="jbpm.tx.hibernate.cfg.xml" />
  <import resource="jbpm.jpdl.cfg.xml" />
  <import resource="jbpm.bpmn.cfg.xml" />
  <import resource="jbpm.identity.cfg.xml" />

  <!-- Job executor is excluded for running the example test cases. -->
  <!-- To enable timers and messages in production use, this should be included. -->
  <!--
  <import resource="jbpm.jobexecutor.cfg.xml" />
  -->

</jbpm-configuration>

      jbpm.hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.url">
        jdbc:mysql://localhost:3306/jbpmnew
    </property>
    <property name="connectio.useUnicode">true</property>
    <property name="connection.characterEncoding">utf-8</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

     <mapping resource="jbpm.repository.hbm.xml" />
     <mapping resource="jbpm.execution.hbm.xml" />
     <mapping resource="jbpm.history.hbm.xml" />
     <mapping resource="jbpm.task.hbm.xml" />
     <mapping resource="jbpm.identity.hbm.xml" />

  </session-factory>
</hibernate-configuration>

      logging.properties:

handlers= java.util.logging.ConsoleHandler
redirect.commons.logging = enabled

java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = org.jbpm.internal.log.LogFormatter

org.jbpm.level=FINE
# org.jbpm.pvm.internal.tx.level=FINE
# org.jbpm.pvm.internal.wire.level=FINE
# org.jbpm.pvm.internal.util.level=FINE

org.hibernate.level=INFO
org.hibernate.cfg.SettingsFactory.level=SEVERE
org.hibernate.cfg.HbmBinder.level=SEVERE
org.hibernate.SQL.level=FINEST
org.hibernate.type.level=FINEST
# org.hibernate.tool.hbm2ddl.SchemaExport.level=FINEST
# org.hibernate.transaction.level=FINEST

建立服务类JbpmService:专门提供服务。

package com.dmsd.jbpm;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;

public class JbpmService {
    protected RepositoryService repositoryService; // 部署流程服务

    protected ExecutionService executionService; //流程执行服务

    protected TaskService taskService; //任务服务

    protected HistoryService historyService; //历史服务

    protected ManagementService managementService; //流程管理服务

    protected IdentityService identityService; //身份认证


    protected void setup() {
        ProcessEngine processEngine = Configuration.getProcessEngine();
        this.repositoryService=processEngine.getRepositoryService();
        this.executionService = processEngine.getExecutionService();
        this.taskService =processEngine.getTaskService();
        this.historyService=processEngine.getHistoryService();
        this.managementService =processEngine.getManagementService();
        this.identityService =processEngine.getIdentityService();
    }
}

3.2 部署

/**
     * 部署
     */
    @Override
    @Test
    public void deploy() {
        //获取服务
        super.setup();
        //根据文件路径进行部署
        repositoryService.createDeployment().addResourceFromClasspath("qingjia.jpdl.xml").deploy();
    }

3.3 创建流程实例

/**
     * 创建流程实例
     */
    @Override
    @Test
    public void createInstance() {
        //获取服务
        super.setup();
        Map<String, Object> testMap = new HashMap<String, Object>();
        testMap.put("userId", "001");
        testMap.put("userName", "tom");
        //创建流程实例
        ProcessInstance processInstance =executionService.startProcessInstanceByKey("test",testMap );
        System.out.println("流程实例ID============"+processInstance.getId() );

    }

3.4 查看所在节点

      根据流程实例ID,获取流程实例(唯一值),再得到活动节点名称(当前节点)。

/**
     * 查询流程当前所在节点
     */
    @Override
    public void getCurrectActivity() {
        //获取服务
        super.setup();
        String activityName = executionService.createProcessInstanceQuery().processInstanceId("test.10001").uniqueResult().findActiveActivityNames().toString();
        System.out.println("当前任务所在节点===" + activityName);

    }

3.5 获取任务

/**
     * 获取任务
     */
    @Override
    public void getTask() {
        //获取服务
        super.setup();
        List<Task> tasks = taskService.findPersonalTasks("张三");
        Task task = tasks.get(0);
        System.out.println("任务数量============="+tasks.size());
        System.out.println("任务名称==============="+ task.getActivityName());
        System.out.println("任务人员==============="+task.getAssignee()+"任务id=========="+task.getId());
    }

3.6 完成任务

/**
     * 完成任务
     */
    @Override
    public void completeTask() {
        //获取服务
        super.setup();
        //根据任务id完成任务
        taskService.completeTask("10004");
    }

四、小结

      通过对jbpm的学习,熟悉了整个的流程,发现其中的API真是不少,所以以后还是要多多的练习的, 套路很固定,就看自己如何发挥了。

      下一篇博客将向大家一一解释jbpm中的各个节点。