• Activiti第二部分


    1.引入依赖

    <dependency>
    	<groupId>org.activitigroupId>
    	<artifactId>activiti-engineartifactId>
    	<version>6.0.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.初始化Activity生成表

    import org.activiti.engine.ProcessEngine;
    import org.activiti.engine.ProcessEngineConfiguration;
    import org.junit.Test;
     
    public class ActivitiTest {
     
        @Test
         public void initProcessEngine(){
            //创建引擎配置对象
            ProcessEngineConfiguration configration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
     
            //创建流程引擎对象
            //目标生成数据库表
            configration.setJdbcUrl("jdbc:mysql://172.20.10.10:3306/activitydemo");
            configration.setJdbcDriver("com.mysql.jdbc.Driver");
            configration.setJdbcUsername("root");
            configration.setJdbcPassword("root");
            //设置表的生成策略
            configration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
     
            ProcessEngine processEngine = configration.buildProcessEngine();
     
            System.out.println(processEngine.getName());
         }
         
        /**
         * 数据源的配置在activiti.cfg.xml 中
         */
        @Test
        public void initProcessEngine2(){
            ProcessEngineConfiguration configration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
            ProcessEngine engine = configration.buildProcessEngine();
            System.out.println("初始化流引擎对象成功"+engine.getName());
     
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    resources/activiti.cfg.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     
        <bean id="processEngineConfiguration"
            class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
            <property name="jdbcUrl" value="jdbc:mysql://172.20.10.10:3306/activitydemo">property>
            <property name="jdbcDriver" value="com.mysql.jdbc.Driver">property>
            <property name="jdbcUsername" value="root">property>
            <property name="jdbcPassword" value="root">property>
            <property name="databaseSchemaUpdate" value="true">property>
        bean>
    beans> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.节点跳转

    https://blog.csdn.net/m0_38001814/article/details/104253357

    // 删除当前的task
    managementService.executeCommand(new DeleteTaskCmd(task.getId()));
    
    // 将当前的task设置为想要退回的节点
    managementService.executeCommand(new JumpCmd(task.getId()));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    
    public class DeleteTaskCmd extends NeedsActiveTaskCmd<String> {
     
        public DeleteTaskCmd(String taskId){
            super(taskId);
        }
     
        public String execute(CommandContext commandContext, TaskEntity currentTask){
            TaskEntityManagerImpl taskEntityManager = (TaskEntityManagerImpl)commandContext.getTaskEntityManager();
            // 获取当前任务的执行对象实例
            ExecutionEntity executionEntity = currentTask.getExecution();
            // 删除当前任务,来源任务
            taskEntityManager.deleteTask(currentTask, "jumpReason", false, false);
            // 返回当前任务的执行对象id
            return executionEntity.getId();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public class JumpCmd implements Command<Void> {
     
        /**
         * 目标节点对象
         */
        private FlowNode flowElement;
        /**
         * 当前任务执行id
         */
        private String executionId;
     
        public SetFLowNodeAndGoCmd(FlowNode flowElement,String executionId){
            this.flowElement = flowElement;
            this.executionId = executionId;
        }
     
        public Void execute(CommandContext commandContext){
            // 获取目标节点的来源连线
            List<SequenceFlow> flows = flowElement.getIncomingFlows();
            if(flows==null || flows.size()<1){
                throw new ActivitiException("回退错误,目标节点没有来源连线");
            }
            // 随便选一条目标节点的入线来执行,使当前执行计划为:从所选择的流程线流转到目标节点,实现跳转
            ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
            executionEntity.setCurrentFlowElement(flows.get(0));
            commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    unity 判断平台
    LabVIEW专栏七、队列
    【Spring Cloud系列】 雪花算法原理及实现
    车载测试中:如何处理 bug
    将 Llama2 中文模型接入 FastGPT,再将 FastGPT 接入任意 GPT 套壳应用,真刺激!
    LetCode刷题[简单题](2)括号匹配问题(堆栈)
    MySQL性能优化(硬件,系统配置,表结构,SQL语句)
    每日一题:连续子数组的最大和(动态规划)
    【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter3-bash shell 基础命令
    Linux 网络命令指南
  • 原文地址:https://blog.csdn.net/name_sakura/article/details/127975929