use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.
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);
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.
the class ComponentManagerShutdownTest method testNormalShutdown.
@Test
public void testNormalShutdown() {
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(0, componentManager.size());
Assert.assertFalse(serviceComponentInfo.isActivated());
Assert.assertFalse(springComponentInfo.isActivated());
Assert.assertFalse(applicationContext.isActive());
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.
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;
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.
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());
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.
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());
}
Aggregations