springCloud集成activiti5.22.0流程引擎

news2024/10/5 13:52:01

springCloud集成activiti5.22.0流程引擎

   点关注不迷路,欢迎再访!	

精简博客内容,尽量已行业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。

小编最近工作需要涉及到流程,由于网络上5.22版本资料最多,所以选用了5.22版本作为项目集成。

文章目录

    • springCloud集成activiti5.22.0流程引擎
      • 一.Activiti表介绍
        • 1 .表前缀说明
      • 二.Spring boot2.x与Activiti 5.22.0整合
        • 1.POM文件中添加依赖
        • 2.启动类配置
        • 3.ActivitiConfiguration 配置
        • 4.封装activiti工具类
      • 三.制作bpmn流程文件
        • 1.安装actiBPM插件
        • 2.创建bpmn文件
        • 3.生成png文件
      • 四.流程演示
        • 1.流程定义
        • 2.启动流程
        • 3.查询我的待办
        • 4.完成任务

一.Activiti表介绍

不同版本的activiti自带表是不一样的,其实我们也不用去关心不同版本的activiti需要那些表,只要配置好数据源,系统启动的时候,会去检查数据库是否包含这些数据库表,如果没有会自动去创建这些表。下面先介绍简单介绍一下,这些表的含义。

表关系图介博客(引用):https://blog.csdn.net/claram/article/details/73277358

1 .表前缀说明
ACT_RE RE表示Repository资源库,保存流程定义,模型等设计阶段的数据。
ACT_RU RU表示Runtime运行时,保存流程实例,任务,变量等运行阶段的数据。
ACT_HI 表示History历史,保存历史实例,历史任务等流程历史数据。
ACT_ID ID表示Identity身份,保存用户,群组,关系等组织机构相关数据(Activiti中的组织机构过于简单,仅用于演示。) 。
ACT_GE GE表示General通用,属于一些通用配置。
其他 ACT_EVT_LOGACT_PROCDEF_INFO没有按照规则来,两者分别属于HIRE

二.Spring boot2.x与Activiti 5.22.0整合

1.POM文件中添加依赖
<dependency>
		<groupId>org.activiti</groupId>
			<artifactId>activiti-spring-boot-starter-basic</artifactId>
		<version>5.22.0</version>
		<exclusions>
		<!-- 排除activiti的mybatis,避免和外面的mybatis-plus冲突 -->
			<exclusion>
				<artifactId>mybatis</artifactId>
				<groupId>org.mybatis</groupId>
			</exclusion>
		</exclusions>
</dependency>
2.启动类配置

注意:SecurityAutoConfiguration.class导入的包是activiti的

/**
*因为GlobalAuthenticationConfigurerAdapter  是spring-boot-starter-security 依赖中的属于安全配置类,  而 引入的activiti-spring-boot-starter-basic 依赖中存在了一个自动安全配置类,  两个安全配置,  所以排除掉 activiti-spring-boot-starter-basic中的安全配置类  SecurityAutoConfiguration
*/
@EnableAutoConfiguration(exclude={org.activiti.spring.boot.SecurityAutoConfiguration.class})
@SpringCloudApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
3.ActivitiConfiguration 配置
@Configuration
public class ActivitiConfiguration {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    public ActivitiConfiguration() {
    }

    //通过@Bean注解将SpringProcessEngineConfiguration实例声明为Spring Bean,使其可供其他组件注入和使用
    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        //设置数据源,将注入的数据源设置到SpringProcessEngineConfiguration实例中
        spec.setDataSource(this.dataSource);
        //设置事务管理器将注入的事务管理器设置到SpringProcessEngineConfiguration实例中
        spec.setTransactionManager(this.platformTransactionManager);
        //设置数据库模式更新策略 true表示在启动时自动创建或更新Activiti引擎所需的数据库表结构
        spec.setDatabaseSchemaUpdate("true");
        Resource[] resources = null;
        //配置流程部署资源
        //使用PathMatchingResourcePatternResolver从classpath中的bpmn目录下加载所有以.bpmn为扩展名的文件作为流程定义资源,
        // 并将它们设置到SpringProcessEngineConfiguration实例中。
        try {
            resources = (new PathMatchingResourcePatternResolver()).getResources("processes/*.bpmn");
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        spec.setDeploymentResources(resources);
        return spec;
    }
}
4.封装activiti工具类
/**
 * 载入 BPMN 文件并部署流程定义
 */
@Slf4j
@Component
public class QuestWorkFlowUtils {

	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private RuntimeService runtimeService;

	/**
	 * 声明定义流程
	 * @return
	 */
	public String createProcessDefinition() throws FileNotFoundException {
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		Deployment deployment = processEngine.getRepositoryService()
				.createDeployment()
				.addClasspathResource("processes/quest_flow.bpmn")
				.addClasspathResource("processes/quest_flow.png")
				.deploy();
		return deployment.getId();
	}

	/**
	 * 启动流程
	 * @param userId  用户ID
	 * @return
	 */
	public ProcessInstance createProcessInstance(String userId){
		//启动流程时传递的参数列表 这里根据实际情况 也可以选择不传
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("userId", userId);
		// 根据流程定义ID查询流程定义  process_test:1:10004部署的流程定义的id
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionId("quest_flow:1:10004")     //act_re_procdef主键ID
				.singleResult();

		// 获取流程定义的Key
		String processDefinitionKey = processDefinition.getKey();
		//定义businessKey  businessKey一般为流程实例key与实际业务数据的结合
		String businessKey = processDefinitionKey + ":" + "001";
		//设置启动流程的人
		Authentication.setAuthenticatedUserId("sysadmin");
		ProcessInstance processInstance = this.runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
		System.out.println("流程启动成功:" + processInstance);
		return processInstance;
	}

	/**
	 * 我的待办
	 * @param userId  用户ID
	 * @param processDefinitionKey  定义流程key
	 * @return
	 */
	public List<Task> getProcessEngineList(String userId,String processDefinitionKey){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		TaskService taskService = processEngine.getTaskService();
		List<Task> list = taskService.createTaskQuery()
				.processDefinitionKey(processDefinitionKey)
				.taskAssignee(userId)
				.list();
		for (Task task : list) {
			System.out.println("流程实例id = " + task.getProcessInstanceId());
			System.out.println("任务id = " + task.getId());
			System.out.println("任务负责人id = " + task.getAssignee());
			System.out.println("任务名称 = " + task.getName());
		}
		return list;
	}

	/**
	 * 通过taskId办理任务
	 * @param taskId  流程任务ID
	 */
	public void getProcessEngine(String taskId,Map<String, Object> variables){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		processEngine.getTaskService().complete(taskId,variables);
	}

	/**
	 * 完成任务
	 * @return
	 */
	public void finishProcessTask(){
		// 1.获取流程引擎
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 2.获取taskService
		TaskService taskService = processEngine.getTaskService();
		Task task = taskService.createTaskQuery()
				.processDefinitionKey("quest_work")
				.taskAssignee("sysadmin")
				.singleResult();
		taskService.complete(task.getId());
	}

	/**
	 * 查看流程图附件
	 * @param deploymentId  流程定义ID
	 */
	public void get(String deploymentId){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		List<String> resourceNames = processEngine.getRepositoryService()
				.getDeploymentResourceNames(deploymentId);
		resourceNames.forEach(resourceName -> {
			System.err.println(resourceName);
			File file = new File("e:/" + resourceName);
			InputStream inputStream = processEngine.getRepositoryService()
					.getResourceAsStream(deploymentId, resourceName);
			try {
				FileOutputStream fileOutputStream = new FileOutputStream(file);
				byte[] bytes = new byte[1024];
				while (inputStream.read(bytes) > 0) {
					fileOutputStream.write(bytes);
				}
				fileOutputStream.flush();
				inputStream.close();
				fileOutputStream.close();
			} catch (Exception e) {

			}
		});
	}

	/**
	 * 撤销任务
	 * @param processInstanceId  流程实例ID
     * @param deleteReason  原因
	 */
	public void deleteProcessTask(String processInstanceId, String deleteReason){
		runtimeService.deleteProcessInstance(processInstanceId,deleteReason);
	}
}

三.制作bpmn流程文件

1.安装actiBPM插件

地址: https://plugins.jetbrains.com/
搜索: actiBPM
在这里插入图片描述

IDEA 插件安装 File->Settings->Plugins 安装新插件actiBPM插件
可能出现查找不到该插件的情况,则可以actiBPM下载通过插件jar文件本地安装
!](https://img-blog.csdnimg.cn/direct/25b563d7703f4d11952886d2857c4864.png)

2.创建bpmn文件

右键该文件夹New–>BPMN File 创建名为quest_flow的流程文件
在这里插入图片描述
文件名"xxx.bpmn" 更改为 “xxx.bpmn.xml”
注: 因为activiti的默认流程图格式是bpmn, 但是idea必须xml格式才能生成图片, 所以改为这样, 后面部署流程的时候才可以部署上去, 否则是存不进数据库.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1713244634324" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="quest_work" isClosed="false" isExecutable="true" processType="None">
    <startEvent id="STEP1" name="开始"/>
    <endEvent id="STEP3" name="结束"/>
    <userTask activiti:assignee="${userId}" activiti:exclusive="true" id="STEP2" name="问卷人员"/>
    <sequenceFlow id="_5" sourceRef="STEP1" targetRef="STEP2"/>
    <sequenceFlow id="_6" sourceRef="STEP2" targetRef="STEP3"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="quest_work">
      <bpmndi:BPMNShape bpmnElement="STEP1" id="Shape-STEP1">
        <omgdc:Bounds height="32.0" width="32.0" x="200.0" y="40.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="STEP3" id="Shape-STEP3">
        <omgdc:Bounds height="32.0" width="32.0" x="200.0" y="340.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="STEP2" id="Shape-STEP2">
        <omgdc:Bounds height="55.0" width="85.0" x="175.0" y="170.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_5" id="BPMNEdge__5" sourceElement="_2" targetElement="_4">
        <omgdi:waypoint x="216.0" y="72.0"/>
        <omgdi:waypoint x="216.0" y="170.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_4" targetElement="_3">
        <omgdi:waypoint x="216.0" y="225.0"/>
        <omgdi:waypoint x="216.0" y="340.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
3.生成png文件

右键更改后文件 Diagrams–>Show BPMN 2.0 Designer…
在这里插入图片描述

四.流程演示

1.流程定义

自动部署:将bpmn文件放到resources下的processes下,启动时会自动部署
在这里插入图片描述
查看数据库表 act_re_procdef:
在这里插入图片描述

2.启动流程
	//启动流程时传递的参数列表 这里根据实际情况 也可以选择不传
	Map<String, Object> variables = new HashMap<String, Object>();
	variables.put("userId","sysadmin");
	// 根据流程定义ID查询流程定义  quest_work:1:47504是我们刚才部署的流程定义的id
	ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionId("quest_work:1:47504")     //act_re_procdef主键ID
				.singleResult();

	// 获取流程定义的Key
	String processDefinitionKey = processDefinition.getKey();
	//定义businessKey  businessKey一般为流程实例key与实际业务数据的结合
	String businessKey = processDefinitionKey + ":" + "001";
	//设置启动流程的人
	Authentication.setAuthenticatedUserId("sysadmin");
	ProcessInstance processInstance = this.runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
	System.out.println("流程启动成功:" + processInstance);
3.查询我的待办
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	TaskService taskService = processEngine.getTaskService();
	List<Task> list = taskService.createTaskQuery()
				.processDefinitionKey(processDefinitionKey)
				.taskAssignee("sysadmin")
				.list();
		for (Task task : list) {
			System.out.println("流程实例id = " + task.getProcessInstanceId());
			System.out.println("任务id = " + task.getId());
			System.out.println("任务负责人id = " + task.getAssignee());
			System.out.println("任务名称 = " + task.getName());
		}
4.完成任务
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	processEngine.getTaskService().complete(taskId);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1617777.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AD--SSL卸载--单向认证和双向认证

一.SSL卸载单向认证 1.添加SSL证书 2.添加SSL卸载策略 由于是测试模拟环境&#xff0c;有些效果表现不出来&#xff0c;配置不了卸载策略 3.起虚拟服务&#xff0c;服务类型选择https或者ssl ,选择SSL卸载策略 实验效果&#xff1a;打开网页进入AD抓包发现&#xff0c;客户端和…

MySQL及SQL语句

SQL语句 数据库相关概念数据查询语言&#xff08;DQL&#xff09;基本查询数据类型条件查询多表查询子查询 数据操作语言&#xff08;DML&#xff09;数据定义语言&#xff08;DDL&#xff09;数据控制语言&#xff08;DCL&#xff09;MySQL数据库约束视图练习题 数据库相关概念…

8【PS作图】画一个“像素云朵”

选择64*128像素大小&#xff0c;横向画布 选择“油漆桶”工具&#xff0c;“容差”调整为0&#xff0c;取消“锯齿”&#xff0c;勾选“连续的”&#xff0c;这样方便后续上色&#xff0c;并且边缘都是像素风格的锯齿状 点击画布&#xff0c;变成蓝色天空 画云朵&#xff0c;首…

win10环境中设置java开机自启动

1 、jdk环境确认 在开始设置Java开机启动之前&#xff0c;确保你的计算机已经安装了Java开发环境&#xff08;JDK&#xff09;。如果没有安装&#xff0c;你可以从Oracle官方网站下载并安装最新的Java开发工具包。 2、准备好jar程序 确认jar程序可以正常运行。 3、编写批处…

【InternLM】大模型的评测——OpenCompass

1. OpenCompass简介 1.1 基本介绍 大模型开源开放评测体系 “司南” (OpenCompass2.0)由上海人工智能实验室科学家团队发布&#xff0c;用于为大语言模型、多模态模型等提供一站式评测服务。其主要特点如下&#xff1a; 开源可复现&#xff1a;提供公平、公开、可复现的大模型…

聊聊实际工作中设计模式的使用

一直想在CSDN上写一篇关于软件设计模式的文章&#xff0c;草稿打了好久&#xff0c;但很长时间都没有想好该如何写&#xff0c;主要有几点考虑&#xff1a; 1、市面上同类的介绍实在太多了。正所谓第一个能够把美女比喻成鲜花的人是天才&#xff0c;第二个还这么说的是庸才&…

Kotlin语法入门-类与对象(6)

Kotlin语法入门-类与对象(6) 文章目录 Kotlin语法入门-类与对象(6)六、类与对象1、声明和调用2、get和set3、init函数初始化4、constructor构造函数4.1、主构造函数4.2、二级构造函数4.3、多个构造函数4.4、省略主构造函数并写了次构造函数 5、类的继承与重写5.1、继承5.2、继承…

【Tello无人机】无人机编队操作面板实现

为了方便进行无人机的编队演示&#xff0c;以及在各种场景下实现队形的灵活切换&#xff0c;开发了一套专门的上位机控制平台。本文将重点介绍应用于tello无人机的编队操作面板及其核心功能。 操作面板页面 下图展示了操作面板&#xff0c;其中包含5种编队动作和3个可选位置设…

2024深圳杯(东北三省)数学建模选题建议及各题思路来啦!

大家好呀&#xff0c;2024深圳杯数学建模&#xff08;东北三省数学建模联赛&#xff09;开始了&#xff0c;来说一下初步的选题建议吧&#xff1a; 首先定下主基调&#xff0c; 本次深圳杯&#xff08;东北三省&#xff09;建议选A。难度上D&#xff1e;B&#xff1e;C&#…

开源模型应用落地-chatglm3-6b-集成langchain(十)

一、前言 langchain框架调用本地模型&#xff0c;使得用户可以直接提出问题或发送指令&#xff0c;而无需担心具体的步骤或流程。通过LangChain和chatglm3-6b模型的整合&#xff0c;可以更好地处理对话&#xff0c;提供更智能、更准确的响应&#xff0c;从而提高对话系统的性能…

Linux中进程和计划任务管理(2)

一.进程命令 1.lsof lsof 命令&#xff0c;“list opened files”的缩写&#xff0c;直译过来&#xff0c;就是列举系统中已经被打开的文件。通过 lsof 命令&#xff0c;我们就可以根据文件找到对应的进程信息&#xff0c;也可以根据进程信息找到进程打开的文件。 格式&…

【详细实现】v1.0 随机点名应用

1.引言 前面对这个应用的功能做了详细的分析&#xff08;长什么样、能干什么&#xff09;&#xff0c;以先这样对一个项目最开始的分析成为需求分析&#xff0c;需求分析之后就是设计阶段。 那么一般的项目&#xff0c;在设计阶段都需要干什么呢&#xff1f;在需求分析阶段确定…

Linux系统中安装MySQL

1、在电脑中安装虚拟机 2、df -h查看光盘是否挂载&#xff0c;没挂载用mount -o ro /dev/sr0 /media命令挂载 3、进入etc/yum.repos.d目录查看仓是否配置&#xff0c;若配置进行下一一步&#xff0c;未配置则进行配置 配置软件仓库 [rootlocalhost yum.repos.d]# vim rhle.r…

423 世界读书日 和京东零售技术人一起读好书

我们正处于一个复杂、变化的世界&#xff0c;想要更好地理解、适应它&#xff0c;读书可能是最方便的方式之一。 4 月 23 日世界读书日&#xff0c;我们整理了 10 位零售技术人的书籍推荐给大家&#xff0c;欢迎大家一起来共读好书。愿大家在忙碌工作之余&#xff0c;都能够持…

Kubectl常见排查pod问题命令

一.查看命名空间pod及其日志 #查看命名空间pod kubectl get pods -n <命名空间名称> #该命令不加-n命名空间名称&#xff0c;默认是查看default命名空间的pod#查看对应pod的日志kubectl logs -f <pod-name> -n <namespace>#同样的如果查看的是default命名空…

在 vue3 中使用高德地图

前言&#xff1a;定位地图位置所需要的经纬度&#xff0c;可以通过 拾取坐标 获取。 一&#xff1a;快速上手 1. 安装依赖 npm install amap/amap-jsapi-loader # or pnpm add amap/amap-jsapi-loader # or yarn add amap/amap-jsapi-loader 2. 创建组件 src/components/Ma…

有关栈的练习

栈练习1 给定一个栈&#xff08;初始为空&#xff0c;元素类型为整数&#xff0c;且小于等于 109&#xff09;&#xff0c;只有两个操作&#xff1a;入栈和出栈。先给出这些操作&#xff0c;请输出最终栈的栈顶元素。 操作解释&#xff1a; 1 表示将一个数据元素入栈&#xff…

Mysql 存在多条数据,按时间取最新的那一组数据

1、数据如下&#xff0c;获取每个用户最近的一次登录数据 思路1&#xff1a;order by group by 先根据UserIdLogInTime排序&#xff0c;再利用Group分组&#xff0c;即可得到每个User_Id的最新数据。 1 SELECT * FROM login_db l ORDER BY l.user_id, l.login_time DESC; 排…

一款辅助应用助力盲人公交出行畅行无阻

在这个日新月异的时代&#xff0c;科技进步正以前所未有的速度改变着人们的生活方式&#xff0c;尤其是在提升特殊群体生活质量方面展现出巨大潜力。今日&#xff0c;我们将目光聚焦于盲人公交出行&#xff0c;探讨一款名叫蝙蝠避障的创新辅助应用如何以其实时避障与拍照识别功…

vben admin Table 实现表格列宽自由拖拽

更改BasicTable.vue文件 Table添加 resize-column“resizeColumn” 添加并 return resizeColumn const resizeColumn (w, col) > { setCacheColumnsByField(col.dataIndex, { width: w }); }; 在column中添加 resizable: true,