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

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis