说在前面
该专栏将会持续更新,最终会做成一个完整的项目【前端流程设计器,后端流程引擎服务】
感兴趣的朋友点赞收藏
一、画图
上文我们提到一个Camunda Modeler,安装一下,方便我们获取xml文件,下载地址:流程设计器
一个简单的流程图画好之后,在面板左下角可以看见有XML,点击它,在这里就可以获取到所需要的xml了
在项目的resources文件夹下新建一个以bpmn结尾的文件【一定要以.bpmn结尾】
把上一步生成的xml文件内容拿过来
二、编码
这里直接给出实现类的代码,接口代码就省略了。这里仅给出两种部署方式,其他方法就不提了,不常用。【返回值改一下,这部分代码与本文无关就不贴出来了】
package com.lonewalker.snail.core.service.impl;
import com.lonewalker.snail.common.constant.ProcessConstant;
import com.lonewalker.snail.common.entity.ResultReturn;
import com.lonewalker.snail.common.utils.ResultReturnUtil;
import com.lonewalker.snail.core.service.ProcessDefinitionService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @description: 流程定义相关服务
* @author: LoneWalker
* @create: 2021-12-26
**/
@Service("processDefService")
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {
public final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
RepositoryService repositoryService;
@Override
public ResultReturn deployDefinitionByResource(String name,String resource) {
Deployment deploy = repositoryService.createDeployment()
.name(name)
.addClasspathResource(resource)
.deploy();
logger.info("流程Id:" + deploy.getId());
logger.info("流程Name:" + deploy.getName());
return ResultReturnUtil.success(ProcessConstant.DEPLOY_PROCESS_DEFINITION_SUCCESS);
}
@Override
public ResultReturn deployDefinitionByString(String definitionName,String resourceName,String resource) {
Deployment deploy = repositoryService.createDeployment()
.name(definitionName)
.addString(resourceName+ ProcessConstant.PROCESS_DEFINITION_NAME_SUFFIX,resource)
.deploy();
logger.info("流程Id:" + deploy.getId());
logger.info("流程Name:" + deploy.getName());
return ResultReturnUtil.success(ProcessConstant.DEPLOY_PROCESS_DEFINITION_SUCCESS);
}
}
本文使用第一种,加载classpath下的资源文件来演示,后续会使用第二种
贴一下Controller的代码和Entity
@RestController
@RequestMapping("/processDefinition")
public class ProcessDefinitionController {
@Autowired
ProcessDefinitionService processDefService;
@PostMapping("/deploy")
public ResultReturn deployProcessDefinition(@RequestBody ProcessDefinition processDefinition){
String definitionName = processDefinition.getDefinitionName();
String resourceName = processDefinition.getResourceName();
return processDefService.deployDefinitionByResource(definitionName,resourceName);
}
}
@Data
public class ProcessDefinition implements Serializable {
private String definitionName;
private String resourceName;
private String resource;
}
行了,直接使用postman测试一下吧
三、查看部署的流程定义
这里我们为了和流程引擎区分开所以新建一个项目,因为后续不会使用camunda webapp依赖,只是方便我们调试
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>7.16.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
application.yml 【相关的值对应自己的改一下】
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: 用户名
password: 密码
application:
name: snail-view
camunda:
bpm:
webapp:
#禁止index跳转到Camunda自带的管理界面,默认true
index-redirect-enabled: true
admin-user:
id: demo
password: demo
filter:
create: All tasks
#禁止自动部署resources下面的bpmn文件
auto-deployment-enabled: false
server:
port: 8900
直接启动项目访问
从界面上可以简单看出有一条定义,一次部署