flowable 全局监听

做到flowable 全局监听这一部分时,按网上查的资料,都无法正确进入监听器。
参考链接:https://blog.csdn.net/liuwenjun05101/article/details/103594484
百思不得其解……
无奈之下,去看了《Flowable BPMN 用户手册》,其中有段叙述让我茅塞顿开:

3.18.7. 附加信息
监听器只会响应其所在引擎分发的事件。因此如果在同一个数据库上运行不同的引擎,则只有该监听器注册的引擎生成的事件,才会分发给该监听器。其他引擎生成的事件不会分发给这个监听器,而不论这些引擎是否运行在同一个JVM下。
某些事件类型(与实体相关)暴露了目标实体。按照事件类型的不同,有些实体不能被更新(如实体删除事件中的实体)。如果可能的话,请使用事件暴露的EngineServices来安全地操作引擎。即使这样,更新、操作事件中暴露的实体仍然需要小心。
历史不会分发实体事件,因为它们都有对应的运行时实体分发事件。

另外一部分:

3.18.3. 在运行时添加监听器
可以使用API(RuntimeService)为引擎添加或删除事件监听器:

/**
 * 新增一个监听器,会在所有事件发生时被通知。
 * @param listenerToAdd 要新增的监听器
 */
void addEventListener(FlowableEventListener listenerToAdd);

/**
 * 新增一个监听器,在给定类型的事件发生时被通知。
 * @param listenerToAdd 要新增的监听器
 * @param types 监听器需要监听的事件类型
 */
void addEventListener(FlowableEventListener listenerToAdd, FlowableEventType... types);

/**
 * 从分发器中移除指定监听器。该监听器将不再被通知,无论该监听器注册为监听何种类型。
 * @param listenerToRemove 要移除的监听器
 */
 void removeEventListener(FlowableEventListener listenerToRemove);

经测试,使用以上方法可以有效触发监听器进行相关操作。

  • Listener
@Component
public class ProcessInstanceCompleteListener extends AbstractFlowableEngineEventListener {
    @Autowired
    private TaskService taskService;
    @Autowired
    private FormService formService;
    @Autowired
    private RuntimeService runtimeService;
    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        System.out.println("进入流程结束监听器……");

        String processInstanceId = event.getProcessInstanceId();
        Map<String, Object> variables = runtimeService.getVariables(processInstanceId);
        Map<String, Object> startForm = (Map<String, Object>) variables.get("startForm");
        Form form = formService.getById(new Long((String) startForm.get("formId")));

        System.out.println("variables: "+variables);
        System.out.println("startForm: "+startForm);
        System.out.println("form: " + form);

        super.processCompleted(event);
    }

    @Override
    protected void taskCompleted(FlowableEngineEntityEvent event) {
        System.out.println("进入taskCompleted监听器……");
        super.taskCompleted(event);
    }

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        System.out.println("进入taskCompleted监听器--onEvent……");
        super.onEvent(flowableEvent);
    }
}
  • 配置器
/**
 * 全局监听配置 ContextRefreshedEvent在类被初始化之后触发
 */
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private ProcessInstanceCompleteListener processInstanceCompleteListener;
    @Autowired
    private RuntimeService runtimeService;
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //任务完成全局监听
        //dispatcher.addEventListener(activityCompleteListener, FlowableEngineEventType.TASK_CREATED);
        runtimeService.addEventListener(processInstanceCompleteListener, FlowableEngineEventType.PROCESS_COMPLETED);
    }
}

  • 运行截图
    flowable 全局监听

特此记之,以供参考。

上一篇:@Autowired和@Resource默认字段注入流程源码分析


下一篇:(六)Bean的自动装配