use of com.alipay.sofa.ark.spi.service.event.EventHandler in project sofa-ark by alipay.
the class EventAdminServiceImpl method sendEvent.
@Override
public void sendEvent(ArkEvent event) {
List<EventHandler> eventHandlers = new ArrayList<>();
for (CopyOnWriteArraySet<EventHandler> values : SUBSCRIBER_MAP.values()) {
eventHandlers.addAll(values);
}
for (ServiceReference<EventHandler> eventHandler : registryService.referenceServices(EventHandler.class, null)) {
eventHandlers.add(eventHandler.getService());
}
Collections.sort(eventHandlers, new OrderComparator());
for (EventHandler eventHandler : eventHandlers) {
if (isSupportEventType(eventHandler, event)) {
eventHandler.handleEvent(event);
}
}
}
use of com.alipay.sofa.ark.spi.service.event.EventHandler in project sofa-ark by alipay.
the class EventAdminServiceTest method test.
@Test
public void test() throws Throwable {
try {
Field field = EventAdminServiceImpl.class.getDeclaredField("SUBSCRIBER_MAP");
field.setAccessible(true);
map = (Map) field.get(null);
} catch (Throwable throwable) {
Assert.assertNull(throwable);
}
EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);
eventAdminService.register(new LowPriorityMockEventHandler());
eventAdminService.register(new HighPriorityMockEventHandler());
eventAdminService.register(new BeforeBizStopEventHandler());
ClassLoader bizClassLoader = getClass().getClassLoader();
Biz biz = new BizModel().setBizState(BizState.DEACTIVATED).setBizName("mock name").setBizVersion("mock name").setClassLoader(bizClassLoader);
Assert.assertNotNull(map.get(bizClassLoader));
biz.stop();
Assert.assertNull(map.get(bizClassLoader));
Assert.assertTrue(mark == 50);
EventHandler eventHandler = new LowPriorityMockEventHandler();
eventAdminService.register(eventHandler);
Assert.assertNotNull(map.get(bizClassLoader));
eventAdminService.unRegister(eventHandler);
Assert.assertFalse(((Set) map.get(bizClassLoader)).contains(eventHandler));
}
use of com.alipay.sofa.ark.spi.service.event.EventHandler in project sofa-ark by alipay.
the class ArkClientTest method testBizArguments.
@Test
public void testBizArguments() throws Throwable {
EventAdminService eventAdminService = arkServiceContainer.getService(EventAdminService.class);
List<String> topicList = new ArrayList<>();
EventHandler eventHandler = new EventHandler<ArkEvent>() {
@Override
public void handleEvent(ArkEvent event) {
topicList.add(event.getTopic());
}
@Override
public int getPriority() {
return 0;
}
};
eventAdminService.register(eventHandler);
File bizFile3 = ArkClient.createBizSaveFile("biz-demo", "3.0.0");
FileUtils.copyInputStreamToFile(bizUrl3.openStream(), bizFile3);
ArkClient.installBiz(bizFile3);
// ArkClient.installBiz(new File(bizUrl3.getFile()));
ArkClient.uninstallBiz("biz-demo", "3.0.0");
File bizFile33 = ArkClient.createBizSaveFile("biz-demo", "3.0.0");
FileUtils.copyInputStreamToFile(bizUrl3.openStream(), bizFile33);
ArkClient.installBiz(bizFile33, new String[] { "demo" });
ArkClient.uninstallBiz("biz-demo", "3.0.0");
Assert.assertEquals("BEFORE-INVOKE-BIZ-START", topicList.get(0));
Assert.assertEquals("No arguments", topicList.get(1));
Assert.assertEquals("AFTER-INVOKE-BIZ-START", topicList.get(2));
// new event
Assert.assertEquals("BEFORE-RECYCLE-BIZ", topicList.get(4));
Assert.assertEquals("demo", topicList.get(7));
}
Aggregations