use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.
the class ConsumerConfigHelper method convertToMethodConfig.
private List<MethodConfig> convertToMethodConfig(List<RpcBindingMethodInfo> methodInfos) {
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
if (!CollectionUtils.isEmpty(methodInfos)) {
for (RpcBindingMethodInfo info : methodInfos) {
String name = info.getName();
Integer timeout = info.getTimeout();
Integer retries = info.getRetries();
String type = info.getType();
Object callbackHandler = info.getCallbackHandler();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName(name);
if (timeout != null) {
methodConfig.setTimeout(timeout);
}
if (retries != null) {
methodConfig.setRetries(retries);
}
if (StringUtils.hasText(type)) {
methodConfig.setInvokeType(type);
}
if (callbackHandler != null) {
if (callbackHandler instanceof SofaResponseCallback) {
methodConfig.setOnReturn((SofaResponseCallback) callbackHandler);
} else {
throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
}
}
methodConfigs.add(methodConfig);
}
}
return methodConfigs;
}
use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.
the class RpcBindingConverter method convertReferenceAnnotation.
/**
* convert props to RpcBindingParam
*
* @param bindingParam
* @param sofaReferenceBindingAnnotation
* @param bindingConverterContext
*/
protected void convertReferenceAnnotation(RpcBindingParam bindingParam, SofaReferenceBinding sofaReferenceBindingAnnotation, BindingConverterContext bindingConverterContext) {
if (sofaReferenceBindingAnnotation.addressWaitTime() != 0) {
bindingParam.setAddressWaitTime(sofaReferenceBindingAnnotation.addressWaitTime());
}
if (StringUtils.hasText(sofaReferenceBindingAnnotation.directUrl())) {
bindingParam.setTargetUrl(sofaReferenceBindingAnnotation.directUrl());
}
if (sofaReferenceBindingAnnotation.timeout() != 0) {
bindingParam.setTimeout(sofaReferenceBindingAnnotation.timeout());
}
if (StringUtils.hasText(sofaReferenceBindingAnnotation.serializeType())) {
bindingParam.setSerialization(sofaReferenceBindingAnnotation.serializeType());
}
if (StringUtils.hasText(sofaReferenceBindingAnnotation.loadBalancer())) {
bindingParam.setLoadBalancer(sofaReferenceBindingAnnotation.loadBalancer());
}
if (StringUtils.hasText(sofaReferenceBindingAnnotation.mockMode())) {
bindingParam.setMockMode(sofaReferenceBindingAnnotation.mockMode());
}
if (StringUtils.hasText(sofaReferenceBindingAnnotation.mockBean())) {
bindingParam.setMockBean(sofaReferenceBindingAnnotation.mockBean());
}
bindingParam.setType(sofaReferenceBindingAnnotation.invokeType());
ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
String[] filterNames = sofaReferenceBindingAnnotation.filters();
if (filterNames.length > 0) {
for (String filterName : filterNames) {
Object filter = applicationContext.getBean(filterName);
if (filter instanceof Filter) {
filters.add((Filter) filter);
} else {
throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
}
}
}
if (!CollectionUtils.isEmpty(filters)) {
bindingParam.setFilters(filters);
}
bindingParam.setRetries(sofaReferenceBindingAnnotation.retries());
String callbackRef = sofaReferenceBindingAnnotation.callbackHandler();
if (StringUtils.hasText(callbackRef)) {
bindingParam.setCallbackHandler(applicationContext.getBean(callbackRef));
}
bindingParam.setLazy(sofaReferenceBindingAnnotation.lazy());
String registryAlias = sofaReferenceBindingAnnotation.registry();
if (StringUtils.hasText(registryAlias)) {
String[] registrys = registryAlias.split(",");
bindingParam.setRegistrys(Arrays.asList(registrys));
}
SofaParameter[] parameters = sofaReferenceBindingAnnotation.parameters();
if (parameters.length > 0) {
bindingParam.setParameters(parseSofaParameters(parameters));
}
SofaMethod[] sofaMethods = sofaReferenceBindingAnnotation.methodInfos();
if (sofaMethods.length > 0) {
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
}
}
use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.
the class RpcBindingConverter method parseFilter.
protected void parseFilter(Element element, RpcBindingParam param, BindingConverterContext bindingConverterContext) {
List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(bindingConverterContext.getApplicationContext()));
if (element != null) {
List<String> filterNames = new ArrayList<String>();
String filterStrs = element.getAttribute(RpcBindingXmlConstants.TAG_FILTER);
if (StringUtils.hasText(filterStrs)) {
String[] subFilter = filterStrs.split(FILTER_SEPERATOR_SYMBOL);
for (String subfilterName : subFilter) {
if (StringUtils.hasText(subfilterName)) {
if (subfilterName.charAt(0) == EXCLUDE_FILTER_BEGIN_SYMBOL) {
String realFilterName = subfilterName.substring(1);
if (StringUtils.hasText(realFilterName)) {
filters.add(new ExcludeFilter(realFilterName));
}
} else {
filterNames.add(subfilterName);
}
}
}
}
if (!CollectionUtils.isEmpty(filterNames)) {
for (String filterName : filterNames) {
Object filter = bindingConverterContext.getApplicationContext().getBean(filterName);
if (filter instanceof Filter) {
filters.add((Filter) filter);
} else {
throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
}
}
}
}
param.setFilters(filters);
}
use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.
the class RpcBindingConverter method convertServiceAnnotation.
/**
* convert props to RpcBindingParam
*
* @param bindingParam
* @param sofaServiceAnnotation
* @param sofaServiceBindingAnnotation
* @param bindingConverterContext
*/
protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaService sofaServiceAnnotation, SofaServiceBinding sofaServiceBindingAnnotation, BindingConverterContext bindingConverterContext) {
if (sofaServiceBindingAnnotation.timeout() != 0) {
bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
}
// TODO need a magic number
if (sofaServiceBindingAnnotation.weight() != 0) {
bindingParam.setWeight(sofaServiceBindingAnnotation.weight());
}
if (sofaServiceBindingAnnotation.warmUpTime() != 0) {
bindingParam.setWarmUpTime(sofaServiceBindingAnnotation.warmUpTime());
}
if (sofaServiceBindingAnnotation.warmUpWeight() != 0) {
bindingParam.setWarmUpWeight(sofaServiceBindingAnnotation.warmUpWeight());
}
if (StringUtils.hasText(sofaServiceBindingAnnotation.serializeType())) {
bindingParam.setSerialization(sofaServiceBindingAnnotation.serializeType());
}
ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
String[] filterNames = sofaServiceBindingAnnotation.filters();
if (filterNames.length > 0) {
for (String filterName : filterNames) {
Object filter = applicationContext.getBean(filterName);
if (filter instanceof Filter) {
filters.add((Filter) filter);
} else {
throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
}
}
}
if (!CollectionUtils.isEmpty(filters)) {
bindingParam.setFilters(filters);
}
String threadPool = sofaServiceBindingAnnotation.userThreadPool();
if (StringUtils.hasText(threadPool)) {
UserThreadPool threadPoolObj = (UserThreadPool) applicationContext.getBean(threadPool);
String interfaceName = sofaServiceAnnotation.interfaceType().getCanonicalName();
String uniqId = sofaServiceAnnotation.uniqueId();
String uniqueName = interfaceName + ":1.0" + (StringUtils.isEmpty(uniqId) ? "" : ":" + uniqId);
UserThreadPoolManager.registerUserThread(uniqueName, threadPoolObj);
}
String registryAlias = sofaServiceBindingAnnotation.registry();
if (StringUtils.hasText(registryAlias)) {
String[] registrys = registryAlias.split(",");
bindingParam.setRegistrys(Arrays.asList(registrys));
}
SofaParameter[] parameters = sofaServiceBindingAnnotation.parameters();
if (parameters.length > 0) {
bindingParam.setParameters(parseSofaParameters(parameters));
}
SofaMethod[] sofaMethods = sofaServiceBindingAnnotation.methodInfos();
if (sofaMethods.length > 0) {
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
}
}
use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.
the class ServerConfigContainerTest method testCustomServerConfig.
@Test
public void testCustomServerConfig() {
final ServerConfig serverConfig = new ServerConfig();
serverConfig.setPort(123);
final String protocol = "xxx";
serverConfigContainer.registerCustomServerConfig(protocol, serverConfig);
ServerConfig serverConfig2 = serverConfigContainer.getServerConfig(protocol);
Assert.assertEquals(123, serverConfig2.getPort());
Assert.assertEquals(serverConfig.getPort(), serverConfig2.getPort());
boolean result = false;
serverConfigContainer.unRegisterCustomServerConfig(protocol);
try {
serverConfigContainer.getServerConfig(protocol);
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaBootRpcRuntimeException);
result = true;
}
Assert.assertTrue(result);
}
Aggregations