Search in sources :

Example 1 with ServiceComponent

use of com.alipay.sofa.runtime.service.component.ServiceComponent 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("Argument delay must be a positive integer or zero.");
    }
    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 ServiceComponent

use of com.alipay.sofa.runtime.service.component.ServiceComponent 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("Interface type is null. Interface type is required while publish a service.");
    }
    Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
    for (BindingParam bindingParam : serviceParam.getBindingParams()) {
        BindingConverter bindingConverter = BindingFactoryContainer.getBindingConverterFactory().getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException("Can not found binding converter for binding type " + 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, 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 3 with ServiceComponent

use of com.alipay.sofa.runtime.service.component.ServiceComponent in project sofa-boot by alipay.

the class ServiceAnnotationBeanPostProcessor method processSofaService.

private void processSofaService(Object bean, String beanName) {
    final Class<?> beanClass = AopProxyUtils.ultimateTargetClass(bean);
    SofaService sofaServiceAnnotation = beanClass.getAnnotation(SofaService.class);
    if (sofaServiceAnnotation == null) {
        return;
    }
    Class<?> interfaceType = sofaServiceAnnotation.interfaceType();
    if (interfaceType.equals(void.class)) {
        Class<?>[] interfaces = beanClass.getInterfaces();
        if (interfaces == null || interfaces.length == 0 || interfaces.length > 1) {
            throw new ServiceRuntimeException("Bean " + beanName + " does not has any interface or has more than one interface.");
        }
        interfaceType = interfaces[0];
    }
    Implementation implementation = new SpringImplementationImpl(beanName, applicationContext);
    implementation.setTarget(bean);
    Service service = new ServiceImpl(sofaServiceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, bean);
    service.addBinding(new JvmBinding());
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) Service(com.alipay.sofa.runtime.service.component.Service) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) SpringImplementationImpl(com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 4 with ServiceComponent

use of com.alipay.sofa.runtime.service.component.ServiceComponent in project sofa-boot by alipay.

the class ServiceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() throws Exception {
    if (hasSofaServiceAnnotation()) {
        throw new ServiceRuntimeException("Bean " + beanId + " of type " + ref.getClass() + " has already annotated by @SofaService," + " can not be registered using xml. Please check it.");
    }
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(ref);
    service = buildService();
    if (bindings.size() == 0) {
        bindings.add(new JvmBinding());
    }
    for (Binding binding : bindings) {
        service.addBinding(binding);
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, 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) 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) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Aggregations

ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)4 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)4 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)3 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)3 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)3 Service (com.alipay.sofa.runtime.service.component.Service)2 ServiceImpl (com.alipay.sofa.runtime.service.component.impl.ServiceImpl)2 Binding (com.alipay.sofa.runtime.spi.binding.Binding)2 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)2 SofaService (com.alipay.sofa.runtime.api.annotation.SofaService)1 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)1 Property (com.alipay.sofa.runtime.api.component.Property)1 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)1 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)1 SpringImplementationImpl (com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl)1