Search in sources :

Example 36 with Binding

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

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 37 with Binding

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

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)

Example 38 with Binding

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

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 39 with Binding

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

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)

Example 40 with Binding

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

the class ServiceComponent method resolveBinding.

private void resolveBinding() {
    Object target = service.getTarget();
    if (target == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00000"));
    }
    if (service.hasBinding()) {
        Set<Binding> bindings = service.getBindings();
        boolean allPassed = true;
        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(" <<PreOut Binding [{}] Begins - {}.", binding.getBindingType(), service);
            try {
                bindingAdapter.preOutBinding(service, binding, target, getContext());
            } catch (Throwable t) {
                allPassed = false;
                SofaLogger.error(ErrorCode.convert("01-00002", binding.getBindingType(), service), t);
                continue;
            }
            SofaLogger.info(" <<PreOut Binding [{}] Ends - {}.", binding.getBindingType(), service);
        }
        if (!allPassed) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00003", service));
        }
    }
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) 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