Activiti6.0教程(6) - 排它閘道器/異或閘道器(ExclusiveGateway)用法
官方文件描述
An exclusive gateway (also called the XOR gateway or more technical the exclusive data-based gateway), is used to model a decision in the process. When the execution arrives at this gateway, all outgoing sequence flow are evaluated in the order in which they are defined. The sequence flow which condition evaluates to true (or which doesn’t have a condition set, conceptually having a ‘true’ defined on the sequence flow) is selected for continuing the process. Note that the semantics of outgoing sequence flow is different to that of the general case in BPMN 2.0. While in general all sequence flow which condition evaluates to true are selected to continue in a parallel way, only one sequence flow is selected when using the exclusive gateway. In case multiple sequence flow have a condition that evaluates to true, the first one defined in the XML (and only that one!) is selected for continuing the process. If no sequence flow can be selected, an exception will be thrown.
翻譯過來就是
排它閘道器(也稱為XOR閘道器或更技術的基於資料的排它閘道器)用於對流程中的決策進行建模。當執行到達此閘道器時,所有傳出序列流都將按定義順序進行計算。選
擇條件計算結果為真(或沒有條件集,概念上在序列流上定義了“真”)的序列流繼續處理。
注意,傳出序列流的語義不同於bpmn 2.0中的一般情況。通常情況下,條件值為true的所有序列流都選擇以並行方式繼續,但使用排它閘道器時只選擇一個序列
流。如果多個序列流的條件的計算結果為true,則在XML中定義的第一個條件(並且只有該條件!)已選擇繼續該程序。如果無法選擇序列流,則將引發異常。
說白了就是你傳變數,閘道器幫你決定該走哪條線路, 下面說一下用法
流程圖
首先它長這個樣
我這畫了個流程圖, 大致流程就是 使用者提交請假, 如果小於等於3天的由部門經理審批, 多於3天的要總經理審批
流程圖裡對應的受理人分別是
- 提交申請 user1
- 部門經理 user2
- 總經理 user3
測試
啟動流程
@Test public void startProcess() { ProcessInstance instance = runtimeService.startProcessInstanceByKey("TestExclusiveGateway"); System.out.println(instance.getId()); }
查詢任務
原連結文: https://tomoya92.github.io/2019/04/25/activiti-exclusive-gateway/
@Test public void queryTask() { List<Task> tasks = taskService.createTaskQuery().taskAssignee("user1") // 分頁查詢 // .listPage(firstResult, maxResults) // 排序 // .orderByTaskCreateTime().desc() .list(); for (Task task : tasks) { System.out.println("任務ID: " + task.getId()); System.out.println("受理人: " + task.getAssignee()); System.out.println("任務名: " + task.getName()); System.out.println("-------------------------------"); } }
提交任務
注意user1提交申請的時候就要設定請假天數(day)這個變量了, 有了這個變數流程圖的閘道器才會決定走哪條路
@Test public void completeTask() { String taskId = "18"; // 建立一個Map存放變數 Map<String, Object> variables = new HashMap<>(); variables.put("day", 4); // 處理任務 taskService.complete(taskId, variables); }
我這測試設定的是4天, 資料庫裡任務的受理人就到user3了
後面處理流程就跟正常流程一樣了
總結
這個閘道器的用處看網上有人說是能在流程圖裡處理的業務, 就不要寫在程式碼裡, 這樣做有一定的安全性保障, 其實想想看, 一般流程圖還不是程式設計師畫的, 程式碼也是程式設計師寫的, 安全性還不是程式設計師說的算 :joy:
寫部落格不易,轉載請保留原文連結,謝謝!
原文連結: