Search in sources :

Example 1 with Lifecycle

use of com.ctrip.xpipe.api.lifecycle.Lifecycle in project x-pipe by ctripcorp.

the class CreatedComponentRedistryTest method test.

@Test
public void test() throws Exception {
    CreatedComponentRedistry registry = new CreatedComponentRedistry();
    String name = registry.add(new Object());
    logger.info("{}", name);
    Lifecycle lifecycle = new NoOpLifecycleObject();
    registry.add(lifecycle);
    registry.initialize();
    registry.start();
    Assert.assertTrue(lifecycle.getLifecycleState().isStarted());
    Lifecycle lifecycle2 = new NoOpLifecycleObject();
    registry.add(lifecycle2);
    Assert.assertTrue(lifecycle2.getLifecycleState().isStarted());
    registry.remove(lifecycle2);
    Assert.assertTrue(lifecycle2.getLifecycleState().isDisposed());
    Assert.assertEquals(2, registry.allComponents().size());
}
Also used : CreatedComponentRedistry(com.ctrip.xpipe.lifecycle.CreatedComponentRedistry) Lifecycle(com.ctrip.xpipe.api.lifecycle.Lifecycle) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 2 with Lifecycle

use of com.ctrip.xpipe.api.lifecycle.Lifecycle in project x-pipe by ctripcorp.

the class AbstractComponentRegistry method disposeAllComponents.

private void disposeAllComponents() {
    List<Lifecycle> components = lifecycleCallable();
    Collections.reverse(components);
    for (Lifecycle lifecycle : components) {
        if (lifecycle.getLifecycleState().canDispose()) {
            try {
                lifecycle.dispose();
            } catch (Throwable th) {
                logger.error("[doDispose]" + lifecycle, th);
            }
        }
    }
}
Also used : Lifecycle(com.ctrip.xpipe.api.lifecycle.Lifecycle)

Example 3 with Lifecycle

use of com.ctrip.xpipe.api.lifecycle.Lifecycle in project x-pipe by ctripcorp.

the class AbstractComponentRegistry method stopAllComponents.

private void stopAllComponents() {
    List<Lifecycle> components = lifecycleCallable();
    Collections.reverse(components);
    for (Lifecycle lifecycle : components) {
        if (lifecycle.getLifecycleState().canStop()) {
            try {
                lifecycle.stop();
            } catch (Throwable th) {
                logger.error("[doStop]" + lifecycle, th);
            }
        }
    }
}
Also used : Lifecycle(com.ctrip.xpipe.api.lifecycle.Lifecycle)

Example 4 with Lifecycle

use of com.ctrip.xpipe.api.lifecycle.Lifecycle in project x-pipe by ctripcorp.

the class CreatedComponentRedistry method doRemove.

@Override
public boolean doRemove(Object component) throws Exception {
    String name = null;
    for (Entry<String, Object> entry : components.entrySet()) {
        if (entry.getValue() == component) {
            name = entry.getKey();
            break;
        }
    }
    if (name == null) {
        logger.info("[doRemove][can not find component]{}", component);
        return false;
    }
    logger.info("[doRemove]{}, {}", name, component);
    components.remove(name);
    if (component instanceof Lifecycle) {
        Lifecycle lifecycle = (Lifecycle) component;
        if (lifecycle.getLifecycleState() != null) {
            if (lifecycle.getLifecycleState().canStop()) {
                lifecycle.stop();
            }
            if (lifecycle.getLifecycleState().canDispose()) {
                lifecycle.dispose();
            }
        }
    }
    return true;
}
Also used : Lifecycle(com.ctrip.xpipe.api.lifecycle.Lifecycle)

Example 5 with Lifecycle

use of com.ctrip.xpipe.api.lifecycle.Lifecycle in project x-pipe by ctripcorp.

the class DefaultLifecycleControllerTest method testCanInitialize.

@Test
public void testCanInitialize() throws Exception {
    Lifecycle lifecycle = new NoOpLifecycleObject();
    lifecycle.initialize();
    try {
        lifecycle.initialize();
        Assert.fail();
    } catch (Exception e) {
    }
    lifecycle.start();
    try {
        lifecycle.initialize();
        Assert.fail();
    } catch (Exception e) {
    }
    lifecycle.stop();
    try {
        lifecycle.initialize();
        Assert.fail();
    } catch (Exception e) {
    }
    lifecycle.dispose();
    lifecycle.initialize();
}
Also used : Lifecycle(com.ctrip.xpipe.api.lifecycle.Lifecycle) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

Lifecycle (com.ctrip.xpipe.api.lifecycle.Lifecycle)10 AbstractTest (com.ctrip.xpipe.AbstractTest)6 Test (org.junit.Test)6 TopElement (com.ctrip.xpipe.api.lifecycle.TopElement)2 ComponentRegistry (com.ctrip.xpipe.api.lifecycle.ComponentRegistry)1 CreatedComponentRedistry (com.ctrip.xpipe.lifecycle.CreatedComponentRedistry)1 SpringComponentRegistry (com.ctrip.xpipe.lifecycle.SpringComponentRegistry)1 LinkedList (java.util.LinkedList)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1