[编织消息框架][JAVA核心技术]动态代理应用7-IRpcSend实现

时间:2023-12-05 22:04:50

根据设计生成两个接口,IRpcSend send方法返回数据要求包装成QResult对象

public interface IRpcSend {
public <T> QResult<T> send(byte command, Object... args);
}
public interface IRpcReceive {
public <T> T receive(byte command, Objec[] args);
}
public interface IRpcContext {
RpcContext getContext();
void setContext(RpcContext ctx);
}
public interface QResult<T> extends Future<T> {

    /**
* 是否出错
* */
public boolean isError();
/**
* 获取返回结果
* */
public T getResult();
/**
* 设置结果
* */
public void setResult(T result);
}
 @SuppressWarnings("unchecked")
public abstract class QRpcFactory {
private static Method SEND_METHOD = ReflectUtil.getMethod(IRpcSend.class, "send");
private static Method RECEIVE_METHOD = ReflectUtil.getMethod(IRpcReceive.class, "receive"); private static Map<Short, Class<?>> SEND_CLASS = new HashMap<>();
private static Map<Short, IRpcReceive> RECEIVE = new HashMap<>(); public static <T> T loadSendProxy(Class<T> target, QNode... nodes) {
T ret = loadSendPorxy0(target);
IRpcContext ctx = (IRpcContext) ret;
ctx.setContext(RpcContext.of(nodes));
return ret;
} public static <T> T loadSendProxy(Class<T> target, Long... ids) {
T ret = loadSendPorxy0(target);
IRpcContext ctx = (IRpcContext) ret;
ctx.setContext(RpcContext.of(ids));
return ret;
} public static <T> T loadSendProxy(Class<T> target, String... addresses) {
T ret = loadSendPorxy0(target);
IRpcContext ctx = (IRpcContext) ret;
ctx.setContext(RpcContext.of(addresses));
return ret;
} private static <T> T loadSendPorxy0(Class<T> target) {
QModel modelAnno = ReflectUtil.getAnno(target, QModel.class);
Class<?> proxyClass = SEND_CLASS.get(modelAnno.value());
T ret = null;
try {
ret = (T) proxyClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return ret;
} public static <T> T loadLocalProxy(Class<T> target) {
QModel modelAnno = ReflectUtil.getAnno(target, QModel.class);
Object ret = RECEIVE.get(modelAnno.value());
return (T) ret;
} public static IRpcReceive loadReceiveProxy(short model) {
IRpcReceive ret = RECEIVE.get(model);
return ret;
}
}

QRpcFactory

 public static void registerSendProxy(Class<?> target) {
if (!target.isInterface()) {
throw new RuntimeException("class is not Interface : " + target);
}
QModel modelAnno = ReflectUtil.getAnno(target, QModel.class);
String proxyClassName = target.getCanonicalName() + "$$send$$";
ClassPool classPool = JavassistHepler.classPool;
CtClass ctClass = classPool.makeClass(proxyClassName); try {
// 设置接口
CtClass[] interfaces = new CtClass[3];
interfaces[0] = classPool.get(target.getName());
interfaces[1] = classPool.get(IRpcSend.class.getName());
interfaces[2] = classPool.get(IRpcContext.class.getName());
ctClass.setInterfaces(interfaces); {
// 添加ctx字段
final String ctxName = RpcContext.class.getName();
CtField ctField = new CtField(classPool.get(ctxName), "ctx", ctClass);
ctField.setModifiers(Modifier.PRIVATE);
ctClass.addField(ctField);
// 添加ctx get set 方法
CtMethod ctMethod = CtMethod.make("public " + ctxName + " getContext(){return ctx;}", ctClass);
ctMethod.setModifiers(Modifier.PUBLIC);
ctClass.addMethod(ctMethod); ctMethod = CtMethod.make("public void setContext(" + ctxName + " value){ ctx =value;}", ctClass);
ctMethod.setModifiers(Modifier.PUBLIC);
ctClass.addMethod(ctMethod);
} // 生成send method 调用静态方法减少书写复杂
{
final String body = " return ($r){ " + resultType + QRpcFactory.class.getName() + ".proxySend(this,$2, (short)" + modelAnno.value() + " ,(byte) $1);}";
JavassistHepler.addMethod(ctClass, SEND_METHOD, body);
} // 生成代理方法
for (Method method : target.getDeclaredMethods()) {
QCommond commond = method.getAnnotation(QCommond.class);
if (commond == null) {
continue;
} String resultType = "";
if (void.class != method.getReturnType()) {
resultType = " return ($r) ";
}
final String body = "{ " + resultType + " this.send((byte)" + commond.value() + ",$args); }";
JavassistHepler.addMethod(ctClass, method, body);
} // 保存记录
Class<?> newClass = ctClass.toClass();
ctClass.detach();
SEND_CLASS.put(modelAnno.value(), newClass); } catch (Exception e) {
e.printStackTrace();
}
}
public static QResult proxySend(IRpcContext target, Object[] args, short model, byte commondIndex) {
System.out.println("model : " + model + " commondIndex : " + commondIndex);
return null;
}

测试理论:

    @QModel(1)
public interface TestObject {
@QCommond(1)
public void a(int a, String b); @QCommond(2)
public void setAge(int value); @QCommond(3)
public QResult<Integer> getAge(); @QCommond(4)
public void setObj(TestObject1 obj); @QCommond(5)
public void test(int a,Integer b,Double c ,List<Integer> d,Integer[] e );
} public class TestObject1 {
public int a = 67;
} public static class TestObjectImpl implements TestObject {
private int age; @Override
public void a(int a, String b) {
System.out.println("a : " + a + " " + b);
} @Override
public void setAge(int value) {
age = value;
} @Override
public QResult<Integer> getAge() {
return QResultWrapper.of(age);
} @Override
public void setObj(TestObject1 obj) {
System.out.println(obj.a);
} @Override
public void test(int a, Integer b, Double c ,List<Integer> d,Integer[] e ) { }
}
 public class TestRpcProxy {
@Test
public void testSend() {
QRpcFactory3.registerSendProxy(TestObject.class);
TestObject proxy = QRpcFactory3.loadSendProxy(TestObject.class, 1L); proxy.a(1, "b");
QResult<Integer> ret = proxy.getAge();
}
}

由于发送处理涉及netty,先不考虑通信处理实现,在实现写框架过程,依赖越少实现越简单。所以每个实例对象构造出来时必须能正常工作