一、项目中Camunda的使用

基本依赖请看另一篇文章
camunda学习使用

介绍

开始事件

开始事件

结束事件

结束事件

网关

网关

顺序流

顺序流

任务

任务

用户任务

用户任务

活动

活动
活动
上面是项目中使用到的一些图形,简单介绍一下

项目集成

依赖

<spring-boot.version>2.5.6</spring-boot.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
<hibernate-types-52.version>2.14.0</hibernate-types-52.version>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
    <groupId>org.camunda.bpm.springboot</groupId>
    <artifactId>camunda-bpm-spring-boot-starter</artifactId>
    <version>7.17.0-alpha1</version>
</dependency>
<dependency>
    <groupId>org.camunda.bpm</groupId>
    <artifactId>camunda-engine-cdi</artifactId>
    <version>7.17.0-alpha1</version>
</dependency>

 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- hibernate支持配置 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
</dependency>
<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>${hibernate-types-52.version}</version>
</dependency>
<!-- hibernate支持配置 -->
 <dependency>
    <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
</dependency>

配置文件

spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
#该选项用来验证数据库连接的有效性。如果您的驱动程序支持JDBC4,我们强烈建议您不要设置此属性
spring.datasource.hikari.connection-test-query=SELECT 1
#池中最大连接数,包括闲置和使用中的连接
spring.datasource.hikari.maximum-pool-size=100
#自动提交从池中返回的连接
spring.datasource.hikari.auto-commit=true
#池中维护的最小空闲连接数
spring.datasource.hikari.minimum-idle=10
#连接允许在池中闲置的最长时间
spring.datasource.hikari.idle-timeout=30000
#连接池的用户定义名称,主要出现在日志记录和JMX管理控制台中以识别池和池配置
spring.datasource.hikari.pool-name=DatebookHikariCP
#池中连接最长生命周期
spring.datasource.hikari.max-lifetime=1800000
#等待来自池的连接的最大毫秒数
spring.datasource.hikari.connection-timeout=30000

spring.jpa.database=POSTGRESQL
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
#spring.jpa.properties.hibernate.current_session_context_class=thread
spring.jpa.properties.hibernate.hbm2ddl.auto=none
#spring.jpa.properties.hibernate.default_schema=mss
spring.jpa.properties.hibernate.dialect=com.xysd.bizbase.infrastructure.dialect.AdvancedPostgreSQLDialect
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.jdbc.batch_size=500
spring.jpa.properties.hibernate.default_batch_fetch_size=160
spring.jpa.properties.hibernate.max_fetch_depth=3
spring.jpa.properties.hibernate.batch_fetch_style=DYNAMIC
spring.jpa.properties.hibernate.jdbc.fetch_size=500
spring.jpa.properties.hibernate.cache.use_second_level_cache=false
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=false
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.query.substitutions=true 1,false 0
#避免KingBase报createClob 错误
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#字段映射规则:无修改命名
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#字段映射规则:遇到大写字母 加”_”的命名   默认
#spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

表结构

表结构

重要的几个表

-- 流程实例
select id_,root_proc_inst_id_,proc_inst_id_,business_key_,parent_id_,proc_def_id_,act_inst_id_
from act_ru_execution
where business_key_ in ('sxfbjk_1cd3840d7cad4f93958c967145c37de0_001-004-002-001');

-- 流程发布事件
select id_,event_type_,event_name_,execution_id_,proc_inst_id_,activity_id_,created_
from act_ru_event_subscr
where proc_inst_id_ in ('1062fe2f-a2d9-11ee-87bd-525400011421')
order by created_ desc offset 0 limit 10;

-- 流程变量存储
select id_,type_,name_,execution_id_,proc_inst_id_,proc_def_id_,text_,text2_
from act_ru_variable
where proc_inst_id_ in ('1062fe2f-a2d9-11ee-87bd-525400011421');

业务中可能使用到的一些类介绍

DelegateExecution

Camunda BPM 是一个开源的工作流引擎,它允许开发人员以声明式方式定义业务流程,并通过 Java 委托来处理业务逻辑。在 Camunda BPM 中,DelegateExecution 是一个接口,它代表了流程实例或某个活动实例的执行上下文。当业务逻辑代码(即 Java Delegate)被流程引擎调用时,DelegateExecution 会作为参数传递给这些代码,从而允许开发人员访问和操作当前执行的流程或活动的上下文。

DelegateExecution 接口提供了一系列方法来访问和操作当前的执行上下文,例如:

  • getVariables() 和 getVariable(String name):用于获取流程变量。
  • setVariables(Map<String, Object> variables) 和 setVariable(String name, Object value):用于设置流程变量。
  • getVariableLocal(String name) 和 setVariableLocal(String name, Object value):用于获取和设置局部流程变量(即只在当前执行上下文中可见)。
  • getProcessInstanceId():用于获取当前执行的流程实例的 ID。
  • getExecutionId():用于获取当前执行的唯一 ID。
  • getActivityId():用于获取当前执行的活动(任务或网关)的 ID。
  • getEventName():用于获取触发当前执行的事件名称。
  • getBpmnModelInstance():用于获取 BPMN 模型的实例,从而可以访问流程定义的所有信息。

除了上述方法外,DelegateExecution 还提供了其他一些用于操作流程实例或活动实例的方法,例如 signalEventReceived(String eventName)、signalEventReceived(String eventName, Map<String, Object> variables) 等。

需要注意的是,DelegateExecution 的具体实现是由 Camunda BPM 引擎提供的,开发人员通常不需要直接与实现类交互。在编写 Java Delegate 时,只需使用 DelegateExecution 接口即可。

RuntimeService

在Camunda BPM中,RuntimeService 是一个核心的服务接口,它提供了运行时流程实例的管理和操作功能。通过RuntimeService,你可以启动新的流程实例,查询正在运行的流程实例,以及操作这些实例。

以下是RuntimeService中一些常用的方法:

  • startProcessInstanceByKey(String processDefinitionKey, Map<String, Object> variables): 使用给定的流程定义键(key)和变量启动一个新的流程实例。流程定义键通常对应BPMN文件中的id属性。
  • startProcessInstanceById(String processDefinitionId, Map<String, Object> variables): 使用给定的流程定义ID和变量启动一个新的流程实例。
  • createProcessInstanceQuery(): 创建一个新的流程实例查询,允许你基于各种条件(如流程定义键、流程实例ID、状态等)来检索正在运行的流程实例。
  • deleteProcessInstance(String processInstanceId, String deleteReason): 删除指定的流程实例,并可以提供一个删除原因。
  • signalEventReceived(String eventName, String executionId): 向指定的执行(execution)发送一个信号事件。这通常用于触发流程中的边界事件。
  • signalEventReceived(String eventName, String executionId, Map<String, Object> variables): 与上一个方法类似,但允许你传递一些变量给事件。
  • setVariable(String executionId, String variableName, Object value): 在指定的执行上设置一个变量。
  • getVariable(String executionId, String variableName): 从指定的执行中获取一个变量的值。
  • setVariables(String executionId, Map<String, ?> variables): 在指定的执行上设置多个变量。
  • getVariables(String executionId): 从指定的执行中获取所有变量的值。

请注意,RuntimeService只处理正在运行的流程实例。如果你需要处理历史的流程实例数据(例如,查询已完成的流程实例),你应该使用HistoryService。同样,如果你需要部署新的流程定义或管理已有的流程定义,你应该使用RepositoryService。TaskService则用于处理与用户任务相关的操作。

TaskService

在Camunda BPM中,TaskService 是一个核心的服务接口,它提供了对用户任务(User Tasks)的管理和操作功能。用户任务通常是流程定义中需要人工干预的步骤,例如审批、填写表单等。通过TaskService,可以查询任务、完成任务、创建任务、委托任务等操作。

以下是TaskService中一些常用的方法:

  • createTaskQuery(): 创建一个新的任务查询,允许你基于各种条件(如任务ID、流程实例ID、候选人、状态等)来检索用户任务。
  • assignTask(String taskId, String userId): 将指定的任务分配给指定的用户。
  • claim(String taskId, String userId): 将任务声明为由指定用户所有,通常用于将任务从任务池中取出。
  • complete(String taskId): 完成指定的任务。这通常意味着用户已经处理了任务中的所有操作,并且任务可以从流程实例中移除。
  • setVariable(String taskId, String variableName, Object value): 在指定的任务上设置一个变量。这些变量可以在任务的Java委托中访问。
  • getVariable(String taskId, String variableName): 从指定的任务中获取一个变量的值。
  • setVariables(String taskId, Map<String, ?> variables): 在指定的任务上设置多个变量。
  • getVariables(String taskId): 从指定的任务中获取所有变量的值。
  • delegateTask(String taskId, String candidateUserId): 将任务委托给另一个用户。这意味着任务将从当前用户的任务列表中移除,并添加到被委托用户的任务列表中。
  • createTask(TaskDto taskDto): 根据提供的TaskDto对象创建一个新的任务。这通常用于创建非流程定义中的自定义任务。

一旦你有了TaskService的实例,你就可以使用它来管理你的用户任务了。这些任务通常是流程实例中需要人工干预的部分,通过TaskService,你可以将任务分配给特定的用户,完成任务,或者查询任务的状态和相关信息。

请注意,TaskService只处理用户任务,而不处理流程实例本身。如果你需要处理流程实例的启动、暂停、删除等操作,你应该使用RuntimeService。如果你需要部署、更新或查询流程定义,你应该使用RepositoryService。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值