Activiti6.0(二)源码分析helloword

获取源码

Activiti6.0(二)源码分析helloword_第1张图片
image.png

  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:结束
Activiti6.0(二)源码分析helloword_第2张图片
image.png

Activiti6.0(二)源码分析helloword_第3张图片
image.png

设置form表单信息

填写审批信息表单

Activiti6.0(二)源码分析helloword_第4张图片
image.png
Activiti6.0(二)源码分析helloword_第5张图片
image.png

主管审批表单

Activiti6.0(二)源码分析helloword_第6张图片
image.png

hr审批表单

Activiti6.0(二)源码分析helloword_第7张图片
image.png

设置流转条件

Activiti6.0(二)源码分析helloword_第8张图片
image.png

提交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项目

Activiti6.0(二)源码分析helloword_第9张图片
image.png
  1. 把导入文件放入项目里
  1. 引入jar包


    4.0.0

    com.guosh.activiti
    guosh-activiti
    1.0-SNAPSHOT


    
        
        
            org.activiti
            activiti-engine
            6.0.0
        
        
            junit
            junit
            4.12
            test
        
        
        
            ch.qos.logback
            logback-classic
            1.1.11
        
        
            com.google.guava
            guava
            20.0
        
        
        
            com.h2database
            h2
            1.3.176
        
    


  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 list = taskService.createTaskQuery().list();
            for (Task task:list) {
                logger.info("待处理任务 [{}]",task.getName());
                Map 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 getStringObjectMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
        //表单Service用于读取和流程、任务相关的表单数据
        FormService formService = processEngine.getFormService();
        //获取任务表单
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        //获取属性集合
        List formProperties = taskFormData.getFormProperties();
        //存储要提交的表单
        Map variables=new HashMap();
        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;
    }
}

你可能感兴趣的:(Activiti6.0(二)源码分析helloword)