flowable實現節點超時自動跳過(二)——-工作日節點超時跳過
- 2019 年 11 月 13 日
- 筆記
之前寫過一個文章flowable實現節點超時自動跳過實現了流程超時自動跳過的功能. 但後面有朋友問我,能不能實現一個工作日自動跳過的功能,當前跳過,是非節假日的. 後面翻了一下flowable的源碼,發現其實實現也是不難的.
- 首先要有一個節假日的介面,這個需要第三方提供, 該介面傳入一個當前時間,及超時天數,計算需要超時跳過的時間.
- 重寫日曆時鐘的配置,示例程式碼如下:
@Bean public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> customIdGeneratorConfigurer() { return engineConfiguration -> { engineConfiguration.setIdGenerator(customIdGenerator()); engineConfiguration.setActivityBehaviorFactory(activityBehaviorFactory()); engineConfiguration.setTransactionManager(transactionManager); //設置全局事件監聽 engineConfiguration.setTypedEventListeners(this.getGlobalFlowableEventListener()); //需要先初始化時鐘 engineConfiguration.setClock(new DefaultClockImpl()); //自定義工作日曆 engineConfiguration.setBusinessCalendarManager(this.getBusinessCalendarManager(engineConfiguration.getClock())); }; } /** * 初始化日曆 * @param clock 時鐘 * @return 日曆實現 */ private MapBusinessCalendarManager getBusinessCalendarManager(Clock clock) { MapBusinessCalendarManager mapBusinessCalendarManager = new MapBusinessCalendarManager(); mapBusinessCalendarManager.addBusinessCalendar(DurationBusinessCalendar.NAME, new DurationBusinessCalendar(clock)); mapBusinessCalendarManager.addBusinessCalendar(DueDateBusinessCalendar.NAME, new AcDueDateBusinessCalendar(clock)); mapBusinessCalendarManager.addBusinessCalendar(CycleBusinessCalendar.NAME, new CycleBusinessCalendar(clock)); return mapBusinessCalendarManager; }
注意,上述程式碼中,AcDueDateBusinessCalendar是我重寫日曆超時的類
- 重寫日曆超時的類,示例如下:
public class AcDueDateBusinessCalendar extends DueDateBusinessCalendar { public AcDueDateBusinessCalendar(ClockReader clockReader) { super(clockReader); } @Override public Date resolveDuedate(String duedate, int maxIterations) { try { // 這裡可以調用工作日介面,返回計算後的工作日時間 if (duedate.startsWith("P")) { return new DateTime(clockReader.getCurrentTime()).plus(Period.parse(duedate)).toDate(); } return DateTime.parse(duedate).toDate(); } catch (Exception e) { throw new FlowableException("couldn't resolve duedate: " + e.getMessage(), e); } } }
看程式碼上的注釋,因為我手頭上沒有工作日的介面,先不實現,大家就按自己的需求實現即可. 至此,工作日節點超時跳過已完成.