|
|
@ -40,7 +40,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException { |
|
|
|
logger.info("server read msg"); |
|
|
|
logger.info("server read msg"); |
|
|
|
System.out.println("收到消息"); |
|
|
|
System.out.println("收到消息"); |
|
|
|
RpcRequest req= (RpcRequest) msg; |
|
|
|
RpcRequest req= (RpcRequest) msg; |
|
|
@ -49,17 +49,26 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { |
|
|
|
response.setMsg("llll"); |
|
|
|
response.setMsg("llll"); |
|
|
|
response.setRequestId(req.getRequestId()); |
|
|
|
response.setRequestId(req.getRequestId()); |
|
|
|
|
|
|
|
|
|
|
|
Class<?> handlerClass = req.getClass(); |
|
|
|
|
|
|
|
System.out.println(req.getMethodName()); |
|
|
|
String classname=req.getClassName(); |
|
|
|
System.out.println(req.getClassName()); |
|
|
|
//获得服务端要调用的方法名称
|
|
|
|
String methodName=req.getMethodName(); |
|
|
|
String methodName=req.getMethodName(); |
|
|
|
|
|
|
|
//获得服务端要调用方法的参数类型
|
|
|
|
Class<?>[] parameterTypes=req.getParameterTypes(); |
|
|
|
Class<?>[] parameterTypes=req.getParameterTypes(); |
|
|
|
Object[] parameters = req.getParameters(); |
|
|
|
//获得服务端要调用方法的每一个参数的值
|
|
|
|
|
|
|
|
Object[] arguments=req.getParameters(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建类
|
|
|
|
|
|
|
|
Class serviceClass=Class.forName(classname); |
|
|
|
|
|
|
|
//创建对象
|
|
|
|
|
|
|
|
Object object = serviceClass.newInstance(); |
|
|
|
|
|
|
|
//获得该类的对应的方法
|
|
|
|
|
|
|
|
Method method=serviceClass.getMethod(methodName, parameterTypes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//该对象调用指定方法
|
|
|
|
|
|
|
|
Object result=method.invoke(object, arguments); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// JDK reflect
|
|
|
|
|
|
|
|
Method method = handlerClass.getMethod(methodName, parameterTypes); |
|
|
|
|
|
|
|
method.setAccessible(true); |
|
|
|
|
|
|
|
Object result = method.invoke(req.getClassName(), parameters); |
|
|
|
|
|
|
|
response.setResult(result); |
|
|
|
response.setResult(result); |
|
|
|
ctx.writeAndFlush(response); |
|
|
|
ctx.writeAndFlush(response); |
|
|
|
} |
|
|
|
} |
|
|
|