Search in sources :

Example 11 with Binding

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

the class ReferenceRegisterHelper method registerReference.

public static Object registerReference(Reference reference, SofaRuntimeContext sofaRuntimeContext) {
    Binding binding = (Binding) reference.getBindings().toArray()[0];
    if (reference.jvmService() && binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
        throw new ServiceRuntimeException("jvm-service=\"true\" can not be used with JVM binding.");
    }
    if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE) && isLocalFirst(reference, sofaRuntimeContext)) {
        reference.addBinding(new JvmBinding());
    }
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent referenceComponent = new ReferenceComponent(reference, new DefaultImplementation(), sofaRuntimeContext);
    if (componentManager.isRegistered(referenceComponent.getName())) {
        return componentManager.getComponentInfo(referenceComponent.getName()).getImplementation().getTarget();
    }
    ComponentInfo componentInfo = componentManager.registerAndGet(referenceComponent);
    return componentInfo.getImplementation().getTarget();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 12 with Binding

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

the class ReferenceRegisterHelper method registerReference.

public static Object registerReference(Reference reference, BindingAdapterFactory bindingAdapterFactory, SofaRuntimeContext sofaRuntimeContext) {
    Binding binding = (Binding) reference.getBindings().toArray()[0];
    if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE) && !SofaRuntimeProperties.isDisableJvmFirst(sofaRuntimeContext) && reference.isJvmFirst()) {
        // as rpc invocation would be serialized, so here would Not ignore serialized
        reference.addBinding(new JvmBinding());
    }
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent referenceComponent = new ReferenceComponent(reference, new DefaultImplementation(), bindingAdapterFactory, sofaRuntimeContext);
    if (componentManager.isRegistered(referenceComponent.getName())) {
        return componentManager.getComponentInfo(referenceComponent.getName()).getImplementation().getTarget();
    }
    ComponentInfo componentInfo = componentManager.registerAndGet(referenceComponent);
    return componentInfo.getImplementation().getTarget();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 13 with Binding

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

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);
}
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) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Service(com.alipay.sofa.runtime.service.component.Service) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 14 with Binding

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

the class ServiceComponent method dump.

@Override
public String dump() {
    StringBuilder sb = new StringBuilder(super.dump());
    Collection<Binding> bindings = service.getBindings();
    for (Binding binding : bindings) {
        sb.append("\n|------>[binding]-").append(binding.dump());
    }
    return sb.toString();
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding)

Example 15 with Binding

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

the class ServiceComponent method unregister.

@Override
public void unregister() throws ServiceRuntimeException {
    super.unregister();
    Property unregisterDelayMillisecondsProperty = properties.get(UNREGISTER_DELAY_MILLISECONDS);
    if (unregisterDelayMillisecondsProperty != null) {
        int unregisterDelayMilliseconds = unregisterDelayMillisecondsProperty.getInteger();
        try {
            TimeUnit.MILLISECONDS.sleep(unregisterDelayMilliseconds);
        } catch (InterruptedException e) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00010", service), e);
        }
    }
    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(" <<Post un-out Binding [{}] Begins - {}.", binding.getBindingType(), service);
            try {
                bindingAdapter.postUnoutBinding(service, binding, target, getContext());
            } catch (Throwable t) {
                allPassed = false;
                SofaLogger.error(ErrorCode.convert("01-00008", binding.getBindingType(), service), t);
                continue;
            }
            SofaLogger.info(" <<Post un-out Binding [{}] Ends - {}.", binding.getBindingType(), service);
        }
        if (!allPassed) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00009", service));
        }
    }
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) Property(com.alipay.sofa.runtime.api.component.Property) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

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