Search in sources :

Example 1 with RpcException

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);
    }
}
Also used : Rpc(com.duangframework.core.annotation.rpc.Rpc) RpcException(com.duangframework.core.exceptions.RpcException) IProxy(com.duangframework.core.interfaces.IProxy) RpcException(com.duangframework.core.exceptions.RpcException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 2 with RpcException

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();
        }
    }
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException)

Example 3 with RpcException

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);
    }
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException) RpcException(com.duangframework.core.exceptions.RpcException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 4 with RpcException

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;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) RpcException(com.duangframework.core.exceptions.RpcException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RpcException(com.duangframework.core.exceptions.RpcException)

Example 5 with RpcException

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);
    }
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) RpcException(com.duangframework.core.exceptions.RpcException) CtClass(javassist.CtClass)

Aggregations

RpcException (com.duangframework.core.exceptions.RpcException)11 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)5 NotFoundException (javassist.NotFoundException)4 CtClass (javassist.CtClass)3 RpcAction (com.duangframework.rpc.common.RpcAction)2 Method (java.lang.reflect.Method)2 CtMethod (javassist.CtMethod)2 Service (com.duangframework.core.annotation.mvc.Service)1 Rpc (com.duangframework.core.annotation.rpc.Rpc)1 IProxy (com.duangframework.core.interfaces.IProxy)1 RpcRequest (com.duangframework.rpc.common.RpcRequest)1 RpcResponse (com.duangframework.rpc.common.RpcResponse)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 File (java.io.File)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClassPool (javassist.ClassPool)1