Activiti 流程啟動及節點流轉原始碼分析

衣舞晨風發表於2016-04-23

作者:jiankunking 出處:http://blog.csdn.net/jiankunking

本文主要是以activiti-study中的xiaomage.xml流程圖為例進行跟蹤分析
具體的流程圖如下:
這裡寫圖片描述
流程圖對應的XML檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    <process id="MyProcess" name="MyProcess">
        <documentation>Place documentation for the 'MyProcess' process here.</documentation>
        <startEvent id="startevent1" name="Start"/>
        <userTask id="sss" name="ddd" activiti:assignee="fq"/>
        <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="sss"/>
        <endEvent id="endevent1" name="End"/>
        <sequenceFlow id="flow2" name="" sourceRef="sss" targetRef="endevent1"/>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_MyProcess">
        <bpmndi:BPMNPlane bpmnElement="MyProcess" id="BPMNPlane_MyProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35" width="35" x="340" y="150"/>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="sss" id="BPMNShape_sss">
                <omgdc:Bounds height="55" width="105" x="305" y="250"/>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35" width="35" x="340" y="370"/>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="357" y="185"/>
                <omgdi:waypoint x="357" y="250"/>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="357" y="305"/>
                <omgdi:waypoint x="357" y="370"/>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

流程例項建立過程如下(下圖轉載自:activiti 原始碼筆記之startProcess):
這裡寫圖片描述
流程啟動跟蹤分析:
圖一:
這裡寫圖片描述
圖二:
這裡寫圖片描述
圖三:
這裡寫圖片描述
圖四:
這裡寫圖片描述
以上主要是跟蹤分析了,三個節點之間的流轉情況。
在流轉的時候需要注意以下兩個介面:
原子操作(AtomicOperation)介面:

public interface AtomicOperation {

  AtomicOperation PROCESS_START = new AtomicOperationProcessStart();
  AtomicOperation PROCESS_START_INITIAL = new AtomicOperationProcessStartInitial();
  AtomicOperation PROCESS_END = new AtomicOperationProcessEnd();
  AtomicOperation ACTIVITY_START = new AtomicOperationActivityStart();
  AtomicOperation ACTIVITY_EXECUTE = new AtomicOperationActivityExecute();
  AtomicOperation ACTIVITY_END = new AtomicOperationActivityEnd();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_END = new AtomicOperationTransitionNotifyListenerEnd();
  AtomicOperation TRANSITION_DESTROY_SCOPE = new AtomicOperationTransitionDestroyScope();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_TAKE = new AtomicOperationTransitionNotifyListenerTake();
  AtomicOperation TRANSITION_CREATE_SCOPE = new AtomicOperationTransitionCreateScope();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_START = new AtomicOperationTransitionNotifyListenerStart();

  AtomicOperation DELETE_CASCADE = new AtomicOperationDeleteCascade();
  AtomicOperation DELETE_CASCADE_FIRE_ACTIVITY_END = new AtomicOperationDeleteCascadeFireActivityEnd();

  void execute(InterpretableExecution execution);

  boolean isAsync(InterpretableExecution execution);
}

注意:

void execute(InterpretableExecution execution);

InterpretableExecution介面:

public interface InterpretableExecution extends ActivityExecution, ExecutionListenerExecution, PvmProcessInstance {

  void take(PvmTransition transition);

  void take(PvmTransition transition, boolean fireActivityCompletedEvent);

  void setEventName(String eventName);

  void setEventSource(PvmProcessElement element);

  Integer getExecutionListenerIndex();
  void setExecutionListenerIndex(Integer executionListenerIndex);

  ProcessDefinitionImpl getProcessDefinition();

  void setActivity(ActivityImpl activity);

  void performOperation(AtomicOperation etomicOperation);

  boolean isScope();

  void destroy();

  void remove();

  InterpretableExecution getReplacedBy();
  void setReplacedBy(InterpretableExecution replacedBy);

  InterpretableExecution getSubProcessInstance();
  void setSubProcessInstance(InterpretableExecution subProcessInstance);

  InterpretableExecution getSuperExecution();

  void deleteCascade(String deleteReason);

  boolean isDeleteRoot();

  TransitionImpl getTransition();
  void setTransition(TransitionImpl object);

  void initialize();

  void setParent(InterpretableExecution parent);

  void setProcessDefinition(ProcessDefinitionImpl processDefinitionImpl);

  void setProcessInstance(InterpretableExecution processInstance);

  boolean isEventScope();

  void setEventScope(boolean isEventScope);

  StartingExecution getStartingExecution();

  void disposeStartingExecution();
}

注意:

void performOperation(AtomicOperation etomicOperation);

單獨摘出來的兩個方法是圖一中:
這裡寫圖片描述
上下文、原子操作、執行器實體三者相互呼叫的關鍵。
上圖的具體呼叫情況如下:
ExecutionEntity類中的:

public void performOperation(AtomicOperation executionOperation) {
    if (executionOperation.isAsync(this)) {
        scheduleAtomicOperationAsync(executionOperation);
    } else {
        performOperationSync(executionOperation);
    }
}

protected void performOperationSync(AtomicOperation executionOperation) {
    Context
    .getCommandContext()
    .performOperation(executionOperation, this);
}

performOperation函式中呼叫上下文CommandContext類中的:

public void performOperation(AtomicOperation executionOperation, InterpretableExecution execution) {
    nextOperations.add(executionOperation);
    if (nextOperations.size()==1) {
        try {
            Context.setExecutionContext(execution);
            while (!nextOperations.isEmpty()) {
                AtomicOperation currentOperation = nextOperations.removeFirst();
                if (log.isTraceEnabled()) {
                    log.trace("AtomicOperation: {} on {}", currentOperation, this);
                }
                if (execution.getReplacedBy() == null) {
                    currentOperation.execute(execution);
                } else {
                    currentOperation.execute(execution.getReplacedBy());
                }
            }
        } finally {
            Context.removeExecutionContext();
        }
    }
}

performOperation函式呼叫原子操作(AtomicOperation)介面中的void execute(InterpretableExecution execution)來處理。
該處的處理分為兩種情況:
1、根據AtomicOperation介面標識來繼續進行流轉
(再次呼叫ExecutionEntity類中的performOperation(AtomicOperation executionOperation)方法)
比如:
PROCESS_START=》PROCESS_START_INITIAL=》ACTIVITY_EXECUTE。。。。。。
具體可以參考本文圖一到圖四的程式碼跟蹤中的標識。
2、根據節點上的ActivityBehavior類進行不同的處理
這裡寫圖片描述
Activiti節點(開始、結束、任務、閘道器等等)都是Activity型別的,只是其掛的ActivityBehavior不同,通過不同的ActivityBehavior來實現相應的操作。

作者:jiankunking 出處:http://blog.csdn.net/jiankunking

相關文章