之前写过一个文章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);
}
}
}
看代码上的注释,因为我手头上没有工作日的接口,先不实现,大家就按自己的需求实现即可.
至此,工作日节点超时跳过已完成.