Search in sources :

Example 1 with Property

use of com.alipay.sofa.runtime.api.component.Property in project sofa-boot by sofastack.

the class ServiceClientImpl method removeService.

@Override
public void removeService(Class<?> interfaceClass, String uniqueId, int millisecondsToDelay) {
    if (millisecondsToDelay < 0) {
        throw new IllegalArgumentException(ErrorCode.convert("01-00202"));
    }
    Collection<ComponentInfo> serviceComponents = sofaRuntimeContext.getComponentManager().getComponentInfosByType(ServiceComponent.SERVICE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : serviceComponents) {
        if (!(componentInfo instanceof ServiceComponent)) {
            continue;
        }
        ServiceComponent serviceComponent = (ServiceComponent) componentInfo;
        if (serviceComponent.getService().getInterfaceType() == interfaceClass && serviceComponent.getService().getUniqueId().equals(uniqueId)) {
            Map<String, Property> properties = serviceComponent.getProperties();
            Property property = new Property();
            property.setValue(millisecondsToDelay);
            properties.put(ServiceComponent.UNREGISTER_DELAY_MILLISECONDS, property);
            sofaRuntimeContext.getComponentManager().unregister(serviceComponent);
        }
    }
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Property(com.alipay.sofa.runtime.api.component.Property)

Example 2 with Property

use of com.alipay.sofa.runtime.api.component.Property in project sofa-boot by alipay.

the class ServiceClientImpl method removeService.

@Override
public void removeService(Class<?> interfaceClass, String uniqueId, int millisecondsToDelay) {
    if (millisecondsToDelay < 0) {
        throw new IllegalArgumentException(ErrorCode.convert("01-00202"));
    }
    Collection<ComponentInfo> serviceComponents = sofaRuntimeContext.getComponentManager().getComponentInfosByType(ServiceComponent.SERVICE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : serviceComponents) {
        if (!(componentInfo instanceof ServiceComponent)) {
            continue;
        }
        ServiceComponent serviceComponent = (ServiceComponent) componentInfo;
        if (serviceComponent.getService().getInterfaceType() == interfaceClass && serviceComponent.getService().getUniqueId().equals(uniqueId)) {
            Map<String, Property> properties = serviceComponent.getProperties();
            Property property = new Property();
            property.setValue(millisecondsToDelay);
            properties.put(ServiceComponent.UNREGISTER_DELAY_MILLISECONDS, property);
            sofaRuntimeContext.getComponentManager().unregister(serviceComponent);
        }
    }
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Property(com.alipay.sofa.runtime.api.component.Property)

Example 3 with Property

use of com.alipay.sofa.runtime.api.component.Property 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)

Example 4 with Property

use of com.alipay.sofa.runtime.api.component.Property 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)

Aggregations

Property (com.alipay.sofa.runtime.api.component.Property)4 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)2 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)2 Binding (com.alipay.sofa.runtime.spi.binding.Binding)2 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)2