Flowable源码地址:https://github.com/flowable/flowable-engine
包路径:org.flowable.engine.test.api.event
FlowableEventDispatcherTest Flowable事件调度器测试类
/**
* @author Frederik Heremans
*/
public class FlowableEventDispatcherTest extends PluggableFlowableTestCase {
protected FlowableEventDispatcher dispatcher;
@BeforeEach
protected void setUp() throws Exception {
dispatcher = new FlowableEventDispatcherImpl();
}
/**
* 测试添加监听器并检查是否向其发送了事件,还检查删除后是否未收到任何事件.
*/
@Test
public void testAddAndRemoveEventListenerAllEvents() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
// 调度事件
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).hasSize(2);
assertThat(newListener.getEventsReceived().get(0)).isEqualTo(event1);
assertThat(newListener.getEventsReceived().get(1)).isEqualTo(event2);
// 删除监听器并再次分派事件,不应调用监听器
dispatcher.removeEventListener(newListener);
newListener.clearEventsReceived();
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
/**
* 测试添加一个监听器,并检查是否向它发送了事件,以及它注册的类型。还检查删除后是否未收到任何事件.
*/
@Test
public void testAddAndRemoveEventListenerTyped() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener, FlowableEngineEventType.ENTITY_CREATED, FlowableEngineEventType.ENTITY_DELETED);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_DELETED);
FlowableEntityEventImpl event3 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_UPDATED);
// 分派事件,三分之二的事件应该已经进入监听器
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event3, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).hasSize(2);
assertThat(newListener.getEventsReceived().get(0)).isEqualTo(event1);
assertThat(newListener.getEventsReceived().get(1)).isEqualTo(event2);
// 删除监听器并再次分派事件,不应调用监听器
dispatcher.removeEventListener(newListener);
newListener.clearEventsReceived();
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
/**
* 测试是否从未调用添加空类型的监听器。
*/
@Test
public void testAddAndRemoveEventListenerTypedNullType() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener, (FlowableEngineEventType) null);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_DELETED);
// 分派事件时,所有事件都应该已进入监听器
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
...
}