activiti6基礎01-如何數據庫操作及相關表
官網文檔://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方法。具體如下所示。
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);
}
- 查找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_不為空 |