- TaskService.completeTask()的执行内幕是啥?
activiti采取了command模式,completeTask会被包装成一个CompleteTaskCmd,一个Cmd执行的时候需要一些外围处理,如:log日志。activiti定义了一个拦截器链,链上的每个拦截器都有个next,会一直next执行下去。以CompleteTaskCmd为例,拦截器链为:
logger拦截器-->spring事务拦截器-->CommandContext拦截器-->CommandInvoker拦截器
其中CommandContext拦截器的工作主要是设置Context:
class="java" name="code"> // Push on stack
Context.setCommandContext(context);
Context.setProcessEngineConfiguration(processEngineConfiguration);
return next.execute(config, command);
这边push,另外有地方pop,CommandInvoker就干的此事:
public <T> T execute(CommandConfig config, Command<T> command) {
return command.execute(Context.getCommandContext());
}
- 一个节点结束了,流程怎么知道往下走?
答案是TaskEntity.completeTask()方法会调用execution.signal()-->activityBehavior.signal()-->activityBehavior.leave()方法,该方法最终会激活AtomicOperationTransitionNotifyListenerStart的eventNotificationsCompleted()方法,该方法会创建当前Transition的destination,代码如下:
protected void eventNotificationsCompleted(InterpretableExecution execution) {
TransitionImpl transition = execution.getTransition();
ActivityImpl destination = null;
if(transition == null) { // this is null after async cont. -> transition is not stored in execution
destination = (ActivityImpl) execution.getActivity();
} else {
destination = transition.getDestination();
}
ActivityImpl activity = (ActivityImpl) execution.getActivity();
if (activity!=destination) {
ActivityImpl nextScope = AtomicOperationTransitionNotifyListenerTake.findNextScope(activity, destination);
execution.setActivity(nextScope);
execution.performOperation(TRANSITION_CREATE_SCOPE);
} else {
execution.setTransition(null);
execution.setActivity(destination);
execution.performOperation(ACTIVITY_EXECUTE);
}
}
}
-
多实例任务怎么知道该loop已结束?
多实例任务会启动多个任务和execution,调用execution.signal()-->activityBehavior.signal()-->activityBehavior.leave(),ParallelMultiInstanceBehavior.leave()其中包含如下代码:
List<ActivityExecution> joinedExecutions = executionEntity.findInactiveConcurrentExecutions(execution.getActivity());
if (joinedExecutions.size() == nrOfInstances || completionConditionSatisfied(execution)) {
// Removing all active child executions (ie because completionCondition is true)
List<ExecutionEntity> executionsToRemove = new ArrayList<ExecutionEntity>();
for (ActivityExecution childExecution : executionEntity.getParent().getExecutions()) {
if (childExecution.isActive()) {
executionsToRemove.add((ExecutionEntity) childExecution);
}
}
for (ExecutionEntity executionToRemove : executionsToRemove) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Execution {} still active, but multi-instance is completed. Removing this execution.", executionToRemove);
}
executionToRemove.inactivate();
executionToRemove.deleteCascade("multi-instance completed");
}
executionEntity.takeAll(executionEntity.getActivity().getOutgoingTransitions(), joinedExecutions);
completionConditionSatisfied()方法将用来判断是否该结束,takeAll()方法将结束当前子执行,并将主执行设置为active。
- 是否可以在运行时期新增/修改一个activity
当然可以!但是记住,标注有当前activity的execution在后续执行和结束的时候会用到这个activity!如果发生程序关闭等情况,execution会尝试从ProcessDefinition里重新根据ID加载activity,如下所示:
protected void ensureProcessDefinitionInitialized() {
if ((processDefinition == null) && (processDefinitionId != null)) {
ProcessDefinitionEntity deployedProcessDefinition = Context
.getProcessEngineConfiguration()
.getDeploymentManager()
.findDeployedProcessDefinitionById(processDefinitionId);
setProcessDefinition(deployedProcessDefinition);
}
}
protected void ensureActivityInitialized() {
if ((activity == null) && (activityId != null)) {
activity = getProcessDefinition().findActivity(activityId);
}
}
再来看看execution的set方法,就能明白它为什么会保留一堆id:
public void setActivity(ActivityImpl activity) {
this.activity = activity;
if (activity != null) {
this.activityId = activity.getId();
this.activityName = (String) activity.getProperty("name");
} else {
this.activityId = null;
this.activityName = null;
}
}
所以,要完全保证程序认识被改造的activity的途径是:自定义ProcessDefinition,重写其findActivity()方法!
- 为什么bpmn文件是XML格式,但model记录里面却采用的是JSON格式,而deployment里又采用的是XML格式?
不知道!真的不知道activiti为什么这么做!是想支持flex里面的JSON建模么?(如上结论主要是针对于activiti-modeler的实现,经仔细验证,activiti-engine对model的editorsource是没有任何限制的~~~)
- 进入多实例节点的时候,系统何时创建了新的子执行?
答案是AtomicOperationTransitionCreateScope.execute(),代码摘录如下:
public void execute(InterpretableExecution execution) {
InterpretableExecution propagatingExecution = null;
ActivityImpl activity = (ActivityImpl) execution.getActivity();
if (activity.isScope()) {
propagatingExecution = (InterpretableExecution) execution.createExecution();
propagatingExecution.setActivity(activity);
propagatingExecution.setTransition(execution.getTransition());
execution.setTransition(null);
execution.setActivity(null);
execution.setActive(false);
log.debug("create scope: parent {} continues as execution {}", execution, propagatingExecution);
propagatingExecution.initialize();
} else {
propagatingExecution = execution;
}
propagatingExecution.performOperation(AtomicOperation.TRANSITION_NOTIFY_LISTENER_START);
}
其中的activity就是当前的节点。
- 什么时候保存历史记录信息?如:HistoricActivity
魅力在于activity的executionListeners,代码如下:
public class ActivityInstanceEndHandler implements ExecutionListener {
public void notify(DelegateExecution execution) {
Context.getCommandContext().getHistoryManager()
.recordActivityEnd((ExecutionEntity) execution);
}
}