咱們如今準備爲Activiti引擎添加額外的BPM邏輯。javascript
爲此,正如咱們的OnboardingRequest
Java類的名稱所示,咱們將使用簡單的入職流程。在這個例子中,咱們將輸入數據,而後,若是經驗年數超過3,則將發佈個性化入職歡迎消息的任務。在該任務中,用戶將手動將數據輸入一個僞後端系統。若是工做經驗在3年或如下,那麼只需簡單地、通常地、自動地將數據集成到一個僞後端系統中。java
Activiti的流程引擎兼容BPMN 2.0標準,在視覺上,上面的流程能夠這樣建模:express
這個例子很是簡單,並且,根據要求,能夠經過幾種不一樣的方式對其進行建模。雖然它也能夠協調簡單的流程,但請注意,Activiti能夠處理很是複雜的流程,包括數十個、數百個甚至數千個步驟。apache
上面的可視化流程模型的底層是BPMN的XML結構,在這種狀況下,XML文檔是onboarding.bpmn20.xml
,這個快速入門不會深刻到底層XML BPMN結構的深度,專一於針對Activiti API開發的機制並將Activiti嵌入到你的應用程序中。然而,爲了支持下面的邏輯,這裏是相關BPMN形狀和底層XML中編碼的定義邏輯的摘要:segmentfault
BPMN形狀 | Onboarding.bpmn20.xml 行 |
註釋 |
---|---|---|
4 | 開始事件 | |
5-10 | 用戶任務收集2個表單屬性:「fullName」和「yearsOfExperience」; 請注意,第9行的候選組設置爲「管理員」。 |
|
14-18 | 用戶任務收集1表單屬性:「personalWelcomeTime」; 請注意,第22行的候選組設置爲「管理員」。 |
|
21-24 | 腳本任務表示數據自動輸入到僞後端; 請注意,雖然簡單,但有一個簡單的腳本能夠設置一個流程變量 autoWelcomeTime |
|
25
26-28 25,26 |
定義「多年經驗」專屬網關; (決定將產生一條或另外一條路徑。) 使用yearsOfExperience變量表示「> 3」邏輯: ${yearsOfExperience > 3}
在專屬網關上,注意指向「automatedIntroPath」的默認節,表示條件「>3」的邏輯「else」。 |
|
19 | 結束事件 |
下載onboarding.bpmn20.xml
文件,下面的整個XML結構,並將onboarding.bpmn20.xml
文件複製到路徑$mvnProject
/src/main/resources/。後端
文件:$mvnProject/src/main/resources/onboarding.bpmn20.xmlui
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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/processdef"> <process id="onboarding" name="Onboarding" isExecutable="true"> <startEvent id="startOnboarding" name="Start" activiti:initiator="initiator"></startEvent> <userTask id="enterOnboardingData" name="Enter Data" activiti:assignee="${initiator}" activiti:candidateGroups="managers"> <extensionElements> <activiti:formProperty id="fullName" name="Full Name" type="string"></activiti:formProperty> <activiti:formProperty id="yearsOfExperience" name="Years of Experience" type="long" required="true"></activiti:formProperty> </extensionElements> </userTask> <sequenceFlow id="sid-1337EA98-7364-4198-B5D9-30F5341D6918" sourceRef="startOnboarding" targetRef="enterOnboardingData"></sequenceFlow> <exclusiveGateway id="decision" name="Years of Experience" default="automatedIntroPath"></exclusiveGateway> <sequenceFlow id="sid-42BE5661-C3D5-4DE6-96F5-73D34822727A" sourceRef="enterOnboardingData" targetRef="decision"></sequenceFlow> <userTask id="personalizedIntro" name="Personalized Introduction and Data Entry" activiti:assignee="${initiator}" activiti:candidateGroups="managers"> <extensionElements> <activiti:formProperty id="personalWelcomeTime" name="Personal Welcome Time" type="date" datePattern="MM-dd-yyyy hh:mm"></activiti:formProperty> </extensionElements> </userTask> <endEvent id="endOnboarding" name="End"></endEvent> <sequenceFlow id="sid-37A73ACA-2E23-400B-96F3-71F77738DAFA" sourceRef="automatedIntro" targetRef="endOnboarding"></sequenceFlow> <scriptTask id="automatedIntro" name="Generic and Automated Data Entry" scriptFormat="javascript" activiti:autoStoreVariables="false"> <script><![CDATA[var dateAsString = new Date().toString(); execution.setVariable("autoWelcomeTime", dateAsString);]]></script> </scriptTask> <sequenceFlow id="automatedIntroPath" sourceRef="decision" targetRef="automatedIntro"></sequenceFlow> <sequenceFlow id="personalizedIntroPath" name=">3" sourceRef="decision" targetRef="personalizedIntro"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${yearsOfExperience > 3}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="sid-BA6F061B-47B6-428B-8CE6-739244B14BD6" sourceRef="personalizedIntro" targetRef="endOnboarding"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_onboarding"> <bpmndi:BPMNPlane bpmnElement="onboarding" id="BPMNPlane_onboarding"> <bpmndi:BPMNShape bpmnElement="startOnboarding" id="BPMNShape_startOnboarding"> <omgdc:Bounds height="30.0" width="30.0" x="155.0" y="145.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="enterOnboardingData" id="BPMNShape_enterOnboardingData"> <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="decision" id="BPMNShape_decision"> <omgdc:Bounds height="40.0" width="40.0" x="385.0" y="140.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="personalizedIntro" id="BPMNShape_personalizedIntro"> <omgdc:Bounds height="80.0" width="100.0" x="519.0" y="15.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endOnboarding" id="BPMNShape_endOnboarding"> <omgdc:Bounds height="28.0" width="28.0" x="725.0" y="165.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="automatedIntro" id="BPMNShape_automatedIntro"> <omgdc:Bounds height="80.0" width="100.0" x="520.0" y="255.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="sid-37A73ACA-2E23-400B-96F3-71F77738DAFA" id="BPMNEdge_sid-37A73ACA-2E23-400B-96F3-71F77738DAFA"> <omgdi:waypoint x="570.0" y="255.0"></omgdi:waypoint> <omgdi:waypoint x="570.0" y="179.0"></omgdi:waypoint> <omgdi:waypoint x="725.0" y="179.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-1337EA98-7364-4198-B5D9-30F5341D6918" id="BPMNEdge_sid-1337EA98-7364-4198-B5D9-30F5341D6918"> <omgdi:waypoint x="185.0" y="160.0"></omgdi:waypoint> <omgdi:waypoint x="240.0" y="160.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="automatedIntroPath" id="BPMNEdge_automatedIntroPath"> <omgdi:waypoint x="405.0" y="180.0"></omgdi:waypoint> <omgdi:waypoint x="405.0" y="295.0"></omgdi:waypoint> <omgdi:waypoint x="520.0" y="295.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="personalizedIntroPath" id="BPMNEdge_personalizedIntroPath"> <omgdi:waypoint x="405.0" y="140.0"></omgdi:waypoint> <omgdi:waypoint x="405.0" y="55.0"></omgdi:waypoint> <omgdi:waypoint x="519.0" y="55.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-42BE5661-C3D5-4DE6-96F5-73D34822727A" id="BPMNEdge_sid-42BE5661-C3D5-4DE6-96F5-73D34822727A"> <omgdi:waypoint x="340.0" y="160.0"></omgdi:waypoint> <omgdi:waypoint x="385.0" y="160.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-BA6F061B-47B6-428B-8CE6-739244B14BD6" id="BPMNEdge_sid-BA6F061B-47B6-428B-8CE6-739244B14BD6"> <omgdi:waypoint x="619.0" y="55.0"></omgdi:waypoint> <omgdi:waypoint x="739.0" y="55.0"></omgdi:waypoint> <omgdi:waypoint x="739.0" y="165.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
添加到OnboardingRequest.java
,以下圖所示:編碼
文件:$mvnProject/src/main/java/com/example/OnboardingRequest.javaspa
package com.example; import java.text.ParseException; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.RepositoryService; import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; public class OnboardingRequest { public static void main(String[] args) throws ParseException { ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration() .setJdbcUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000") .setJdbcUsername("sa") .setJdbcPassword("") .setJdbcDriver("org.h2.Driver") .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); ProcessEngine processEngine = cfg.buildProcessEngine(); String pName = processEngine.getName(); String ver = ProcessEngine.VERSION; System.out.println("ProcessEngine [" + pName + "] Version: [" + ver + "]"); RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment() .addClasspathResource("onboarding.bpmn20.xml").deploy(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .deploymentId(deployment.getId()).singleResult(); System.out.println( "Found process definition [" + processDefinition.getName() + "] with id [" + processDefinition.getId() + "]"); } }
文件:$mvnProject/src/main/java/com/example/OnboardingRequest.javacode
添加行 | 說明 |
---|---|
25-27 | 加載提供的BPMN模型並將其部署到Activiti流程引擎。 |
28-33 | 檢索已部署的模型,證實它位於Activiti存儲庫中。 |
有關BPMN及其在Activiti中的使用的更多信息,請參閱Activiti用戶指南中的各個部分。
經過運行「mvn package」打包代碼。
像之前同樣運行Java程序,示例輸出以下所示。
命令:java -jar target/ActivitiDeveloperQuickStart-0.0.1-SNAPSHOT-jar-with-dependencies.jar
或 java -jar target/$quickStartJavaProjectName-0.0.1-SNAPSHOT-jar-with-dependencies.jar
... 02:01:19,277 [main] INFO org.activiti.engine.impl.ProcessEngineImpl - ProcessEngine default created processEngine [default] version: [5.22.0.0] ... 02:01:19,327 [main] DEBUG org.activiti.engine.impl.bpmn.deployer.BpmnDeployer - Processing deployment null 02:01:19,327 [main] INFO org.activiti.engine.impl.bpmn.deployer.BpmnDeployer - Processing resource onboarding.bpmn20.xml 02:01:19,444 [main] DEBUG org.activiti.engine.impl.bpmn.parser.handler.ProcessParseHandler - Parsing process ... 02:01:21,696 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 667346055 to pool. 02:01:21,696 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - --- DeployCmd finished -------------------------------------------------------- ... 02:01:21,696 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - --- starting ProcessDefinitionQueryImpl -------------------------------------------------------- ... 02:01:21,710 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 667346055 to pool. 02:01:21,710 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - --- ProcessDefinitionQueryImpl finished -------------------------------------------------------- 02:01:21,710 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - Found process definition [Onboarding] with id [onboarding:1:4]
這裏感興趣的關鍵輸出是列表行,它記錄了流程名稱「Onboarding」和「onboarding:1:4」的惟一流程ID(包括部署版本)。
你的應用程序如今在運行時部署Onboarding流程。