use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.
the class RpcFactory method initClient.
/**
* 初始化RPC客户端,即消费者,生成代理类
* @param classSet
*/
public static void initClient(Set<Class<?>> classSet) {
if (ToolsKit.isEmpty(classSet)) {
return;
}
try {
for (Class<?> clientCls : classSet) {
if (!clientCls.isInterface()) {
continue;
}
List<IProxy> proxyList = new ArrayList<>();
proxyList.add(new RpcClientProxy());
Object proxyObj = ProxyManager.createProxy(clientCls, proxyList);
if (null != proxyObj) {
// 再将该代理类存放在BeanUtils里,让框架在执行IocHepler时注入到对应的Service
BeanUtils.setBean2Map(clientCls, proxyObj);
// 取出产品代号
Rpc rpc = clientCls.getAnnotation(Rpc.class);
watchNodePath.add(RpcUtils.getZookNotePath(rpc.productcode()));
}
}
} catch (Exception e) {
throw new RpcException("RpcFactory newProxyInstance is fail : " + e.getMessage(), e);
}
}
use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.
the class JdkSerializableUtil method deserialize.
private static <T> T deserialize(final InputStream inputStream) {
if (inputStream == null) {
throw new IllegalArgumentException("The InputStream must not be null");
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(inputStream);
@SuppressWarnings("unchecked") final T obj = (T) in.readObject();
return obj;
} catch (final ClassCastException ex) {
throw new RpcException(ex);
} catch (final ClassNotFoundException ex) {
throw new RpcException(ex);
} catch (final IOException ex) {
throw new RpcException(ex);
} finally {
try {
if (in != null) {
in.close();
}
} catch (final IOException ex) {
ex.printStackTrace();
}
}
}
use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.
the class RpcUtils method autoCreateBatchInterface.
/**
* 自动创建Service类的接口文件
* RPC模块文件夹路径,即是接口文件存在的父目录
*/
public static void autoCreateBatchInterface() throws Exception {
try {
// 自定义目录
String customizeDir = ConfigKit.duang().key("rpc.module.customdir").defaultValue("provider").asString();
AutoBuildServiceInterface.createBatchInterface(RpcUtils.getRpcModulePath(customizeDir), customizeDir);
} catch (Exception e) {
throw new RpcException(e.getMessage(), e);
}
}
use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.
the class RpcClient method call.
/**
* 调用运程方法
* @param request rpc请求对象,封装请求的参数,参数类型等
* @param action rpc生产者对象,封装了生产者或者说是要调用的方法的类名,方法名,IP地址,端口等
* @return 处理结果 RpcResponse对象
* @throws Exception
*/
public RpcResponse call(RpcRequest request, RpcAction action) throws Exception {
// 这里要用内网的IP地址
Channel channel = getChannel(action.getIntranetip(), action.getPort());
RpcResponse response = null;
// 先初始化一个以请求ID为KEY的响应阵列到集合中
final String requestId = request.getRequestId();
// 将请求结果预存到MAP中,以请求ID为key
RESPONSE_MAP.put(requestId, new LinkedBlockingQueue<RpcResponse>(1));
try {
if (channel.isOpen()) {
MessageHolder<RpcRequest> messageHolder = new MessageHolder<RpcRequest>(Protocol.REQUEST, Protocol.OK, request);
ChannelFuture writeFuture = channel.writeAndFlush(messageHolder).sync();
writeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
// 如果返回正常,则直接退出
if (future.isSuccess()) {
return;
} else {
String errorMsg = "Send request[" + requestId + "] to [" + future.channel().toString() + "] is error: " + future.cause().toString();
throw new RpcException(errorMsg);
}
}
});
} else {
logger.warn("channel.isClose: " + channel.remoteAddress());
}
response = getRpcResponse(requestId, request.getTimeout());
if (null != response) {
logger.warn("poll time: " + (System.currentTimeMillis() - response.getRequestStartTime()) + " ms");
}
} catch (Exception e) {
response = createExceptionRpcResponse(action, e, requestId);
} finally {
// 无论成功与否,都必须移除集合中指定KEY的队列
RESPONSE_MAP.remove(requestId);
}
return response;
}
use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.
the class AutoBuildServiceInterface method toGenericString.
/**
* 构建方法内容字符串,包括方法名,参数名<br/>
* @param method 方法对象
* @param variableNames 参数集合
* @return
*/
private static String toGenericString(Method method, List<String> variableNames) {
try {
StringBuilder sb = new StringBuilder();
int mod = method.getModifiers() & Modifier.methodModifiers();
if (mod != 0) {
sb.append(Modifier.toString(mod)).append(" ");
}
TypeVariable<?>[] typeparms = method.getTypeParameters();
if (ToolsKit.isNotEmpty(typeparms) && typeparms.length > 0) {
boolean first = true;
sb.append("<");
for (TypeVariable<?> typeparm : typeparms) {
if (!first) {
sb.append(",");
}
sb.append(typeparm.toString());
first = false;
}
sb.append("> ");
}
// 方法的返回类型
Type genRetType = method.getGenericReturnType();
sb.append(((genRetType instanceof Class<?>) ? getTypeName((Class<?>) genRetType) : genRetType.toString())).append(" ");
sb.append(getTypeName(method.getDeclaringClass())).append(".");
sb.append(method.getName()).append("(");
Type[] params = method.getGenericParameterTypes();
if (ToolsKit.isNotEmpty(params)) {
for (int j = 0; j < params.length; j++) {
String param = (params[j] instanceof Class) ? getTypeName((Class) params[j]) : params[j].toString();
if (method.isVarArgs() && (j == params.length - 1)) {
param = param.replaceFirst("\\[\\]$", "...");
}
sb.append(param);
if (j < (params.length - 1)) {
sb.append(" " + variableNames.get(j) + ", ");
}
}
sb.append(" " + variableNames.get(params.length - 1));
}
sb.append(")");
Type[] exceptions = method.getGenericExceptionTypes();
if (ToolsKit.isNotEmpty(exceptions) && exceptions.length > 0) {
sb.append(" throws ");
for (int k = 0; k < exceptions.length; k++) {
sb.append((exceptions[k] instanceof Class) ? ((Class) exceptions[k]).getName() : exceptions[k].toString());
if (k < (exceptions.length - 1)) {
sb.append(",");
}
}
}
return (sb.length() > -1) ? sb.toString() : "";
} catch (Exception e) {
throw new RpcException(e.getMessage(), e);
}
}
Aggregations