use of com.alipay.sofa.runtime.api.ServiceRuntimeException 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.api.ServiceRuntimeException in project sofa-boot by alipay.
the class ServiceComponent method activateBinding.
private void activateBinding() {
Object target = service.getTarget();
if (target == null) {
throw new ServiceRuntimeException("Must contains the target object whiling registering Service.");
}
if (service.hasBinding()) {
Set<Binding> bindings = service.getBindings();
Iterator<Binding> it = bindings.iterator();
while (it.hasNext()) {
Binding binding = it.next();
BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
if (bindingAdapter == null) {
throw new ServiceRuntimeException("Can't find BindingAdapter of type " + binding.getBindingType() + " while registering service " + service + ".");
}
SofaLogger.info(" <<Out Binding [{0}] Begins - {1}.", binding.getBindingType(), service);
Object outBindingResult = bindingAdapter.outBinding(service, binding, target, getContext());
if (!Boolean.FALSE.equals(outBindingResult)) {
SofaLogger.info(" <<Out Binding [{0}] Ends - {1}.", binding.getBindingType(), service);
} else {
binding.setHealthy(false);
SofaLogger.info(" <<Out Binding [{0}] Fails, Don't publish service - {1}.", binding.getBindingType(), service);
}
}
}
SofaLogger.info("Register Service - {0}", service);
}
use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.
the class ServiceComponent method resolveBinding.
private void resolveBinding() {
Object target = service.getTarget();
if (target == null) {
throw new ServiceRuntimeException("Must contains the target object whiling registering Service.");
}
if (service.hasBinding()) {
Set<Binding> bindings = service.getBindings();
Iterator<Binding> it = bindings.iterator();
while (it.hasNext()) {
Binding binding = it.next();
BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
if (bindingAdapter == null) {
throw new ServiceRuntimeException("Can't find BindingAdapter of type " + binding.getBindingType() + " while registering service " + service + ".");
}
SofaLogger.info(" <<PreOut Binding [{0}] Begins - {1}.", binding.getBindingType(), service);
bindingAdapter.preOutBinding(service, binding, target, getContext());
SofaLogger.info(" <<PreOut Binding [{0}] Ends - {1}.", binding.getBindingType(), service);
}
}
}
use of com.alipay.sofa.runtime.api.ServiceRuntimeException 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("Unregiter component " + toString() + " got an error", e);
}
}
Object target = service.getTarget();
if (target == null) {
throw new ServiceRuntimeException("Must contains the target object whiling registering Service.");
}
if (service.hasBinding()) {
Set<Binding> bindings = service.getBindings();
for (Binding binding : bindings) {
BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
if (bindingAdapter == null) {
throw new ServiceRuntimeException("Can't find BindingAdapter of type " + binding.getBindingType() + " while unregister service " + service + ".");
}
SofaLogger.info(" <<Post un-out Binding [{0}] Begins - {1}.", binding.getBindingType(), service);
bindingAdapter.postUnoutBinding(service, binding, target, getContext());
SofaLogger.info(" <<Post un-out Binding [{0}] Ends - {1}.", binding.getBindingType(), service);
}
}
}
use of com.alipay.sofa.runtime.api.ServiceRuntimeException 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();
}
Aggregations