use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.
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());
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.
the class SofaEventHandlerTest method testDynamicProxyFinder.
@Test
public void testDynamicProxyFinder() throws Exception {
SofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
new MockUp<DynamicJvmServiceProxyFinder>() {
@Mock
public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) {
return biz;
}
};
new Expectations() {
{
biz.getIdentity();
result = "MockName:MockVersion";
biz.getBizState();
result = BizState.ACTIVATED;
sofaRuntimeManager.getAppClassLoader();
result = ctx.getClassLoader().getParent();
}
};
new Expectations() {
{
sofaRuntimeManager.getComponentManager();
result = ((SofaRuntimeContext) ctx.getBean("sofaRuntimeContext")).getComponentManager();
contract.getInterfaceType();
result = SampleService.class;
contract.getUniqueId();
result = "";
contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
result = new JvmBinding();
invocation.getArguments();
result = new Object[] {};
invocation.getMethod();
result = SampleService.class.getMethod("service");
}
};
DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().setHasFinishStartup(true);
ServiceProxy serviceProxy = DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().findServiceProxy(ctx.getClassLoader(), contract);
try {
Assert.assertTrue(SofaEventHandlerTest.class.getName().equals(serviceProxy.invoke(invocation)));
} catch (Throwable throwable) {
throw new RuntimeException("testDynamicProxyFinder case failed.", throwable);
}
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.
the class ReferenceFactoryBean method doAfterPropertiesSet.
@Override
protected void doAfterPropertiesSet() throws Exception {
Reference reference = buildReference();
Assert.isTrue(bindings.size() <= 1, "Found more than one binding in <sofa:reference/>, <sofa:reference/> can only have one binding.");
// default add jvm binding and reference jvm binding should set serialize as false
if (bindings.size() == 0) {
// default reference prefer to ignore serialize
JvmBindingParam jvmBindingParam = new JvmBindingParam();
jvmBindingParam.setSerialize(false);
bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
}
reference.addBinding(bindings.get(0));
proxy = ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext, applicationContext);
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.
the class ServiceClientImpl method service.
@SuppressWarnings("unchecked")
public void service(ServiceParam serviceParam) {
Implementation implementation = new DefaultImplementation();
implementation.setTarget(serviceParam.getInstance());
if (serviceParam.getInterfaceType() == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00201"));
}
Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
for (BindingParam bindingParam : serviceParam.getBindingParams()) {
BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
if (bindingConverter == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
}
BindingConverterContext bindingConverterContext = new BindingConverterContext();
bindingConverterContext.setInBinding(false);
bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
service.addBinding(binding);
}
boolean hasJvmBinding = false;
for (Binding binding : service.getBindings()) {
if (binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
hasJvmBinding = true;
break;
}
}
if (!hasJvmBinding) {
service.addBinding(new JvmBinding());
}
ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
sofaRuntimeContext.getComponentManager().register(componentInfo);
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.
the class ReferenceComponent method isHealthy.
@Override
public HealthResult isHealthy() {
HealthResult result = new HealthResult(componentName.getRawName());
List<HealthResult> bindingHealth = new ArrayList<>();
JvmBinding jvmBinding = null;
HealthResult jvmBindingHealthResult = null;
if (reference.hasBinding()) {
for (Binding binding : reference.getBindings()) {
bindingHealth.add(binding.healthCheck());
if (JvmBinding.JVM_BINDING_TYPE.equals(binding.getBindingType())) {
jvmBinding = (JvmBinding) binding;
jvmBindingHealthResult = bindingHealth.get(bindingHealth.size() - 1);
}
}
}
// check reference has a corresponding service
if (!SofaRuntimeProperties.isSkipJvmReferenceHealthCheck(sofaRuntimeContext) && jvmBinding != null) {
Object serviceTarget = getServiceTarget();
if (serviceTarget == null && !jvmBinding.hasBackupProxy()) {
jvmBindingHealthResult.setHealthy(false);
jvmBindingHealthResult.setHealthReport("can not find corresponding jvm service");
}
}
List<HealthResult> failedBindingHealth = new ArrayList<>();
for (HealthResult healthResult : bindingHealth) {
if (healthResult != null && !healthResult.isHealthy()) {
failedBindingHealth.add(healthResult);
}
}
result.setHealthy(failedBindingHealth.size() == 0);
String report = aggregateBindingHealth(reference.getBindings());
if (e != null) {
report += " [" + e.getMessage() + "]";
result.setHealthy(false);
}
result.setHealthReport(report);
return result;
}
Aggregations