Search in sources :

Example 21 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.

the class ComponentManagerShutdownTest method testSkipAllComponentShutdown.

@Test
public void testSkipAllComponentShutdown() {
    SofaRuntimeProperties.setSkipAllComponentShutdown(Thread.currentThread().getContextClassLoader(), true);
    ComponentManager componentManager = initComponentManager();
    ComponentInfo serviceComponentInfo = componentManager.getComponentInfosByType(SERVICE_COMPONENT_TYPE).stream().findFirst().get();
    ComponentInfo springComponentInfo = componentManager.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
    GenericApplicationContext applicationContext = (GenericApplicationContext) springComponentInfo.getImplementation().getTarget();
    Assert.assertEquals(2, componentManager.size());
    Assert.assertTrue(serviceComponentInfo.isActivated());
    Assert.assertTrue(springComponentInfo.isActivated());
    Assert.assertTrue(applicationContext.isActive());
    componentManager.shutdown();
    Assert.assertEquals(2, componentManager.size());
    Assert.assertTrue(serviceComponentInfo.isActivated());
    Assert.assertTrue(springComponentInfo.isActivated());
    Assert.assertTrue(applicationContext.isActive());
    SofaRuntimeProperties.setSkipAllComponentShutdown(Thread.currentThread().getContextClassLoader(), false);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Test(org.junit.Test)

Example 22 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.

the class ComponentManagerTest method testRegisterServiceDuplicated.

@Test
public void testRegisterServiceDuplicated() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ComponentName componentName = ComponentNameFactory.createComponentName(SERVICE_COMPONENT_TYPE, SampleService.class, "");
    ComponentInfo componentInfo = componentManager.getComponentInfo(componentName);
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.setId("testModuleName");
    componentInfo.setApplicationContext(applicationContext);
    componentManager.register(componentInfo);
    Assert.assertEquals("testModuleName", componentInfo.getApplicationContext().getId());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.

the class IsleSpringComponentTest method test.

@Test
public void test() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ComponentName componentName1 = ComponentNameFactory.createComponentName(SpringContextComponent.SPRING_COMPONENT_TYPE, "com.alipay.sofa.isle.module1");
    ComponentName componentName2 = ComponentNameFactory.createComponentName(SpringContextComponent.SPRING_COMPONENT_TYPE, "com.alipay.sofa.isle.module2");
    Assert.assertNotNull(componentManager.getComponentInfo(componentName1));
    Assert.assertNotNull(componentManager.getComponentInfo(componentName2));
    Assert.assertEquals("com.alipay.sofa.isle.module1", componentManager.getComponentInfo(componentName1).getApplicationContext().getId());
    Assert.assertEquals("com.alipay.sofa.isle.module2", componentManager.getComponentInfo(componentName2).getApplicationContext().getId());
}
Also used : ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class SofaBootComponentsEndPoint method components.

@ReadOperation
public ApplicationComponents components() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    Map<String, Collection<ComponentDisplayInfo>> componentsInfoMap = new HashMap<>();
    Collection<ComponentType> componentTypes = componentManager.getComponentTypes();
    componentTypes.forEach(componentType -> {
        Collection<ComponentInfo> componentInfos = componentManager.getComponentInfosByType(componentType);
        Collection<ComponentDisplayInfo> componentDisplayInfos = componentInfos.stream().map(componentInfo -> new ComponentDisplayInfo(componentInfo.getName().getName(), componentInfo.getApplicationContext().getId())).collect(Collectors.toList());
        componentsInfoMap.put(componentType.getName(), componentDisplayInfos);
    });
    return new ApplicationComponents(componentsInfoMap);
}
Also used : SofaRuntimeContext(com.alipay.sofa.runtime.spi.component.SofaRuntimeContext) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation) ComponentType(com.alipay.sofa.runtime.model.ComponentType) Endpoint(org.springframework.boot.actuate.endpoint.annotation.Endpoint) Collection(java.util.Collection) Map(java.util.Map) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentType(com.alipay.sofa.runtime.model.ComponentType) HashMap(java.util.HashMap) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) Collection(java.util.Collection) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Example 25 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class ExtensionPointComponent method deactivate.

@Override
public void deactivate() throws ServiceRuntimeException {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    for (ComponentInfo componentInfo : componentManager.getComponents()) {
        if (componentInfo.getType().equals(ExtensionComponent.EXTENSION_COMPONENT_TYPE)) {
            ExtensionComponent extensionComponent = (ExtensionComponent) componentInfo;
            if (extensionComponent.getExtension().getTargetComponentName().equals(componentName)) {
                componentManager.unregister(componentInfo);
            }
        }
    }
    if (componentStatus != ComponentStatus.ACTIVATED) {
        return;
    }
    // skip deactivate SpringImplementationImpl because it's already deactivated
    if (this.implementation != null && !(implementation instanceof SpringImplementationImpl)) {
        Object target = this.implementation.getTarget();
        if (target instanceof ComponentLifeCycle) {
            ((ComponentLifeCycle) target).deactivate();
        }
    }
    componentStatus = ComponentStatus.RESOLVED;
}
Also used : ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) ComponentLifeCycle(com.alipay.sofa.runtime.api.component.ComponentLifeCycle) SpringImplementationImpl(com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl)

Aggregations

ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)31 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)26 Test (org.junit.Test)14 ComponentName (com.alipay.sofa.runtime.api.component.ComponentName)10 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)10 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 Binding (com.alipay.sofa.runtime.spi.binding.Binding)4 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)4 SofaRuntimeContext (com.alipay.sofa.runtime.spi.component.SofaRuntimeContext)4 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)3 ComponentLifeCycle (com.alipay.sofa.runtime.api.component.ComponentLifeCycle)2 ComponentType (com.alipay.sofa.runtime.model.ComponentType)2 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)2 SpringImplementationImpl (com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl)2 SpringContextComponent (com.alipay.sofa.runtime.spring.SpringContextComponent)2 SpringContextImplementation (com.alipay.sofa.runtime.spring.SpringContextImplementation)2 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)2 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)2