use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ClassUtils method newInstance.
/**
* 实例化一个对象(只检测默认构造函数,其它不管)
*
* @param clazz 对象类
* @param <T> 对象具体类
* @return 对象实例
* @throws SofaRpcRuntimeException 没有找到方法,或者无法处理,或者初始化方法异常等
*/
public static <T> T newInstance(Class<T> clazz) throws SofaRpcRuntimeException {
if (clazz.isPrimitive()) {
return (T) getDefaultPrimitiveValue(clazz);
}
T t = getDefaultWrapperValue(clazz);
if (t != null) {
return t;
}
try {
// 普通类,如果是成员类(需要多传一个父类参数)
if (!(clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()))) {
try {
// 先找一个空的构造函数
Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (Exception ignore) {
// NOPMD
}
}
// 不行的话,找一个最少参数的构造函数
Constructor<T>[] constructors = (Constructor<T>[]) clazz.getDeclaredConstructors();
if (constructors == null || constructors.length == 0) {
throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no default constructor!");
}
Constructor<T> constructor = constructors[0];
if (constructor.getParameterTypes().length > 0) {
for (Constructor<T> c : constructors) {
if (c.getParameterTypes().length < constructor.getParameterTypes().length) {
constructor = c;
if (constructor.getParameterTypes().length == 0) {
break;
}
}
}
}
constructor.setAccessible(true);
// 虚拟构造函数的参数值,基本类型使用默认值,其它类型使用null
Class<?>[] argTypes = constructor.getParameterTypes();
Object[] args = new Object[argTypes.length];
for (int i = 0; i < args.length; i++) {
args[i] = getDefaultPrimitiveValue(argTypes[i]);
}
return constructor.newInstance(args);
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(e.getMessage(), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class AbstractCluster method init.
@Override
public synchronized void init() {
if (initialized) {
// 已初始化
return;
}
// 构造Router链
routerChain = RouterChain.buildConsumerChain(consumerBootstrap);
// 负载均衡策略 考虑是否可动态替换?
loadBalancer = LoadBalancerFactory.getLoadBalancer(consumerBootstrap);
// 地址管理器
addressHolder = AddressHolderFactory.getAddressHolder(consumerBootstrap);
// 连接管理器
connectionHolder = ConnectionHolderFactory.getConnectionHolder(consumerBootstrap);
// 构造Filter链,最底层是调用过滤器
this.filterChain = FilterChain.buildConsumerChain(this.consumerConfig, new ConsumerInvoker(consumerBootstrap));
if (consumerConfig.isLazy()) {
// 延迟连接
if (LOGGER.isInfoEnabled(consumerConfig.getAppName())) {
LOGGER.infoWithApp(consumerConfig.getAppName(), "Connection will be initialized when first invoke.");
}
}
// 启动重连线程
connectionHolder.init();
try {
// 得到服务端列表
List<ProviderGroup> all = consumerBootstrap.subscribe();
if (CommonUtils.isNotEmpty(all)) {
// 初始化服务端连接(建立长连接)
updateAllProviders(all);
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_INIT_PROVIDER_TRANSPORT), e);
}
// 启动成功
initialized = true;
// 如果check=true表示强依赖
if (consumerConfig.isCheck() && !isAvailable()) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_CHECK_ALIVE_PROVIDER));
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ClusterFactory method getCluster.
/**
* 构造Cluster对象
*
* @param consumerBootstrap 客户端配置
* @return Cluster对象
*/
public static Cluster getCluster(ConsumerBootstrap consumerBootstrap) {
String cluster = null;
try {
ConsumerConfig consumerConfig = consumerBootstrap.getConsumerConfig();
cluster = consumerConfig.getCluster();
ExtensionClass<Cluster> ext = ExtensionLoaderFactory.getExtensionLoader(Cluster.class).getExtensionClass(cluster);
if (ext == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CLUSTER, cluster));
}
return ext.getExtInstance(new Class[] { ConsumerBootstrap.class }, new Object[] { consumerBootstrap });
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CLUSTER, cluster), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NetUtils method getAvailablePort.
/**
* 检查当前指定端口是否可用,不可用则自动+1再试(随机端口从默认端口开始检查)
*
* @param host 当前ip地址
* @param port 当前指定端口
* @param maxPort 最大端口
* @return 从指定端口开始后第一个可用的端口
*/
public static int getAvailablePort(String host, int port, int maxPort) {
if (isAnyHost(host) || isLocalHost(host) || isHostInNetworkCard(host)) {
if (port < MIN_PORT) {
port = MIN_PORT;
}
for (int i = port; i <= maxPort; i++) {
ServerSocket ss = null;
try {
ss = new ServerSocket();
ss.bind(new InetSocketAddress(host, i));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("ip:{} port:{} is available", host, i);
}
return i;
} catch (IOException e) {
// continue
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Can't bind to address [{}:{}], " + "Maybe 1) The port has been bound. " + "2) The network card of this host is not exists or disable. " + "3) The host is wrong.", host, i);
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Begin try next port(auto +1):{}", i + 1);
}
} finally {
IOUtils.closeQuietly(ss);
}
}
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_BIND_PORT_ERROR, host));
} else {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HOST_NOT_FOUND, host));
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class AbstractInterfaceConfig method queryAttribute.
/**
* 查询属性值
*
* @param property 属性
* @return oldValue 属性值
*/
public String queryAttribute(String property) {
try {
Object oldValue = null;
if (property.charAt(0) == RpcConstants.HIDE_KEY_PREFIX) {
// 方法级配置 例如.echoStr.timeout
String methodAndP = property.substring(1);
int index = methodAndP.indexOf(RpcConstants.HIDE_KEY_PREFIX);
if (index <= 0) {
throw ExceptionUtils.buildRuntime(property, "", "Unknown query attribute key!");
}
String methodName = methodAndP.substring(0, index);
String methodProperty = methodAndP.substring(index + 1);
MethodConfig methodConfig = getMethodConfig(methodName);
if (methodConfig != null) {
Method getMethod = ReflectUtils.getPropertyGetterMethod(MethodConfig.class, methodProperty);
// 旧值的类型
Class propertyClazz = getMethod.getReturnType();
oldValue = BeanUtils.getProperty(methodConfig, methodProperty, propertyClazz);
}
} else {
// 接口级配置 例如timeout
// 先通过get方法找到类型
Method getMethod = ReflectUtils.getPropertyGetterMethod(getClass(), property);
// 旧值的类型
Class propertyClazz = getMethod.getReturnType();
// 拿到旧的值
oldValue = BeanUtils.getProperty(this, property, propertyClazz);
}
return oldValue == null ? null : oldValue.toString();
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_QUERY_ATTRIBUTE, property), e);
}
}
Aggregations