官网文档:https://www.activiti.org/userguide/#queryAPI

     activiti的官方文档讲解详细很详细,也很范。按着文档写完了一个简单的demo发现,现实中的大多数问题,还是没法很好的解决。

例如:首先我需要知道的是,activiti的有那些表,及各个表的作用。这个网上有人罗列过,但总是觉得不通透。

所以,我先简单看了一下activiti数据处理的源码。

  • RepositoryServiceImpl

进行对那个操作的封装,传递Command接口的对应子类,里面封装了具体的操作

  1. public Deployment deploy(DeploymentBuilderImpl deploymentBuilder) {
  2. return commandExecutor.execute(new DeployCmd<Deployment>(deploymentBuilder));
  3. }
  • CommandInvoker

commandExecutor.execute() 这个方法的执行本质,是里面的CommandInterceptor执行链式的execute,而实质执行的是传进来的CMD接口类的execute方法。具体如下所示。

  1. public <T> T execute(final CommandConfig config, final Command<T> command) {
  2. final CommandContext commandContext = Context.getCommandContext();
  3. //省略一些代码
  4. commandContext.getAgenda().planOperation(new Runnable() {
  5. @Override
  6. public void run() {
  7. commandContext.setResult(command.execute(commandContext));//这里是关键
  8. }
  9. });
  10. // 省略一些代码。。。。。。。。。。。。
  11. return (T) commandContext.getResult();
  12. }
  • DeployCmd(从这里调用DataManager的实现类进行相关数据库操作)
  1. public Deployment execute(CommandContext commandContext) {
  2. // Backwards compatibility with Activiti v5
  3. if (commandContext.getProcessEngineConfiguration().isActiviti5CompatibilityEnabled()
  4. && deploymentBuilder.getDeploymentProperties() != null
  5. && deploymentBuilder.getDeploymentProperties().containsKey(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION)
  6. && deploymentBuilder.getDeploymentProperties().get(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION).equals(Boolean.TRUE)) {
  7. return deployAsActiviti5ProcessDefinition(commandContext);
  8. }
  9. return executeDeploy(commandContext);
  10. }
  • DeploymentEntityManagerImpl

实际数据库操作的是DataManager的实现类,进行数据库操作。

这个我们能看出来是有两个

  1. @Override
  2. public void insert(DeploymentEntity deployment) {
  3. insert(deployment, false);
  4. for (ResourceEntity resource : deployment.getResources().values()) {
  5. resource.setDeploymentId(deployment.getId());
  6. getResourceEntityManager().insert(resource);
  7. }
  8. }
  • 对应的xml操作

在mapping目录下可以查找到对应的操作。前面的${prefix}标识数据库,activiti已经帮我们判断好了,不需要我们再传入。

  1. <insert id="insertDeployment" parameterType="org.activiti.engine.impl.persistence.entity.DeploymentEntityImpl">
  2. insert into ${prefix}ACT_RE_DEPLOYMENT(ID_, NAME_, CATEGORY_, KEY_, TENANT_ID_, DEPLOY_TIME_, ENGINE_VERSION_)
  3. values(#{id, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{category, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{tenantId, jdbcType=VARCHAR}, #{deploymentTime, jdbcType=TIMESTAMP}, #{engineVersion, jdbcType=VARCHAR})
  4. </insert>
  5. <insert id="insertResource" parameterType="org.activiti.engine.impl.persistence.entity.ResourceEntityImpl">
  6. insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_)
  7. values (#{id, jdbcType=VARCHAR}, 1, #{name, jdbcType=VARCHAR}, #{bytes, jdbcType=${blobType}}, #{deploymentId, jdbcType=VARCHAR}, #{generated, jdbcType=BOOLEAN})
  8. </insert>
  • DataManager

数据查询操作本质都是通过DataManger的实现类MybatisDeploymentDataManager进行操作;

  1. @Override
  2. public List<Deployment> executeList(CommandContext commandContext, Page page) {
  3. checkQueryOk();
  4. return commandContext.getDeploymentEntityManager().findDeploymentsByQueryCriteria(this, page);
  5. }
  6. @Override
  7. @SuppressWarnings("unchecked")
  8. public List<Deployment> findDeploymentsByQueryCriteria(DeploymentQueryImpl deploymentQuery, Page page) {
  9. final String query = "selectDeploymentsByQueryCriteria";
  10. return getDbSqlSession().selectList(query, deploymentQuery, page);
  11. }

  • 查找xml中对应的sql
  1. <select id="selectDeploymentsByQueryCriteria" parameterType="org.activiti.engine.impl.DeploymentQueryImpl" resultMap="deploymentResultMap">
  2. ${limitBefore}
  3. select distinct RES.* ${limitBetween}
  4. <include refid="selectDeploymentsByQueryCriteriaSql"/>
  5. ${orderBy}
  6. ${limitAfter}
  7. </select>

需要了解任务类型,以及网关相关知识。

  • TaskService
  1. taskService.complete(task.getId(), taskVariables);
  • AbstractCompleteTaskCmd
  1. protected void executeTaskComplete(CommandContext commandContext, TaskEntity taskEntity, Map<String, Object> variables, boolean localScope) {
  2. // Task complete logic
  3. if (taskEntity.getDelegationState() != null && taskEntity.getDelegationState().equals(DelegationState.PENDING)) {
  4. throw new ActivitiException("A delegated task cannot be completed, but should be resolved instead.");
  5. }
  6. //执行节点监听事件
  7. commandContext.getProcessEngineConfiguration().getListenerNotificationHelper().executeTaskListeners(taskEntity, TaskListener.EVENTNAME_COMPLETE);
  8. if (Authentication.getAuthenticatedUserId() != null && taskEntity.getProcessInstanceId() != null) {
  9. //查找任务select * from ${prefix}ACT_RU_EXECUTION
  10. // where ROOT_PROC_INST_ID_ = (select ROOT_PROC_INST_ID_ from ${prefix}ACT_RU_EXECUTION where ID_ = #{parameter})
  11. ExecutionEntity processInstanceEntity = commandContext.getExecutionEntityManager().findById(taskEntity.getProcessInstanceId());
  12. //任务和identity绑定
  13. commandContext.getIdentityLinkEntityManager().involveUser(processInstanceEntity, Authentication.getAuthenticatedUserId(),IdentityLinkType.PARTICIPANT);
  14. }
  15. ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
  16. if (eventDispatcher.isEnabled()) {
  17. if (variables != null) {
  18. eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityWithVariablesEvent(ActivitiEventType.TASK_COMPLETED, taskEntity, variables, localScope));
  19. } else {
  20. eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_COMPLETED, taskEntity));
  21. }
  22. }
  23. //删除已有的任务相关数据
  24. commandContext.getTaskEntityManager().deleteTask(taskEntity, null, false, false);
  25. // Continue process (if not a standalone task) 激活下个步骤工作
  26. if (taskEntity.getExecutionId() != null) {
  27. ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(taskEntity.getExecutionId());
  28. Context.getAgenda().planTriggerExecutionOperation(executionEntity);
  29. }
  30. }

ManagementService

  1. managementService.getTableName()

TableDataManagerImpl

  1. static {
  2. // runtime
  3. entityToTableNameMap.put(TaskEntity.class, "ACT_RU_TASK");
  4. entityToTableNameMap.put(ExecutionEntity.class, "ACT_RU_EXECUTION");
  5. entityToTableNameMap.put(IdentityLinkEntity.class, "ACT_RU_IDENTITYLINK");
  6. entityToTableNameMap.put(VariableInstanceEntity.class, "ACT_RU_VARIABLE");
  7. entityToTableNameMap.put(JobEntity.class, "ACT_RU_JOB");
  8. entityToTableNameMap.put(TimerJobEntity.class, "ACT_RU_TIMER_JOB");
  9. entityToTableNameMap.put(SuspendedJobEntity.class, "ACT_RU_SUSPENDED_JOB");
  10. entityToTableNameMap.put(DeadLetterJobEntity.class, "ACT_RU_DEADLETTER_JOB");
  11. entityToTableNameMap.put(EventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
  12. entityToTableNameMap.put(CompensateEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
  13. entityToTableNameMap.put(MessageEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
  14. entityToTableNameMap.put(SignalEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
  15. // repository
  16. entityToTableNameMap.put(DeploymentEntity.class, "ACT_RE_DEPLOYMENT");
  17. entityToTableNameMap.put(ProcessDefinitionEntity.class, "ACT_RE_PROCDEF");
  18. entityToTableNameMap.put(ModelEntity.class, "ACT_RE_MODEL");
  19. entityToTableNameMap.put(ProcessDefinitionInfoEntity.class, "ACT_PROCDEF_INFO");
  20. // history
  21. entityToTableNameMap.put(CommentEntity.class, "ACT_HI_COMMENT");
  22. entityToTableNameMap.put(HistoricActivityInstanceEntity.class, "ACT_HI_ACTINST");
  23. entityToTableNameMap.put(AttachmentEntity.class, "ACT_HI_ATTACHMENT");
  24. entityToTableNameMap.put(HistoricProcessInstanceEntity.class, "ACT_HI_PROCINST");
  25. entityToTableNameMap.put(HistoricVariableInstanceEntity.class, "ACT_HI_VARINST");
  26. entityToTableNameMap.put(HistoricTaskInstanceEntity.class, "ACT_HI_TASKINST");
  27. entityToTableNameMap.put(HistoricIdentityLinkEntity.class, "ACT_HI_IDENTITYLINK");
  28. // a couple of stuff goes to the same table
  29. entityToTableNameMap.put(HistoricDetailAssignmentEntity.class, "ACT_HI_DETAIL");
  30. entityToTableNameMap.put(HistoricDetailTransitionInstanceEntity.class, "ACT_HI_DETAIL");
  31. entityToTableNameMap.put(HistoricFormPropertyEntity.class, "ACT_HI_DETAIL");
  32. entityToTableNameMap.put(HistoricDetailVariableInstanceUpdateEntity.class, "ACT_HI_DETAIL");
  33. entityToTableNameMap.put(HistoricDetailEntity.class, "ACT_HI_DETAIL");
  34. // Identity module
  35. entityToTableNameMap.put(GroupEntity.class, "ACT_ID_GROUP");
  36. entityToTableNameMap.put(MembershipEntity.class, "ACT_ID_MEMBERSHIP");
  37. entityToTableNameMap.put(UserEntity.class, "ACT_ID_USER");
  38. entityToTableNameMap.put(IdentityInfoEntity.class, "ACT_ID_INFO");
  39. // general
  40. entityToTableNameMap.put(PropertyEntity.class, "ACT_GE_PROPERTY");
  41. entityToTableNameMap.put(ByteArrayEntity.class, "ACT_GE_BYTEARRAY");
  42. entityToTableNameMap.put(ResourceEntity.class, "ACT_GE_BYTEARRAY");
  43. entityToTableNameMap.put(EventLogEntryEntity.class, "ACT_EVT_LOG");
  44. // and now the map for the API types (does not cover all cases)
  45. apiTypeToTableNameMap.put(Task.class, "ACT_RU_TASK");
  46. apiTypeToTableNameMap.put(Execution.class, "ACT_RU_EXECUTION");
  47. apiTypeToTableNameMap.put(ProcessInstance.class, "ACT_RU_EXECUTION");
  48. apiTypeToTableNameMap.put(ProcessDefinition.class, "ACT_RE_PROCDEF");
  49. apiTypeToTableNameMap.put(Deployment.class, "ACT_RE_DEPLOYMENT");
  50. apiTypeToTableNameMap.put(Job.class, "ACT_RU_JOB");
  51. apiTypeToTableNameMap.put(Model.class, "ACT_RE_MODEL");
  52. // history
  53. apiTypeToTableNameMap.put(HistoricProcessInstance.class, "ACT_HI_PROCINST");
  54. apiTypeToTableNameMap.put(HistoricActivityInstance.class, "ACT_HI_ACTINST");
  55. apiTypeToTableNameMap.put(HistoricDetail.class, "ACT_HI_DETAIL");
  56. apiTypeToTableNameMap.put(HistoricVariableUpdate.class, "ACT_HI_DETAIL");
  57. apiTypeToTableNameMap.put(HistoricFormProperty.class, "ACT_HI_DETAIL");
  58. apiTypeToTableNameMap.put(HistoricTaskInstance.class, "ACT_HI_TASKINST");
  59. apiTypeToTableNameMap.put(HistoricVariableInstance.class, "ACT_HI_VARINST");
  60. // identity
  61. apiTypeToTableNameMap.put(Group.class, "ACT_ID_GROUP");
  62. apiTypeToTableNameMap.put(User.class, "ACT_ID_USER");
  63. // TODO: Identity skipped for the moment as no SQL injection is provided
  64. // here
  65. }

以Task接口为例

可以看到接口的实现类对应xml的命名空间

官方文档中的说明

  1. Activiti的数据库名称都以ACT_开头。第二部分是表用例的两个字符的标识。该用例也将与服务API大致匹配。
  2. ACT_RE_ *:RE代表repository。具有此前缀的表包含静态信息,例如流程定义和流程资源(图像,规则等)。
  3. ACT_RU_ *:RU代表runtime。这些是运行时表,其中包含流程实例,用户任务,变量,作业等的运行时数据。Activiti仅在流程实例执行期间存储运行时数据,并在流程实例结束时删除记录。这样可以使运行时表较小而又快速。
  4. ACT_ID_ *:ID代表identity。这些表包含身份信息,例如用户,组等。
  5. ACT_HI_ *:HI代表history。这些表包含历史数据,例如过去的流程实例,变量,任务等。
  6. ACT_GE_ *:general数据,用于各种用例中

初次使用中使用到的表格(后续继续补充)

类型 表格名称 存储的数据 备注
生成 ACT_GE_BYTEARRAY 存储部署的bpmn相关文件
re ACT_RE_DEPLOYMENT 发布的流程数据
re act_re_procdef 存储流程分解数据
runtime ACT_RU_TASK 正在运行的任务
runtime ACT_RU_VARIABLE 用于存储流程流转中的字段 PROC_INST_ID_ 对应ACT_RU_TASK中的EXECUTION_ID_
TASK_ID_ 对应 ACT_RU_TASK中的ID_
runtime ACT_RU_EXECUTION 运行时流程执行实例
history act_hi_taskinst 流程历史步骤数据
history act_hi_variable 历史步骤流程中转字段
history act_hi_procinst 已经发起的流程实例 已经结束的流程任务END_ACT_ID_不为空

版权声明:本文为perferect原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/perferect/p/13234784.html