activiti(三)activiti 6.0 模块介绍及编写helloWorld程序

同文

购物流程模型化

1.绘制BPMN

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第1张图片

填写id和name

StartEvent                       ------------- 开始

UserTask1                      ------------- 提交审批信息

decideSubmit                 ------------- 提交OR取消

tl_approve                      ------------- 主管审批

decideTLApprove           ------------- 主管审批校验

hr_approve                     ------------- 人事审批

decideHRApprove          ------------- 人事审批校验

EndEvent                        ------------- 结束

2.表单绘制

2.1点击提交审批信息

点击form ,填写表单字段 (id,name,Tyle,Required)

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第2张图片activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第3张图片

2.2点击 主管审批

点击form ,填写表单字段 (id,name,Tyle,Required)

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第4张图片

2.3 点击 人事审批

 点击form ,填写表单字段 (id,name,Tyle,Required)

 activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第5张图片

2.4 点击 提交OR取消  和  主管审批 中间的连接线,编辑

在condition填入表达式: ${submitType == "y" || submitType == "Y"}

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第6张图片

2.5点击主管审批校验人事审批中间的连接线,编辑

在condition填入表达式:

${tlApprove == "y" || tlApprove == "Y"}

2.6点击主管审批校验提交审批信息中间的连接线,编辑

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第7张图片

在condition填入表达式:

${tlApprove == "n" || tlApprove == "N"}

 2.7 同理 人事审批校验的两条线编辑

${hrApprove == "y" || hrApprove == "Y"}

${hrApprove == "n" || hrApprove == "N"}

2.8保存文件,修改文件后缀名bpmn为xml,右键

activiti(三)activiti 6.0 模块介绍及编写helloWorld程序_第8张图片

3.java编码

3.1.maven配置



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
        
    

    com.nn
    activiti-test
    1.0-SNAPSHOT

    
        
            org.activiti
            activiti-engine
            6.0.0
        
        
            junit
            junit
            4.11
        
        
            ch.qos.logback
            logback-classic
            1.1.11
        
        
            com.google.guava
            guava
            23.0
        
        
            com.h2database
            h2
            1.3.176
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    8
                    8
                
            

            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3.2 logback.xml配置



    
    
    
    
    
    
    
        
            ${plain}
            ${encoding}
        
    

    
    
        ${log.dir}/file.log
        
            ${log.dir}/file.%d{yyyy-MM-dd}.log
            30
        
        
            ${std}
            ${encoding}
        
    
    
        
    
    
    
        
    
    
        
        
    

3.3  DemoMain

package com.nn.activiti;

import com.google.common.collect.Maps;
import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.StringFormType;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author huangRuYuan 2019/10/9
 */
public class DemoMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);

    public static void main(String[] args) {
        // 创建流程引擎
        ProcessEngine processEngine = getProcessEngine();

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

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

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

    private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) {
        Scanner scanner = new Scanner(System.in);
        while (processInstance != null && !processInstance.isEnded()) {
            TaskService taskService = processEngine.getTaskService();
            List list = taskService.createTaskQuery().list();
            LOGGER.info("待处理任务数量[{}]", list.size());
            for (Task task : list) {
                LOGGER.info("待处理任务[{}]", task.getName());
                Map variables = getMap(processEngine, scanner, task);
                taskService.complete(task.getId(), variables);
                processInstance = processEngine.getRuntimeService()
                        .createProcessInstanceQuery()
                        .processInstanceId(processInstance.getId())
                        .singleResult();
            }
        }
        scanner.close();
    }

    private static Map getMap(ProcessEngine processEngine, Scanner scanner, Task task) {
        FormService formService = processEngine.getFormService();
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List formProperties = taskFormData.getFormProperties();
        Map variables = Maps.newHashMap();
        formProperties.forEach((property) -> {
            String line = null;
            if (StringFormType.class.isInstance(property.getType())) {
                LOGGER.info("请输入{}?", property.getName());
                line = scanner.nextLine();
                variables.put(property.getId(), line);
            } else if (DateFormType.class.isInstance(property.getType())) {
                LOGGER.info("请输入{}?格式 (yyyy-MM-dd)", property.getName());
                line = scanner.nextLine();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = simpleDateFormat.parse(line);
                    variables.put(property.getId(), date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                LOGGER.info("类型暂不支持{}", property.getType());
            }
            LOGGER.info("您输入的是[{}]", line);
        });
        return variables;
    }

    private static ProcessInstance processInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        LOGGER.info("启动流程{}", processInstance.getProcessDefinitionKey());
        return processInstance;
    }

    private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        deploymentBuilder.addClasspathResource("diagrams/second_approve.bpmn20.xml");
        Deployment deploy = deploymentBuilder.deploy();
        String deployId = deploy.getId();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).singleResult();
        LOGGER.info("流程定义文件 {},流程Id {}", processDefinition.getName(), processDefinition.getId());
        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;
    }

}


second_approve.bpmn20.xml



  
    
    
      
        
        
        
        
      
    
    
    
    
    
      
        
        
      
    
    
    
      
    
    
    
      
    
    
    
    
    
      
        
        
      
    
    
    
      
    
    
    
      
    
    
      
    
  
  
    
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
          
        
      
    
  

activiti.cfg.xml



    

        
        
        
        

        
        
        
        

        
    

你可能感兴趣的:(activiti,maven,java)