前面提到了實現Deployer接口擴展咱們本身的規則部署類,同時還要作一個監聽器,時時監控規則腳本的變化,比較麻煩,另外也提到了activiti自身對於規則的執行方式與咱們自己的不一樣,兩者仍是沒有統一。express
當咱們的系統擴張爲分佈式的時候,將規則腳本加載到本地內存並不合適,多臺機器之間的加載可能並不一樣步,這就形成了流程在A機器上執行的腳本與在B機器上執行的腳本不相同api
讓咱們統一一下,activiti也調用咱們本身的規則執行api入口,這樣就簡單了,配置,傳參都是經過編輯器可視化配置,只需修改BusinessRuleTaskActivityBehavior類,直接調用咱們的api,不調用RulesHelper,RulesDeployer,返回結果後,在調用流程走向api.這條路也通了,調試也都成功了,,編輯器
之前只是一個項目,項目運行加載類的時候,首先加載到咱們修改過的BusinessRuleTaskActivityBehavior,而不是去加載activiti jar包中的BusinessRuleTaskActivityBehavior,可是項目拆分爲分佈式的時候,問題又來了,加載順序變了,直接修改源碼帶來的麻煩仍是挺多的,最好的方式是去擴展分佈式
先看BusinessRuleParseHandler:ide
public class BusinessRuleParseHandler extends AbstractActivityBpmnParseHandler<BusinessRuleTask> { public Class< ? extends BaseElement> getHandledType() { return BusinessRuleTask.class; } @Override protected void executeParse(BpmnParse bpmnParse, BusinessRuleTask businessRuleTask) { ActivityImpl activity = createActivityOnCurrentScope(bpmnParse, businessRuleTask, BpmnXMLConstants.ELEMENT_TASK_BUSINESSRULE); activity.setAsync(businessRuleTask.isAsynchronous()); activity.setExclusive(!businessRuleTask.isNotExclusive()); activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createBusinessRuleTaskActivityBehavior(businessRuleTask)); } }
最後一段,找到ActivityBehaviorFactory實現DefaultActivityBehaviorFactory類調試
public BusinessRuleTaskActivityBehavior createBusinessRuleTaskActivityBehavior(BusinessRuleTask businessRuleTask) { BusinessRuleTaskActivityBehavior ruleActivity = null; if(StringUtils.isNotEmpty(businessRuleTask.getClassName())){ try { Class<?> clazz=Class.forName(businessRuleTask.getClassName()); ruleActivity=(BusinessRuleTaskActivityBehavior)clazz.newInstance(); } catch (Exception e) { throw new ActivitiException( "Could not instiate businessRuleTask class: ", e); } }else{ ruleActivity=new BusinessRuleTaskActivityBehavior(); }
看代碼知道BusinessRuleTask有個className屬性,能夠配置BusinessRuleTaskActivityBehavior的子類,那就行了,新建一個類繼承BusinessRuleTaskActivityBehavior,在這裏實現調用咱們本身的規則,這樣BusinessRuleTaskActivityBehavior也不用修改了,可是發現流程編輯器上並無給提供配置className屬性的地方。code
看BusinessRuleParseHandler代碼,是經過工廠模式建立的BusinessRuleTaskActivityBehavior實例,能不能實現自定義的工廠呢,而後注入咱們自定義的工廠,ActivityBehaviorFactory是一個接口,能夠實現咱們本身的工廠了,看能不能經過配置文件配置注入呢,找到activiti配置引擎ProcessEngineConfigurationImpl,其中一段代碼繼承
if (activityBehaviorFactory == null) { DefaultActivityBehaviorFactory defaultActivityBehaviorFactory = new DefaultActivityBehaviorFactory(); defaultActivityBehaviorFactory.setExpressionManager(expressionManager); activityBehaviorFactory = defaultActivityBehaviorFactory; } else if ((activityBehaviorFactory instanceof AbstractBehaviorFactory) && ((AbstractBehaviorFactory) activityBehaviorFactory).getExpressionManager() == null) { ((AbstractBehaviorFactory) activityBehaviorFactory).setExpressionManager(expressionManager); }
若是屬性activityBehaviorFactory是AbstractBehaviorFactory的實例就能夠了,至此擴展就沒問題了,總共擴展了兩個類,工廠類,業務規則執行類接口