Activiti 流程引擎(二)

1.引入依赖

<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-engine</artifactId>
	<version>6.0.0</version>
</dependency>

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());
 
    }
}

resources/activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<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> 

3.节点跳转

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

// 删除当前的task
managementService.executeCommand(new DeleteTaskCmd(task.getId()));

// 将当前的task设置为想要退回的节点
managementService.executeCommand(new JumpCmd(task.getId()));

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();
    }
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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值