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);
}
}
}
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);
}
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);
}
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);
}
Aggregations