Search in sources :

Example 1 with EventAdminService

use of com.alipay.sofa.ark.spi.service.event.EventAdminService in project sofa-ark by alipay.

the class BizModel method start.

@Override
public void start(String[] args) throws Throwable {
    AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED");
    if (mainClass == null) {
        throw new ArkRuntimeException(String.format("biz: %s has no main method", getBizName()));
    }
    ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.classLoader);
    EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);
    try {
        eventAdminService.sendEvent(new BeforeBizStartupEvent(this));
        resetProperties();
        if (!isMasterBizAndEmbedEnable()) {
            long start = System.currentTimeMillis();
            MainMethodRunner mainMethodRunner = new MainMethodRunner(mainClass, args);
            mainMethodRunner.run();
            // this can trigger health checker handler
            eventAdminService.sendEvent(new AfterBizStartupEvent(this));
            LOGGER.info("Ark biz {} started in {} ms", getIdentity(), (System.currentTimeMillis() - start));
        }
    } catch (Throwable e) {
        bizState = BizState.BROKEN;
        throw e;
    } finally {
        ClassLoaderUtils.popContextClassLoader(oldClassLoader);
    }
    BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(BizManagerService.class);
    if (Boolean.getBoolean(Constants.ACTIVATE_NEW_MODULE)) {
        Biz currentActiveBiz = bizManagerService.getActiveBiz(bizName);
        if (currentActiveBiz == null) {
            bizState = BizState.ACTIVATED;
        } else {
            ((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED);
            bizState = BizState.ACTIVATED;
        }
    } else {
        if (bizManagerService.getActiveBiz(bizName) == null) {
            bizState = BizState.ACTIVATED;
        } else {
            bizState = BizState.DEACTIVATED;
        }
    }
}
Also used : BeforeBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent) MainMethodRunner(com.alipay.sofa.ark.bootstrap.MainMethodRunner) Biz(com.alipay.sofa.ark.spi.model.Biz) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) AfterBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent) AbstractClasspathClassLoader(com.alipay.sofa.ark.container.service.classloader.AbstractClasspathClassLoader) BizManagerService(com.alipay.sofa.ark.spi.service.biz.BizManagerService) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException)

Example 2 with EventAdminService

use of com.alipay.sofa.ark.spi.service.event.EventAdminService in project sofa-ark by alipay.

the class PluginModel method stop.

@Override
public void stop() throws ArkRuntimeException {
    EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);
    eventAdminService.sendEvent(new BeforePluginStopEvent(this));
    try {
        if (pluginActivator != null) {
            pluginActivator.stop(pluginContext);
        }
    } catch (Throwable ex) {
        throw new ArkRuntimeException(ex.getMessage(), ex);
    } finally {
        eventAdminService.sendEvent(new AfterPluginStopEvent(this));
    }
}
Also used : BeforePluginStopEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStopEvent) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) AfterPluginStopEvent(com.alipay.sofa.ark.spi.event.plugin.AfterPluginStopEvent)

Example 3 with EventAdminService

use of com.alipay.sofa.ark.spi.service.event.EventAdminService in project sofa-ark by alipay.

the class PluginModel method start.

@Override
public void start() throws ArkRuntimeException {
    if (activator == null || activator.isEmpty()) {
        return;
    }
    EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);
    ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.pluginClassLoader);
    try {
        eventAdminService.sendEvent(new BeforePluginStartupEvent(this));
        pluginActivator = (PluginActivator) pluginClassLoader.loadClass(activator).newInstance();
        pluginActivator.start(pluginContext);
    } catch (Throwable ex) {
        throw new ArkRuntimeException(ex.getMessage(), ex);
    } finally {
        eventAdminService.sendEvent(new AfterPluginStartupEvent(this));
        ClassLoaderUtils.popContextClassLoader(oldClassLoader);
    }
}
Also used : BeforePluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) AfterPluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.AfterPluginStartupEvent) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException)

Example 4 with EventAdminService

use of com.alipay.sofa.ark.spi.service.event.EventAdminService in project sofa-ark by alipay.

the class ZookeeperConfigActivator method registerEventHandler.

protected void registerEventHandler(final PluginContext context) {
    final String bizInitConfig = new String(bizNodeCache.getCurrentData().getData());
    EventAdminService eventAdminService = context.referenceService(EventAdminService.class).getService();
    eventAdminService.register(new EventHandler<AfterFinishDeployEvent>() {

        @Override
        public void handleEvent(AfterFinishDeployEvent event) {
            LOGGER.info("Start to process init app config: {}", bizInitConfig);
            OperationProcessor.process(OperationTransformer.transformToBizOperation(bizInitConfig, context));
        }

        @Override
        public int getPriority() {
            return 0;
        }
    });
    eventAdminService.register(new EventHandler<AfterFinishStartupEvent>() {

        @Override
        public void handleEvent(AfterFinishStartupEvent event) {
            ConfigProcessor.createConfigProcessor(context, ipConfigDeque, "ip-zookeeper-config").start();
            ConfigProcessor.createConfigProcessor(context, bizConfigDeque, "app-zookeeper-config").start();
        }

        @Override
        public int getPriority() {
            return 0;
        }
    });
}
Also used : AfterFinishDeployEvent(com.alipay.sofa.ark.spi.event.AfterFinishDeployEvent) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) AfterFinishStartupEvent(com.alipay.sofa.ark.spi.event.AfterFinishStartupEvent)

Example 5 with EventAdminService

use of com.alipay.sofa.ark.spi.service.event.EventAdminService 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)

Aggregations

EventAdminService (com.alipay.sofa.ark.spi.service.event.EventAdminService)7 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)3 BaseTest (com.alipay.sofa.ark.container.BaseTest)2 AbstractClasspathClassLoader (com.alipay.sofa.ark.container.service.classloader.AbstractClasspathClassLoader)2 Biz (com.alipay.sofa.ark.spi.model.Biz)2 BizManagerService (com.alipay.sofa.ark.spi.service.biz.BizManagerService)2 EventHandler (com.alipay.sofa.ark.spi.service.event.EventHandler)2 Test (org.junit.Test)2 MainMethodRunner (com.alipay.sofa.ark.bootstrap.MainMethodRunner)1 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 AfterFinishDeployEvent (com.alipay.sofa.ark.spi.event.AfterFinishDeployEvent)1 AfterFinishStartupEvent (com.alipay.sofa.ark.spi.event.AfterFinishStartupEvent)1 ArkEvent (com.alipay.sofa.ark.spi.event.ArkEvent)1 AfterBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent)1 AfterBizStopEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStopEvent)1 BeforeBizRecycleEvent (com.alipay.sofa.ark.spi.event.biz.BeforeBizRecycleEvent)1 BeforeBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent)1 BeforeBizStopEvent (com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent)1 AfterPluginStartupEvent (com.alipay.sofa.ark.spi.event.plugin.AfterPluginStartupEvent)1 AfterPluginStopEvent (com.alipay.sofa.ark.spi.event.plugin.AfterPluginStopEvent)1