文章目录
- 下载与安装
- Camunda Modeler
- Download Camunda 7 Run
- 与Spring Boot集成
- 普通Java项目中集成Camunda
- 手动部署流程
- 查询流程
- 启动流程实例
- 完成任务
- 删除流程定义
- 查找历史节点信息
下载与安装
Camunda下载
Camunda7下载
有2个组件需要下载:
- Open Source Desktop Modeler,用于设计流程图
- Camunda 7 Community Edition,Community表示社区版
Camunda Modeler
Modeler目录:
Modeler界面:
Download Camunda 7 Run
Download Camunda 7 Run可以直接部署Camunda服务,可以直接使用不用再做开发了
配置文件有default.yml、production.yml
执行start脚本启动时,默认使用的是default.yml
在配置文件中可以配置,用户和密码,默认都是demo:
camunda.bpm:admin-user:id: demopassword: demo
可以配置数据库,默认使用h2:
spring.datasource:url: jdbc:h2:./camunda-h2-default/process-engine;TRACE_LEVEL_FILE=0;DB_CLOSE_ON_EXIT=FALSEdriver-class-name: org.h2.Driverusername: sapassword: sa
指定数据库,可以通过SQL目录下的创建表SQL手动创建表:
还需要将数据库驱动放在camunda-bpm-run\configuration\userlib目录下。
如果使用h2直接运行start脚本启动就可以:
Run相关配置文档
Camunda8文档
与Spring Boot集成
除了直接部署Camunda Run,还可以使用SpringBoot继承,可以通过下面的链接生成项目:
SpringBoot Camunda项目生成
生成项目和Camunda Run基本一样,可以通过application.yaml配置文件来进行配置。
默认配置:
spring.datasource.url: jdbc:h2:file:./camunda-h2-databasecamunda.bpm.admin-user:id: demopassword: demo
用户名和密码都是demo,数据库使用的是h2,默认是放在项目目录下
普通Java项目中集成Camunda
引入依赖:
<dependency><groupId>org.camunda.bpm</groupId><artifactId>camunda-engine</artifactId><version>7.15.0</version><scope>provided</scope>
</dependency>
在classpath中(如resources目录下)创建camunda.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 默认id对应的值 为processEngineConfiguration --><bean id="processEngineConfiguration" class="org.camunda.bpm.engine.impl.cfg.StandaloneProcessEngineConfiguration"><property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/clearn?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true"/><property name="jdbcUsername" value="tim"/><property name="jdbcPassword" value="123456"/><property name="databaseSchemaUpdate" value="true"/></bean>
</beans>
databaseSchemaUpdate:
- true表示会检查更新Camunda相关表
- false表示检查相关版本是否匹配
- drop-create表示删除并重新创建表
- create-drop、drop-create、create表示要创建表
相关逻辑:
org.camunda.bpm.engine.impl.SchemaOperationsProcessEngineBuild#execute
org.activiti.engine.impl.db.DbSqlSession#performSchemaOperationsProcessEngineBuild
org.camunda.bpm.engine.impl.db.sql.DbSqlSession#isTablePresent
com.mysql.cj.jdbc.DatabaseMetaDataUsingInfoSchema#getTables
com.mysql.cj.jdbc.DatabaseMetaData#getDatabase
public String getResourceForDbOperation(String directory,String operation,String component) {String databaseType = dbSqlSessionFactory.getDatabaseType();return "org/activiti/db/" + directory + "/activiti." + databaseType + "." + operation + "." + component + ".sql";}
@Test
public void initCamundaTable() {//默认会使用classpath下的camunda.cfg.xml或activiti.cfg.xml创建processEngineProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();System.out.println(processEngine);
}
如果配置文件不是:camunda.cfg.xml或activiti.cfg.xml
可以手动指定配置文件:
@Test
public void initCamundaTableUseSpecficConfigFile() {// 指定配置文件ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("custom.cfg.xml");ProcessEngine processEngine = configuration.buildProcessEngine();System.out.println(processEngine);
}
运行之后,自动创建了相关表:
手动部署流程
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.io.InputStream;
import java.util.zip.ZipInputStream;public class CamundaDeploymentTest {private static RepositoryService repositoryService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();repositoryService = processEngine.getRepositoryService();}@Testpublic void deploy() {Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpmn/javaservice.bpmn") // 添加bpmn资源.addClasspathResource("bpmn/javaservice.png") // 添加png资源.name("javaservice").deploy();System.out.println("流程部署id:" + deployment.getId());System.out.println("流程部署名称:" + deployment.getName());}@Testpublic void deployZip() {// 可以直接部署压缩包资源InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/javaservice.zip");assert inputStream != null;ZipInputStream zipInputStream = new ZipInputStream(inputStream);Deployment deployment = repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy();System.out.println("流程部署id:" + deployment.getId());System.out.println("流程部署名称:" + deployment.getName());}
}
查询流程
import org.apache.commons.io.IOUtils;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class CamundaDeploymentQueryTest {private static RepositoryService repositoryService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();repositoryService = processEngine.getRepositoryService();}/*** 查询流程定义*/@Testpublic void queryProcessDefinition() {ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();List<ProcessDefinition> definitionList = processDefinitionQuery.processDefinitionKey("Process_1ynfu43").orderByProcessDefinitionVersion().desc().list();for (ProcessDefinition processDefinition : definitionList) {System.out.println("流程定义 id=" + processDefinition.getId());System.out.println("流程定义 name=" + processDefinition.getName());System.out.println("流程定义 key=" + processDefinition.getKey());System.out.println("流程定义 Version=" + processDefinition.getVersion());System.out.println("流程部署ID =" + processDefinition.getDeploymentId());}}/*** 查找流程对应的只有* @throws IOException IO异常*/@Testpublic void queryBpmnResources() throws IOException {ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("Process_1ynfu43").singleResult();String deploymentId = processDefinition.getDeploymentId();try (FileOutputStream bpmnOut = new FileOutputStream("E:\\tmp\\jsv.bpmn");FileOutputStream pngOut = new FileOutputStream("E:\\tmp\\jsv.png");InputStream pngInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());) {IOUtils.copy(pngInput, pngOut);IOUtils.copy(bpmnInput, bpmnOut);}}
}
启动流程实例
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.util.HashMap;public class CamundaProcessTest {private static RuntimeService runtimeService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();runtimeService = processEngine.getRuntimeService();}/*** 启动流程实例*/@Testpublic void startProcess() {HashMap<String, Object> params = new HashMap<>();params.put("number",101);ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_1ynfu43",params);System.out.println("流程定义id:" + processInstance.getProcessDefinitionId());System.out.println("流程实例id:" + processInstance.getId());}}
完成任务
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.task.Task;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.util.HashMap;
import java.util.List;public class CamundaTaskTest {private static TaskService taskService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();taskService = processEngine.getTaskService();}/*** 查询当前个人待执行的任务*/@Testpublic void taskList() {String assignee = "allen";
// 根据流程key 和 任务负责人 查询任务List<Task> list = taskService.createTaskQuery().processDefinitionKey("Process_1ynfu43") //流程Key.taskAssignee(assignee)//只查询该任务负责人的任务.list();for (Task task : list) {System.out.println("流程实例id:" + task.getProcessInstanceId());System.out.println("任务id:" + task.getId());System.out.println("任务负责人:" + task.getAssignee());System.out.println("任务名称:" + task.getName());}}// 完成任务@Testpublic void completTask() {Task task = taskService.createTaskQuery().processDefinitionKey("Process_1ynfu43") //流程Key.taskAssignee("allen") //要查询的负责人.singleResult();
// 完成任务,参数:任务idHashMap<String, Object> params = new HashMap<>();params.put("approved",true);taskService.complete(task.getId(),params);}
}
删除流程定义
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.RepositoryService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;public class CamundaDeleteTest {private static RepositoryService repositoryService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();repositoryService = processEngine.getRepositoryService();}@Testpublic void deleteDeployment() {// 流程部署idString deploymentId = "1";//删除流程定义,如果该流程定义已有流程实例启动则删除不能删除repositoryService.deleteDeployment(deploymentId);}@Testpublic void deleteDeploymentCascade() {// 流程部署idString deploymentId = "1";// 级联删除流程定义,即使该流程有流程实例启动也可以删除repositoryService.deleteDeployment(deploymentId, true);}
}
查找历史节点信息
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricActivityInstanceQuery;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.util.List;public class CamundaHistoryTest {private static HistoryService historyService;@BeforeAllpublic static void beforeAll() {ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();historyService = processEngine.getHistoryService();}/*** 查看历史信息*/@Testpublic void findHistoryInfo() {HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();instanceQuery.processDefinitionId("pxxxxx");instanceQuery.orderByHistoricActivityInstanceStartTime().asc();List<HistoricActivityInstance> activityInstanceList = instanceQuery.list();for (HistoricActivityInstance hi : activityInstanceList) {System.out.println(hi.getActivityId());System.out.println(hi.getActivityName());System.out.println(hi.getProcessDefinitionId());System.out.println(hi.getProcessInstanceId());System.out.println("<==========================>");}}
}