Search in sources :

Example 26 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class ServiceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() {
    if (!apiType && hasSofaServiceAnnotation()) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00103", beanId, ref.getClass()));
    }
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(ref);
    service = buildService();
    // default add jvm binding and service jvm binding should set serialize as true
    if (bindings.size() == 0) {
        JvmBindingParam jvmBindingParam = new JvmBindingParam().setSerialize(true);
        bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    }
    for (Binding binding : bindings) {
        service.addBinding(binding);
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    componentInfo.setApplicationContext(applicationContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 27 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class ServiceComponent method deactivate.

@Override
public void deactivate() throws ServiceRuntimeException {
    Object target = service.getTarget();
    if (target == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00000"));
    }
    if (service.hasBinding()) {
        boolean allPassed = true;
        Set<Binding> bindings = service.getBindings();
        for (Binding binding : bindings) {
            BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
            if (bindingAdapter == null) {
                throw new ServiceRuntimeException(ErrorCode.convert("01-00001", binding.getBindingType(), service));
            }
            SofaLogger.info(" <<Pre un-out Binding [{}] Begins - {}.", binding.getBindingType(), service);
            try {
                bindingAdapter.preUnoutBinding(service, binding, target, getContext());
            } catch (Throwable t) {
                allPassed = false;
                SofaLogger.error(ErrorCode.convert("01-00006", binding.getBindingType(), service), t);
                continue;
            }
            SofaLogger.info(" <<Pre un-out Binding [{}] Ends - {}.", binding.getBindingType(), service);
        }
        if (!allPassed) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00007", service));
        }
    }
    super.deactivate();
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 28 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class XsdTimeoutTest method testReferenceTimeout.

@Test
public void testReferenceTimeout() {
    Collection<ComponentInfo> c = sofaRuntimeContext.getComponentManager().getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : c) {
        if (componentInfo instanceof ReferenceComponent) {
            ReferenceComponent referenceComponent = (ReferenceComponent) componentInfo;
            if (!referenceComponent.getReference().getInterfaceType().equals(WhateverInterface.class)) {
                continue;
            }
            Binding binding = referenceComponent.getReference().getBinding(RpcBindingType.BOLT_BINDING_TYPE);
            if (binding instanceof RpcBinding) {
                RpcBinding rpcBinding = (RpcBinding) binding;
                Assert.assertEquals((long) rpcBinding.getRpcBindingParam().getTimeout(), 10000);
            }
        }
    }
}
Also used : RpcBinding(com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) RpcBinding(com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 29 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class ReferenceClientImpl method getReferenceFromReferenceParam.

@SuppressWarnings("unchecked")
private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
    BindingParam bindingParam = referenceParam.getBindingParam();
    Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null);
    if (bindingParam == null) {
        // default add jvm binding and reference jvm binding should set serialize as false
        JvmBindingParam jvmBindingParam = new JvmBindingParam();
        jvmBindingParam.setSerialize(false);
        reference.addBinding(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    } else {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(true);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        reference.addBinding(binding);
    }
    return reference;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 30 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class ReferenceComponent method activate.

@Override
public void activate() throws ServiceRuntimeException {
    if (reference.hasBinding()) {
        Binding candidate = null;
        Set<Binding> bindings = reference.getBindings();
        if (bindings.size() == 1) {
            candidate = bindings.iterator().next();
        } else if (bindings.size() > 1) {
            Object backupProxy = null;
            for (Binding binding : bindings) {
                if (JvmBinding.JVM_BINDING_TYPE.getType().equals(binding.getName())) {
                    candidate = binding;
                } else {
                    // Under normal RPC reference (local-first/jvm-first is not set to false) binding,
                    // backup proxy is the RPC proxy, which will be invoked if Jvm service is not found
                    backupProxy = createProxy(reference, binding);
                }
            }
            if (candidate != null) {
                ((JvmBinding) candidate).setBackupProxy(backupProxy);
            }
        }
        Object proxy = null;
        if (candidate != null) {
            proxy = createProxy(reference, candidate);
        }
        this.implementation = new DefaultImplementation();
        implementation.setTarget(proxy);
    }
    super.activate();
    latch.countDown();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation)

Aggregations

Binding (com.alipay.sofa.runtime.spi.binding.Binding)42 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)23 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)22 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)12 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)12 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)10 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)10 ArrayList (java.util.ArrayList)10 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)6 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)6 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)4 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)4 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)4 Reference (com.alipay.sofa.runtime.service.component.Reference)4 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)4 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)4 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)4 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)4 HealthResult (com.alipay.sofa.runtime.spi.health.HealthResult)4