activiti6基礎01-如何資料庫操作及相關表

wuzhiaite發表於2020-07-06

官網文件:https://www.activiti.org/userguide/#queryAPI

1. Activit的簡單原始碼解讀

     activiti的官方文件講解詳細很詳細,也很範。按著文件寫完了一個簡單的demo發現,現實中的大多數問題,還是沒法很好的解決。

例如:首先我需要知道的是,activiti的有那些表,及各個表的作用。這個網上有人羅列過,但總是覺得不通透。

所以,我先簡單看了一下activiti資料處理的原始碼。

1.1 流程釋出

  • RepositoryServiceImpl

進行對那個操作的封裝,傳遞Command介面的對應子類,裡面封裝了具體的操作

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

commandExecutor.execute() 這個方法的執行本質,是裡面的CommandInterceptor執行鏈式的execute,而實質執行的是傳進來的CMD介面類的execute方法。具體如下所示。
activiti6基礎01-如何資料庫操作及相關表

 public <T> T execute(final CommandConfig config, final Command<T> command) {
    final CommandContext commandContext = Context.getCommandContext();

    //省略一些程式碼

    commandContext.getAgenda().planOperation(new Runnable() {
      @Override
      public void run() {
        commandContext.setResult(command.execute(commandContext));//這裡是關鍵
      }
    });

     // 省略一些程式碼。。。。。。。。。。。。

    return (T) commandContext.getResult();
  }

  • DeployCmd(從這裡呼叫DataManager的實現類進行相關資料庫操作)
public Deployment execute(CommandContext commandContext) {

    // Backwards compatibility with Activiti v5
    if (commandContext.getProcessEngineConfiguration().isActiviti5CompatibilityEnabled()
        && deploymentBuilder.getDeploymentProperties() != null 
        && deploymentBuilder.getDeploymentProperties().containsKey(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION)
        && deploymentBuilder.getDeploymentProperties().get(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION).equals(Boolean.TRUE)) {
      
        return deployAsActiviti5ProcessDefinition(commandContext);
    }

    return executeDeploy(commandContext);
  }


  • DeploymentEntityManagerImpl

實際資料庫操作的是DataManager的實現類,進行資料庫操作。

這個我們能看出來是有兩個


 @Override
  public void insert(DeploymentEntity deployment) {
    insert(deployment, false);

    for (ResourceEntity resource : deployment.getResources().values()) {
      resource.setDeploymentId(deployment.getId());
      getResourceEntityManager().insert(resource);
    }
  }
  • 對應的xml操作

在mapping目錄下可以查詢到對應的操作。前面的${prefix}標識資料庫,activiti已經幫我們判斷好了,不需要我們再傳入。

  <insert id="insertDeployment" parameterType="org.activiti.engine.impl.persistence.entity.DeploymentEntityImpl">
    insert into ${prefix}ACT_RE_DEPLOYMENT(ID_, NAME_, CATEGORY_, KEY_, TENANT_ID_, DEPLOY_TIME_, ENGINE_VERSION_)
    values(#{id, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{category, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{tenantId, jdbcType=VARCHAR}, #{deploymentTime, jdbcType=TIMESTAMP}, #{engineVersion, jdbcType=VARCHAR})
  </insert>

<insert id="insertResource" parameterType="org.activiti.engine.impl.persistence.entity.ResourceEntityImpl">
    insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_)
    values (#{id, jdbcType=VARCHAR}, 1, #{name, jdbcType=VARCHAR}, #{bytes, jdbcType=${blobType}}, #{deploymentId, jdbcType=VARCHAR}, #{generated, jdbcType=BOOLEAN})  
  </insert>


1.2 資料查詢

  • DataManager

資料查詢操作本質都是通過DataManger的實現類MybatisDeploymentDataManager進行操作;

  @Override
  public List<Deployment> executeList(CommandContext commandContext, Page page) {
    checkQueryOk();
    return commandContext.getDeploymentEntityManager().findDeploymentsByQueryCriteria(this, page);
  }

  @Override
  @SuppressWarnings("unchecked")
  public List<Deployment> findDeploymentsByQueryCriteria(DeploymentQueryImpl deploymentQuery, Page page) {
    final String query = "selectDeploymentsByQueryCriteria";
    return getDbSqlSession().selectList(query, deploymentQuery, page);
  }    
activiti6基礎01-如何資料庫操作及相關表
  • 查詢xml中對應的sql
  <select id="selectDeploymentsByQueryCriteria" parameterType="org.activiti.engine.impl.DeploymentQueryImpl" resultMap="deploymentResultMap">
  	${limitBefore}
    select distinct RES.* ${limitBetween}
    <include refid="selectDeploymentsByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

1.3 任務執行

需要了解任務型別,以及閘道器相關知識。

  • TaskService
taskService.complete(task.getId(), taskVariables);
  • AbstractCompleteTaskCmd
protected void executeTaskComplete(CommandContext commandContext, TaskEntity taskEntity, Map<String, Object> variables, boolean localScope) {
    // Task complete logic

    if (taskEntity.getDelegationState() != null && taskEntity.getDelegationState().equals(DelegationState.PENDING)) {
      throw new ActivitiException("A delegated task cannot be completed, but should be resolved instead.");
    }
    //執行節點監聽事件  
    commandContext.getProcessEngineConfiguration().getListenerNotificationHelper().executeTaskListeners(taskEntity, TaskListener.EVENTNAME_COMPLETE);
    if (Authentication.getAuthenticatedUserId() != null && taskEntity.getProcessInstanceId() != null) {
       //查詢任務select * from ${prefix}ACT_RU_EXECUTION
       // where ROOT_PROC_INST_ID_ = (select ROOT_PROC_INST_ID_ from ${prefix}ACT_RU_EXECUTION where ID_ = #{parameter})
      ExecutionEntity processInstanceEntity = commandContext.getExecutionEntityManager().findById(taskEntity.getProcessInstanceId());
      //任務和identity繫結
      commandContext.getIdentityLinkEntityManager().involveUser(processInstanceEntity, Authentication.getAuthenticatedUserId(),IdentityLinkType.PARTICIPANT);
    }

    ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
    if (eventDispatcher.isEnabled()) {
      if (variables != null) {
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityWithVariablesEvent(ActivitiEventType.TASK_COMPLETED, taskEntity, variables, localScope));
      } else {
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_COMPLETED, taskEntity));
      }
    }
      //刪除已有的任務相關資料
    commandContext.getTaskEntityManager().deleteTask(taskEntity, null, false, false);

    // Continue process (if not a standalone task) 啟用下個步驟工作
    if (taskEntity.getExecutionId() != null) {
      ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(taskEntity.getExecutionId());
      Context.getAgenda().planTriggerExecutionOperation(executionEntity);
    }
  }

1.4 獲取類對應表名稱

ManagementService

  managementService.getTableName()

TableDataManagerImpl

static {
    // runtime
    entityToTableNameMap.put(TaskEntity.class, "ACT_RU_TASK");
    entityToTableNameMap.put(ExecutionEntity.class, "ACT_RU_EXECUTION");
    entityToTableNameMap.put(IdentityLinkEntity.class, "ACT_RU_IDENTITYLINK");
    entityToTableNameMap.put(VariableInstanceEntity.class, "ACT_RU_VARIABLE");

    entityToTableNameMap.put(JobEntity.class, "ACT_RU_JOB");
    entityToTableNameMap.put(TimerJobEntity.class, "ACT_RU_TIMER_JOB");
    entityToTableNameMap.put(SuspendedJobEntity.class, "ACT_RU_SUSPENDED_JOB");
    entityToTableNameMap.put(DeadLetterJobEntity.class, "ACT_RU_DEADLETTER_JOB");
    

    entityToTableNameMap.put(EventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
    entityToTableNameMap.put(CompensateEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
    entityToTableNameMap.put(MessageEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");
    entityToTableNameMap.put(SignalEventSubscriptionEntity.class, "ACT_RU_EVENT_SUBSCR");

    // repository
    entityToTableNameMap.put(DeploymentEntity.class, "ACT_RE_DEPLOYMENT");
    entityToTableNameMap.put(ProcessDefinitionEntity.class, "ACT_RE_PROCDEF");
    entityToTableNameMap.put(ModelEntity.class, "ACT_RE_MODEL");
    entityToTableNameMap.put(ProcessDefinitionInfoEntity.class, "ACT_PROCDEF_INFO");

    // history
    entityToTableNameMap.put(CommentEntity.class, "ACT_HI_COMMENT");

    entityToTableNameMap.put(HistoricActivityInstanceEntity.class, "ACT_HI_ACTINST");
    entityToTableNameMap.put(AttachmentEntity.class, "ACT_HI_ATTACHMENT");
    entityToTableNameMap.put(HistoricProcessInstanceEntity.class, "ACT_HI_PROCINST");
    entityToTableNameMap.put(HistoricVariableInstanceEntity.class, "ACT_HI_VARINST");
    entityToTableNameMap.put(HistoricTaskInstanceEntity.class, "ACT_HI_TASKINST");
    entityToTableNameMap.put(HistoricIdentityLinkEntity.class, "ACT_HI_IDENTITYLINK");

    // a couple of stuff goes to the same table
    entityToTableNameMap.put(HistoricDetailAssignmentEntity.class, "ACT_HI_DETAIL");
    entityToTableNameMap.put(HistoricDetailTransitionInstanceEntity.class, "ACT_HI_DETAIL");
    entityToTableNameMap.put(HistoricFormPropertyEntity.class, "ACT_HI_DETAIL");
    entityToTableNameMap.put(HistoricDetailVariableInstanceUpdateEntity.class, "ACT_HI_DETAIL");
    entityToTableNameMap.put(HistoricDetailEntity.class, "ACT_HI_DETAIL");

    // Identity module
    entityToTableNameMap.put(GroupEntity.class, "ACT_ID_GROUP");
    entityToTableNameMap.put(MembershipEntity.class, "ACT_ID_MEMBERSHIP");
    entityToTableNameMap.put(UserEntity.class, "ACT_ID_USER");
    entityToTableNameMap.put(IdentityInfoEntity.class, "ACT_ID_INFO");

    // general
    entityToTableNameMap.put(PropertyEntity.class, "ACT_GE_PROPERTY");
    entityToTableNameMap.put(ByteArrayEntity.class, "ACT_GE_BYTEARRAY");
    entityToTableNameMap.put(ResourceEntity.class, "ACT_GE_BYTEARRAY");
    
    entityToTableNameMap.put(EventLogEntryEntity.class, "ACT_EVT_LOG");

    // and now the map for the API types (does not cover all cases)
    apiTypeToTableNameMap.put(Task.class, "ACT_RU_TASK");
    apiTypeToTableNameMap.put(Execution.class, "ACT_RU_EXECUTION");
    apiTypeToTableNameMap.put(ProcessInstance.class, "ACT_RU_EXECUTION");
    apiTypeToTableNameMap.put(ProcessDefinition.class, "ACT_RE_PROCDEF");
    apiTypeToTableNameMap.put(Deployment.class, "ACT_RE_DEPLOYMENT");
    apiTypeToTableNameMap.put(Job.class, "ACT_RU_JOB");
    apiTypeToTableNameMap.put(Model.class, "ACT_RE_MODEL");

    // history
    apiTypeToTableNameMap.put(HistoricProcessInstance.class, "ACT_HI_PROCINST");
    apiTypeToTableNameMap.put(HistoricActivityInstance.class, "ACT_HI_ACTINST");
    apiTypeToTableNameMap.put(HistoricDetail.class, "ACT_HI_DETAIL");
    apiTypeToTableNameMap.put(HistoricVariableUpdate.class, "ACT_HI_DETAIL");
    apiTypeToTableNameMap.put(HistoricFormProperty.class, "ACT_HI_DETAIL");
    apiTypeToTableNameMap.put(HistoricTaskInstance.class, "ACT_HI_TASKINST");
    apiTypeToTableNameMap.put(HistoricVariableInstance.class, "ACT_HI_VARINST");

    // identity
    apiTypeToTableNameMap.put(Group.class, "ACT_ID_GROUP");
    apiTypeToTableNameMap.put(User.class, "ACT_ID_USER");

    // TODO: Identity skipped for the moment as no SQL injection is provided
    // here
  }

2.activiti表格及對應的資料

初次使用中使用到的表格(後續繼續補充)

型別 表格名稱 儲存的資料 備註
生成 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_不為空

相關文章