From 6a2194b469ee5ca8edec557ffb5287ea45aff116 Mon Sep 17 00:00:00 2001 From: yichen Date: Fri, 19 Aug 2022 11:09:19 +0800 Subject: [PATCH] Ddd self-defined annotation to appoint the full name of class in RPC. --- .../com/fanruan/handler/MyDispatcherImpl.java | 2 +- agent/src/test/java/Test.java | 2 +- .../src/main/java/com/fanruan/RemoteClass.java | 16 ++++++++++++++++ .../fanruan/proxy/interceptor/Interceptor.java | 11 ++++++++++- .../servicejdbc/connection/MyConnection.java | 2 ++ .../com/fanruan/servicejdbc/driver/MyDriver.java | 2 ++ .../servicejdbc/resultset/MyResultSet.java | 3 +++ .../statement/MyPreparedStatement.java | 2 ++ .../servicejdbc/statement/MyStatement.java | 2 ++ 9 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 service/src/main/java/com/fanruan/RemoteClass.java diff --git a/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java b/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java index 389ce48..454fbf0 100644 --- a/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java +++ b/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java @@ -40,7 +40,7 @@ public class MyDispatcherImpl implements Dispatcher{ @Override public Object invokeAsRequest(RpcRequest rpcRequest, BeanCacheImpl beanCache) throws Exception{ - String fullName = DispatcherHelper.transformName(rpcRequest.getServiceClassName()); + String fullName = rpcRequest.getServiceClassName(); Class clazz = Class.forName(fullName); String methodName = rpcRequest.getMethodName(); Object[] args = rpcRequest.getArgs(); diff --git a/agent/src/test/java/Test.java b/agent/src/test/java/Test.java index 1f09520..8b25a12 100644 --- a/agent/src/test/java/Test.java +++ b/agent/src/test/java/Test.java @@ -17,7 +17,7 @@ public class Test { } - static void testStart() throws IOException, ClassNotFoundException { + static void testStart() throws ClassNotFoundException { Class.forName(DBProperties.HSQL[1]); String[][] DBs = new String[][]{ DBProperties.HSQL, diff --git a/service/src/main/java/com/fanruan/RemoteClass.java b/service/src/main/java/com/fanruan/RemoteClass.java new file mode 100644 index 0000000..7992e2f --- /dev/null +++ b/service/src/main/java/com/fanruan/RemoteClass.java @@ -0,0 +1,16 @@ +package com.fanruan; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Yichen Dai + * @date 2022/8/19 10:50 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface RemoteClass { + public String remoteClassName() default ""; +} diff --git a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java b/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java index 6cb2b90..30c9d5a 100644 --- a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java +++ b/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java @@ -2,6 +2,7 @@ package com.fanruan.proxy.interceptor; import com.corundumstudio.socketio.SocketIOClient; +import com.fanruan.RemoteClass; import com.fanruan.ServerStater; import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientWrapper; @@ -62,11 +63,19 @@ public class Interceptor implements MethodInterceptor { rpcRequest.setReply(false) .setBinding(false) .setID(Commons.getID()) - .setServiceClassName(clazz.getName()) .setMethodName(method.getName()) .setArgs(objects) .setArgTypes(getArgTypes(objects)); + // get RPC class name from annotation + // if null, send the name of this clazz + if(clazz.isAnnotationPresent(RemoteClass.class)){ + RemoteClass annotation = clazz.getAnnotation(RemoteClass.class); + rpcRequest.setServiceClassName(annotation.remoteClassName()); + }else{ + rpcRequest.setServiceClassName(clazz.getName()); + } + // Set whether the rpcResponses of this rpcRequest need to carry return value if(o instanceof MyResultSet){ boolean flag = InterceptorUtils.isInReplyList(method.getName()); diff --git a/service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java b/service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java index 90ac92b..90f1ddf 100644 --- a/service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java +++ b/service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java @@ -2,6 +2,7 @@ package com.fanruan.servicejdbc.connection; import com.corundumstudio.socketio.SocketIOClient; +import com.fanruan.RemoteClass; import com.fanruan.cache.ClientCache; import com.fanruan.servicejdbc.MyDataBaseMetaData; import com.fanruan.servicejdbc.statement.MyPreparedStatement; @@ -16,6 +17,7 @@ import java.util.concurrent.Executor; /** * @author Yichen Dai */ +@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.connection.MyConnection") public class MyConnection implements Connection { private String ID; diff --git a/service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java b/service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java index 509eb2b..591b3a8 100644 --- a/service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java +++ b/service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java @@ -1,5 +1,6 @@ package com.fanruan.servicejdbc.driver; +import com.fanruan.RemoteClass; import com.fanruan.servicejdbc.connection.MyConnection; import com.fanruan.proxy.ProxyFactory; @@ -11,6 +12,7 @@ import java.util.logging.Logger; /** * @author Yichen Dai */ +@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.driver.MyDriver") public class MyDriver implements Driver { static public final int DRIVER_VERSION_MAJOR = 1; diff --git a/service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java b/service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java index 3ab5d8d..4cad91f 100644 --- a/service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java +++ b/service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java @@ -1,6 +1,8 @@ package com.fanruan.servicejdbc.resultset; +import com.fanruan.RemoteClass; + import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; @@ -13,6 +15,7 @@ import java.util.Map; /** * @author Yichen Dai */ +@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.resultset.MyResultSet") public class MyResultSet implements ResultSet { private String sql; diff --git a/service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java b/service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java index d841182..6a323f0 100644 --- a/service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java +++ b/service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java @@ -1,5 +1,6 @@ package com.fanruan.servicejdbc.statement; +import com.fanruan.RemoteClass; import com.fanruan.servicejdbc.resultset.MyResultSet; import com.fanruan.proxy.ProxyFactory; @@ -14,6 +15,7 @@ import java.util.Properties; /** * @author Yichen Dai */ +@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.statement.MyPreparedStatement") public class MyPreparedStatement implements PreparedStatement { private Properties info; diff --git a/service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java b/service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java index d26b5ab..4fa1bec 100644 --- a/service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java +++ b/service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java @@ -1,11 +1,13 @@ package com.fanruan.servicejdbc.statement; +import com.fanruan.RemoteClass; import com.fanruan.servicejdbc.resultset.MyResultSet; import com.fanruan.proxy.ProxyFactory; import java.sql.*; import java.util.Properties; +@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.statement.MyStatement") public class MyStatement implements Statement { private Properties info;