Skip to main content

Activiti 6.0 源码分析 helloword


Activiti 6.0 源码分析 helloword 学习

获取源码

  1. 然后从自己目录下把项目克隆到本地
  2. 切换分支>git checkout -b study6 activiti-6.0.0
  3. 编译>mvn clean test-compile
  4. 导入到编辑器

Activiti6.0模块介绍

  • module/activiti-engine 核心模块
  • module/activiti-spring Spring集成模块
  • module/activiti-sping-boot SpringBoot集成模块
  • module/activiti-rest 对外提供rest api模块
  • module/activiti-form-engine 表单引擎模块
  • module/activiti-ldap 集成ldap用户的模块

基于源码activiti-app运行

启动cativiti-app

cd modules/activiti-ui/activiti-app
mvn clean tomcat7:run

打开浏览器访问http://127.0.0.1:9999/activiti-app/

activiti-ui

activiti-app 集成发布的war工程

activiti-app-conf UI独立于业务外的配置

activiti-app-logic UI的业务逻辑

activiti-app-rest 提供接口的rest api

Activiti6.0初体验helloword

通过activiti-app画好流程图并设置属性

  • id:startEvent | name:开始
  • id:submitForm | name:填写审批信息
  • id:decideSubmit | name:提交OR取消
  • id:endEventCancel | name:取消
  • id:tl_approve | name:主管审批
  • id:decideTLApprove | name:主管审批校验
  • id:hr_approve | name:人事审批
  • id:decideHRApprove | name:人事审批校验
  • id:endEvent |name:结束

设置form表单信息

填写审批信息表单

主管审批表单

hr审批表单

设置流转条件

提交or取消:

  • ${submitType=="y"||submitType=="Y"}
  • ${submitType=="n"||submitType=="N"}

主管审批校验:

  • ${tlApprove=="y"||tlApprove=="Y"}
  • ${tlApprove=="n"||tlApprove=="N"}

人事审批校验:

  • ${hrApprove=="y"||hrApprove=="Y"}
  • ${hrApprove=="n"||hrApprove=="N"}

导出工作流xml文件创建demoActiviti项目

  1. 把导入文件放入项目里
  2. 引入jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.guosh.activiti</groupId>
    <artifactId>guosh-activiti</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!--activiti核心模块-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.11</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
        <!--h2内存数据库-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.176</version>
        </dependency>
    </dependencies>

</project>
  1. 流程demo
public class DemoMain {
    private static  final Logger logger= LoggerFactory.getLogger(DemoMain.class);

    public static void main(String[] args) throws ParseException {
        logger.info("启动程序");

        //创建流程引擎(基于内存数据库的流程引擎对象)
        ProcessEngine processEngine = getProcessEngine();

        //部署流程定义文件
        ProcessDefinition processDefinition = getProcessDefinition(processEngine);

        //启动流程
        ProcessInstance processInstance = getProcessInstance(processEngine, processDefinition);


        //处理流程任务
        processTask(processEngine, processInstance);
        logger.info("结束程序");
    }

    private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
        Scanner scanner = new Scanner(System.in);

        //流程不为空并且流程没有结束
        while (processInstance!=null && !processInstance.isEnded()){
            //任务Service用于管理、查询任务,例如签收、办理、指派等
            TaskService taskService = processEngine.getTaskService();
            List<Task> list = taskService.createTaskQuery().list();
            for (Task task:list) {
                logger.info("待处理任务 [{}]",task.getName());
                Map<String, Object> variables = getStringObjectMap(processEngine, scanner, task);
                //提交
                taskService.complete(task.getId(),variables);
                //获取流程对象最新状态
                processInstance=processEngine.getRuntimeService().createProcessInstanceQuery()
                        .processInstanceId(processInstance.getId()).singleResult();


            }
            logger.info("待处理任务数量 [{}]",list.size());
        }
        scanner.close();
    }

    private static Map<String, Object> getStringObjectMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
        //表单Service用于读取和流程、任务相关的表单数据
        FormService formService = processEngine.getFormService();
        //获取任务表单
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        //获取属性集合
        List<FormProperty> formProperties = taskFormData.getFormProperties();
        //存储要提交的表单
        Map<String, Object> variables=new HashMap<String, Object>();
        String line=null;
        for (FormProperty property:formProperties){
            //如果是string类型
            if(StringFormType.class.isInstance(property.getType())){
                logger.info("请输入 {}",property.getName());
                line=scanner.nextLine();
                logger.info("您输入的内容是 [{}]",line);
                variables.put(property.getId(),line);
            }else if(DateFormType.class.isInstance(property.getType())){
                logger.info("请输入 {} 格式 (yyyy-MM-dd)",property.getName());
                line=scanner.nextLine();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(line);
                variables.put(property.getId(),date);
            }else{
                logger.info("类型不支持");
            }
        }
        return variables;
    }

    private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
        //运行时Service可以处理所有运行状态的流程实例流程控制(开始,暂停,挂起等)
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        logger.info("启动流程[{}]",processInstance.getProcessDefinitionKey());
        return processInstance;
    }

    private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
        //流程仓库Service,可以管理流程仓库例如部署删除读取流程资源
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("second_approve.bpmn20.xml")
                .deploy();

        //部署id
        String deploymentId = deployment.getId();

        //流程对应对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();

        logger.info("流程名称 [{}],流程ID [{}],流程KEY [{}]",processDefinition.getName(),processDefinition.getId(),processDefinition.getKey());
        return processDefinition;
    }

    private static ProcessEngine getProcessEngine() {
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        //构造流程引擎
        ProcessEngine processEngine = cfg.buildProcessEngine();
        String name = processEngine.getName();
        String version = processEngine.VERSION;
        logger.info("流程引擎名称 [{}],版本 [{}]",name,version);
        return processEngine;
    }
}