Springboot 2.0.3.RELEASE 与 activiti 5.22.0 整合

2024-08-29 06:48

本文主要是介绍Springboot 2.0.3.RELEASE 与 activiti 5.22.0 整合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

activiti Spring boot 整合源码

项目采用Springboot 2.0.3.RELEASE版本以及activiti 5.22.0版本

在acitiviti官网下载完整包https://github.com/Activiti/Activiti/releases/tag/activiti-5.22.0

下载完成后

1、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\main\webapp下的diagram-viewer、editor-app以及modeler.html文件放置在项目resources\static文件夹下。

2、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\resources下的stencilset.json放置在项目resources\static文件夹下。

3、将Activiti-activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor下的main以及model中的java文件放置到项目mian\java目录下

复制完成后,首先将ModelEditoeJsonRestResource.java、ModelSaveRestResource.java、StencilsetRestResource.java上添加 @RequestMapping(value = “/service”)

然后修改resources\static\editor-app\app-cfg.js,如下图
在这里插入图片描述

修改ModelSaveRestResource.java,如下(参考https://blog.csdn.net/qq_21770005/article/details/78713912)

@RestController
@RequestMapping(value = "/service")
public class ModelSaveRestResource implements ModelDataJsonConstants {private static final Logger LOGGER = LogManager.getLogger(ModelSaveRestResource.class);@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ObjectMapper objectMapper;@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)@ResponseStatus(value = HttpStatus.OK)public void saveModel(@PathVariable String modelId, @RequestParam("name") String name,@RequestParam("json_xml") String json_xml, @RequestParam("svg_xml") String svg_xml,@RequestParam("description") String description) {try {Model model = repositoryService.getModel(modelId);ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());modelJson.put(MODEL_NAME, name);modelJson.put(MODEL_DESCRIPTION, description);model.setMetaInfo(modelJson.toString());model.setName(name);repositoryService.saveModel(model);repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));TranscoderInput input = new TranscoderInput(svgStream);PNGTranscoder transcoder = new PNGTranscoder();// Setup outputByteArrayOutputStream outStream = new ByteArrayOutputStream();TranscoderOutput output = new TranscoderOutput(outStream);// Do the transformationtranscoder.transcode(input, output);final byte[] result = outStream.toByteArray();repositoryService.addModelEditorSourceExtra(model.getId(), result);outStream.close();} catch (Exception e) {LOGGER.error("Error saving model", e);throw new ActivitiException("Error saving model", e);}}
}

在pom.xml添加activiti依赖

        <!--activiti--><dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter-basic</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter-actuator</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-rest</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-codec</artifactId><version>1.7</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-css</artifactId><version>1.7</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-svg-dom</artifactId><version>1.7</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-svggen</artifactId><version>1.7</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-explorer</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-diagram-rest</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-simple-workflow</artifactId><version>${activiti.version}</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-spring</artifactId><version>${activiti.version}</version></dependency><!-- END activiti -->

在application.yml中添加activiti的数据源,如下

spring:datasource:activiti:url:jdbc:mysql://127.0.0.1:3306/clouddo?nullCatalogMeansCurrent=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=falseusername: rootpassword: 123asddriverClassName: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource

添加activiti数据源配置类

@Configuration
public class ActitytiDataSourceConfig extends AbstractProcessEngineAutoConfiguration {private static final Logger log = LogManager.getLogger(ActitytiDataSourceConfig.class);@Bean(name = "activitiDataSource")@ConfigurationProperties(prefix = "spring.datasource.activiti")public DataSource activitiDataSource(){log.info("activitiDataSource 初始化...");return new DruidDataSource();}@Beanpublic PlatformTransactionManager transactionManager() {return new DataSourceTransactionManager(activitiDataSource());}@Beanpublic SpringProcessEngineConfiguration springProcessEngineConfiguration() {SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();configuration.setDataSource(activitiDataSource());configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);configuration.setJobExecutorActivate(true);configuration.setTransactionManager(transactionManager());return configuration;}}

以上,基本配置已完成。在启动项目后,将自动创建activiti所需要的25张表。

如报错无法启动,在application.yml中添加

spring: activiti:check-process-definitions: false

成功启动项目后,发现访问项目任何路径都需要输入用户名和密码。这是因为acitivi-rest中集成了spring-security,需要在启动类上添加

@EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.activiti.spring.boot.SecurityAutoConfiguration.class})

访问http://localhost:8080/modeler.html,发现页面未显示内容,这是因为目前还未创建任何model

此时创建ActivitiModelController.java,如下

@Controller
public class ActivitiModelController {private static final Logger log = LogManager.getLogger(ActivitiModelController.class);@AutowiredProcessEngine processEngine;@AutowiredObjectMapper objectMapper;/*** 新建一个空模型*/@RequestMapping("/create")public void newModel(HttpServletResponse response) throws IOException {RepositoryService repositoryService = processEngine.getRepositoryService();//初始化一个空模型Model model = repositoryService.newModel();//设置一些默认信息String name = "new-process";String description = "";int revision = 1;String key = "process";ObjectNode modelNode = objectMapper.createObjectNode();modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);model.setName(name);model.setKey(key);model.setMetaInfo(modelNode.toString());repositoryService.saveModel(model);String id = model.getId();//完善ModelEditorSourceObjectNode editorNode = objectMapper.createObjectNode();editorNode.put("id", "canvas");editorNode.put("resourceId", "canvas");ObjectNode stencilSetNode = objectMapper.createObjectNode();stencilSetNode.put("namespace","http://b3mn.org/stencilset/bpmn2.0#");editorNode.put("stencilset", stencilSetNode);repositoryService.addModelEditorSource(id,editorNode.toString().getBytes("utf-8"));response.sendRedirect("/modeler.html?modelId="+id);}/*** 获取所有模型*/@RequestMapping("/modelList")@ResponseBodypublic Object modelList(){RepositoryService repositoryService = processEngine.getRepositoryService();return repositoryService.createModelQuery().list();}/*** 发布模型为流程定义*/@RequestMapping("/deploy")@ResponseBodypublic Object deploy(String modelId) throws Exception {//获取模型RepositoryService repositoryService = processEngine.getRepositoryService();Model modelData = repositoryService.getModel(modelId);byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());if (bytes == null) {return "模型数据为空,请先设计流程并成功保存,再进行发布。";}JsonNode modelNode = new ObjectMapper().readTree(bytes);BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);if(model.getProcesses().size()==0){return "数据模型不符要求,请至少设计一条主线流程。";}byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);//发布流程String processName = modelData.getName() + ".bpmn20.xml";Deployment deployment = repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes, "UTF-8")).deploy();modelData.setDeploymentId(deployment.getId());repositoryService.saveModel(modelData);return "SUCCESS";}/***  启动流程*/@RequestMapping("/start")@ResponseBodypublic Object startProcess(String keyName) {ProcessInstance process = processEngine.getRuntimeService().startProcessInstanceByKey(keyName);return process.getId() + " : " + process.getProcessDefinitionId();}/***  提交任务*/@RequestMapping("/run")@ResponseBodypublic Object run(String processInstanceId) {Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).singleResult();log.info("task {} find ", task.getId());processEngine.getTaskService().complete(task.getId());return "SUCCESS";}}

通过访问http://localhost:8080/create创建一个空白的model并跳转到编辑页面

在绘制流程完成后,访问http://localhost:8080/deploy?modelId=1 对该流程进行部署

http://localhost:8080/start?keyName=hello 启动流程

http://localhost:8080/run?processInstanceId=1 提交

至此,springboot集成activiti modeler完成

需要源码,请及时联系我。
https://download.csdn.net/download/u013116345/10774314

这篇关于Springboot 2.0.3.RELEASE 与 activiti 5.22.0 整合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1117146

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定