Search in sources :

Example 16 with ComponentManager

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

the class ComponentManagerShutdownTest method testSkipCommonComponentShutdown.

@Test
public void testSkipCommonComponentShutdown() {
    SofaRuntimeProperties.setSkipCommonComponentShutdown(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(1, componentManager.size());
    Assert.assertTrue(serviceComponentInfo.isActivated());
    Assert.assertFalse(springComponentInfo.isActivated());
    Assert.assertFalse(applicationContext.isActive());
    SofaRuntimeProperties.setSkipCommonComponentShutdown(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 17 with ComponentManager

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

the class ComponentManagerShutdownTest method initComponentManager.

private ComponentManager initComponentManager() {
    AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext(ComponentManagerTestConfiguration.class);
    SofaRuntimeContext sofaRuntimeContext = rootContext.getBean(SofaRuntimeContext.class);
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ComponentName serviceComponentName = ComponentNameFactory.createComponentName(SERVICE_COMPONENT_TYPE, SampleService.class, "");
    ComponentInfo serviceComponentInfo = componentManager.getComponentInfo(serviceComponentName);
    componentManager.register(serviceComponentInfo);
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    ComponentName springComponentName = ComponentNameFactory.createComponentName(SPRING_COMPONENT_TYPE, "testModule");
    ComponentInfo springComponentInfo = new SpringContextComponent(springComponentName, new SpringContextImplementation(applicationContext), sofaRuntimeContext);
    applicationContext.refresh();
    componentManager.register(springComponentInfo);
    return componentManager;
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SpringContextImplementation(com.alipay.sofa.runtime.spring.SpringContextImplementation) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) SpringContextComponent(com.alipay.sofa.runtime.spring.SpringContextComponent) SofaRuntimeContext(com.alipay.sofa.runtime.spi.component.SofaRuntimeContext)

Example 18 with ComponentManager

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

the class SofaBindingTest method testReferenceBinding.

@Test
public void testReferenceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent serializeTrueViaAnnotation = null;
    ReferenceComponent defaultSerializeFalseViaAnnotation = null;
    ReferenceComponent defaultElement = null;
    ReferenceComponent element = null;
    ReferenceComponent noneUniqueId = null;
    Collection<ComponentInfo> componentInfos = componentManager.getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : componentInfos) {
        String rawName = componentInfo.getName().getRawName();
        if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "serializeTrueViaAnnotation").getRawName())) {
            serializeTrueViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "defaultSerializeFalseViaAnnotation").getRawName())) {
            defaultSerializeFalseViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "default-element").getRawName())) {
            defaultElement = (ReferenceComponent) componentInfo;
        } else if (componentInfo.getName().getRawName().contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "element").getRawName())) {
            element = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(":#") && rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "").getRawName())) {
            noneUniqueId = (ReferenceComponent) componentInfo;
        }
    }
    Assert.assertNotNull(serializeTrueViaAnnotation);
    Assert.assertNotNull(defaultSerializeFalseViaAnnotation);
    Assert.assertNotNull(defaultElement);
    Assert.assertNotNull(element);
    Assert.assertNotNull(noneUniqueId);
    JvmBinding jvmBinding;
    jvmBinding = (JvmBinding) serializeTrueViaAnnotation.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultSerializeFalseViaAnnotation.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultElement.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) element.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) noneUniqueId.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
}
Also used : DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with ComponentManager

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

the class SofaFrameworkInitializer method addSofaRuntimeManager.

private static void addSofaRuntimeManager(String appName, ConfigurableApplicationContext applicationContext) {
    if (!SofaFrameworkHolder.containsSofaFramework()) {
        SofaFrameworkHolder.setSofaFramework(new SofaFrameworkImpl());
    }
    SofaFrameworkImpl sofaFramework = (SofaFrameworkImpl) SofaFrameworkHolder.getSofaFramework();
    AppConfiguration applicationConfiguration = createAppConfigurationImpl(applicationContext);
    ClientFactoryInternal clientFactoryInternal = new ClientFactoryImpl();
    SofaRuntimeManager sofaRuntimeManager = new StandardSofaRuntimeManager(appName, SofaFrameworkInitializer.class.getClassLoader(), applicationConfiguration, clientFactoryInternal);
    ComponentManager componentManager = sofaRuntimeManager.getComponentManager();
    // register service client & reference client
    componentManager.registerComponentClient(ServiceClient.class, new ServiceClientImpl(sofaRuntimeManager.getSofaRuntimeContext()));
    componentManager.registerComponentClient(ReferenceClient.class, new ReferenceClientImpl(sofaRuntimeManager.getSofaRuntimeContext()));
    sofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
}
Also used : ClientFactoryImpl(com.alipay.sofa.runtime.client.impl.ClientFactoryImpl) ClientFactoryInternal(com.alipay.sofa.runtime.spi.client.ClientFactoryInternal) SofaFrameworkImpl(com.alipay.sofa.runtime.SofaFrameworkImpl) AppConfiguration(com.alipay.sofa.runtime.api.component.AppConfiguration) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ReferenceClientImpl(com.alipay.sofa.runtime.service.client.ReferenceClientImpl) StandardSofaRuntimeManager(com.alipay.sofa.runtime.component.impl.StandardSofaRuntimeManager) ServiceClientImpl(com.alipay.sofa.runtime.service.client.ServiceClientImpl) SofaRuntimeManager(com.alipay.sofa.runtime.spi.component.SofaRuntimeManager) StandardSofaRuntimeManager(com.alipay.sofa.runtime.component.impl.StandardSofaRuntimeManager)

Example 20 with ComponentManager

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

the class SofaBindingTest method testServiceBinding.

@Test
public void testServiceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ServiceComponent serializeFalseViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "serializeFalseViaAnnotation"));
    ServiceComponent defaultSerializeTrueViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "defaultSerializeTrueViaAnnotation"));
    ServiceComponent defaultElement = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "default-element"));
    ServiceComponent element = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "element"));
    ServiceComponent noneUniqueId = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, ""));
    Assert.assertNotNull(serializeFalseViaAnnotation);
    Assert.assertNotNull(defaultSerializeTrueViaAnnotation);
    Assert.assertNotNull(defaultElement);
    Assert.assertNotNull(element);
    Assert.assertNotNull(noneUniqueId);
    JvmBinding jvmBinding;
    jvmBinding = (JvmBinding) serializeFalseViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultSerializeTrueViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultElement.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) element.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) noneUniqueId.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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