activiti 5.21工作流规则引擎扩展(businessRuleTask)
2019獨角獸企業重金招聘Python工程師標準>>>
背景介紹:
公司有自己的規則引擎配置平臺,執行核心為drools,配置后生成規則腳本,存入數據庫,執行的時候調用drools的Api,關鍵代碼:
?? ??? ?StatelessSession statelessSession = ruleBase.newStatelessSession();
?? ??? ?statelessSession.setGlobal("externalConditionResult", true);
? ? ? ? statelessSession.execute(list);
以前一直是在后臺硬編碼調用每一個規則。
使用activiti5工作流引擎,節點使用上只是用了用戶任務和自動任務,封裝在兩個方面:1.流程啟動,任務獲取,完成任務,也就是對這幾個API的封裝,同時引入了流程業務關聯表。2。在流程節點分配這塊使用我們系統的崗位或者角色,擁有該崗位/角色的人都可以處理這個任務,分配到崗位/角色這塊是通過規則配置的,后臺也是硬編碼的,現在平臺升級了以后,有這么個需求,就是流程編輯器上可以直接配置規則節點,這樣的話,用戶可以直接修改流程圖,選擇要調用的規則
activiti5本身是支持規則節點的,網上查一查,也能查到一些資料,總結一下:
<property name="customPostDeployers">
<list>
<bean class="org.activiti.engine.impl.rules.RulesDeployer" />
</list>
</property>
就是說執行到規則節點的時候,以.drl結尾的drool腳本會交給RulesDeploer部署,加載到內存,供規則節點執行器執行。
4。在流程編輯器上的規則節點添加規則名,輸入變量,輸出變量,繼承JavaDelegate獲取輸出變量,或者直接在流程圖上根據流程變量zhi配置節點的走向
這樣看就完活了,但是實際執行我們的規則的時候怎么調試到報錯,這方面沒有完善的文檔,只能看源碼了只能看源代碼了。
首先我們的規則執行方式是無狀態的StatelessKnowledgeSession,結果變量就是一個實體類,是以在內存中更新全局變量的方式返回的。
規則節點的執行是BusinessRuleTaskActivityBehavior來處理的
?KnowledgeBase knowledgeBase =RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId);?
?StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
1.通過流程實例部署ID,在內存中找到以.drl結尾的腳本名稱,如果沒有,重新查詢act_ge_bytearray表,加載到內存,添加到知識庫.
2.獲取規則節點上配置的輸入輸出變量,規則名,如果不配置規則名,則全部執行,執行方式是通過有狀態StatefulKnowledgeSession的fireAllRules來執行規則腳本,其中一個關鍵點是我們的規則腳本里面很多個規則名,總不能一個一個寫到編輯器上面吧,重寫執行源碼,發現工作流實現了drools的規則匹配,只需修改我們的規則名已相同的名字結尾,流程編輯器上配置這個相同的名字,就可以全部執行了,對于結果變量,drools不支持在他的工組內存中更新全局變量,如果需要這樣做,就要調用ksession.setGlobal("ruleResultBase", obj);將全局變量set到他的工作內存,獲取結果變量是一個集合,規則那一套基本不會改動,那怎么辦,直接修改改這個類源碼好了,修改為獲取當個實體(不是一個好方法,后面會說到),就這樣把源碼拿出來改了,可以部署了,調試也通過了。。。。,
第一個問題解決了:通過改動BusinessRuleTaskActivityBehavior源碼,工作流可以調用我們的規則腳本,可以和規則聯動了。
public void execute(ActivityExecution execution) throws Exception {PvmProcessDefinition processDefinition = execution.getActivity().getProcessDefinition();String deploymentId = processDefinition.getDeploymentId();KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId); StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();if (variablesInputExpressions != null) {Iterator<Expression> itVariable = variablesInputExpressions.iterator();while (itVariable.hasNext()) {Expression variable = itVariable.next();Object obj=variable.getValue(execution);if(obj instanceof IRuleResultBase){//設置全局變量ksession.setGlobal("ruleResultBase", obj);}ksession.insert(variable.getValue(execution));}}if (!rulesExpressions.isEmpty()) {RulesAgendaFilter filter = new RulesAgendaFilter();Iterator<Expression> itRuleNames = rulesExpressions.iterator();while (itRuleNames.hasNext()) {Expression ruleName = itRuleNames.next();filter.addSuffic(ruleName.getValue(execution).toString());}filter.setAccept(!exclude);ksession.fireAllRules(filter);} else {ksession.fireAllRules();}//客戶化 結果bean/* Collection<Object> ruleOutputObjects = ksession.getObjects();if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {Collection<Object> outputVariables = new ArrayList<Object>();for (Object object : ruleOutputObjects) {outputVariables.add(object);}execution.setVariable(resultVariable, outputVariables);}*//*ruleResultBase=(RuleResultBase) ksession.getGlobal("ruleResultBase");Collection<Object> outputVariables = new ArrayList<Object>();outputVariables.add(ruleResultBase);execution.setVariable(resultVariable, ruleResultBase);*/execution.setVariable(resultVariable, ksession.getGlobal("ruleResultBase"));ksession.dispose();leave(execution);}?
但是有幾個問題,規則編輯之后難道要重新部署流程嗎?看流程是如何加載規則的
public class RulesHelper {public static KnowledgeBase findKnowledgeBaseByDeploymentId(String deploymentId) {DeploymentCache<Object> knowledgeBaseCache = Context.getProcessEngineConfiguration().getDeploymentManager().getKnowledgeBaseCache();KnowledgeBase knowledgeBase = (KnowledgeBase) knowledgeBaseCache.get(deploymentId);if (knowledgeBase==null) {DeploymentEntity deployment = Context.getCommandContext().getDeploymentEntityManager().findDeploymentById(deploymentId);if (deployment==null) {throw new ActivitiObjectNotFoundException("no deployment with id "+deploymentId, Deployment.class);}Context.getProcessEngineConfiguration().getDeploymentManager().deploy(deployment);knowledgeBase = (KnowledgeBase) knowledgeBaseCache.get(deploymentId);if (knowledgeBase==null) {throw new ActivitiException("deployment "+deploymentId+" doesn't contain any rules");}}return knowledgeBase;} }發現上面代碼從緩存中取KnowledgeBase,?if (knowledgeBase==null),那么根據部署id,去數據庫查詢,然后交給Deployer來部署,這是一個接口:
public interface Deployer {void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings); }對于規則的部署,activiti的實現類為前面提到的,配置文件中配置的RulesDeployer
public class RulesDeployer implements Deployer {private static final Logger log = LoggerFactory.getLogger(RulesDeployer.class);public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {log.debug("Processing deployment {}", deployment.getName());KnowledgeBuilder knowledgeBuilder = null;DeploymentManager deploymentManager = Context.getProcessEngineConfiguration().getDeploymentManager();Map<String, ResourceEntity> resources = deployment.getResources();for (String resourceName : resources.keySet()) {log.info("Processing resource {}", resourceName);if (resourceName.endsWith(".drl")) { // is only parsing .drls sufficient? what about other rule dsl's? (@see ResourceType)if (knowledgeBuilder==null) {knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();}ResourceEntity resourceEntity = resources.get(resourceName);byte[] resourceBytes = resourceEntity.getBytes();Resource droolsResource = ResourceFactory.newByteArrayResource(resourceBytes);knowledgeBuilder.add(droolsResource, ResourceType.DRL);}}if (knowledgeBuilder!=null) {KnowledgeBase knowledgeBase = knowledgeBuilder.newKnowledgeBase();deploymentManager.getKnowledgeBaseCache().add(deployment.getId(), knowledgeBase);}} }從這里可以知道,文件必須是以.drl結尾,所謂部署就是將規則腳本添加到知識庫,用部署ID,作為key,KnowledgeBase作為value,加載到本地緩存,那么解決辦法如下:
- 重新部署流程,帶著更新后的規則腳本
- 直接修改此類,設置一個監聽器,時時更新緩存
- 這個類是實現的Deployer接口,并且在配置文件中可配置的,那么最好的辦法就是,實現我們自己的類來擴展
轉載于:https://my.oschina.net/u/2886096/blog/1819094
總結
以上是生活随笔為你收集整理的activiti 5.21工作流规则引擎扩展(businessRuleTask)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云有一群 “猪猪侠”
- 下一篇: 图片和图形之性能和视图层次结构(18)