Search in sources :

Example 1 with EventHandler

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) EventHandler(com.alipay.sofa.ark.spi.service.event.EventHandler) OrderComparator(com.alipay.sofa.ark.common.util.OrderComparator)

Example 2 with EventHandler

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));
}
Also used : Field(java.lang.reflect.Field) Biz(com.alipay.sofa.ark.spi.model.Biz) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) EventHandler(com.alipay.sofa.ark.spi.service.event.EventHandler) BizModel(com.alipay.sofa.ark.container.model.BizModel) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 3 with 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));
}
Also used : EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) ArrayList(java.util.ArrayList) EventHandler(com.alipay.sofa.ark.spi.service.event.EventHandler) ArkEvent(com.alipay.sofa.ark.spi.event.ArkEvent) File(java.io.File) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Aggregations

EventHandler (com.alipay.sofa.ark.spi.service.event.EventHandler)3 BaseTest (com.alipay.sofa.ark.container.BaseTest)2 EventAdminService (com.alipay.sofa.ark.spi.service.event.EventAdminService)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 OrderComparator (com.alipay.sofa.ark.common.util.OrderComparator)1 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 ArkEvent (com.alipay.sofa.ark.spi.event.ArkEvent)1 Biz (com.alipay.sofa.ark.spi.model.Biz)1 File (java.io.File)1 Field (java.lang.reflect.Field)1