Browse Source

Ddd self-defined annotation to appoint the full name of class in RPC.

pull/1/head
yichen 2 years ago
parent
commit
6a2194b469
  1. 2
      agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java
  2. 2
      agent/src/test/java/Test.java
  3. 16
      service/src/main/java/com/fanruan/RemoteClass.java
  4. 11
      service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java
  5. 2
      service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java
  6. 2
      service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java
  7. 3
      service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java
  8. 2
      service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java
  9. 2
      service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java

2
agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java

@ -40,7 +40,7 @@ public class MyDispatcherImpl implements Dispatcher{
@Override @Override
public Object invokeAsRequest(RpcRequest rpcRequest, BeanCacheImpl beanCache) throws Exception{ public Object invokeAsRequest(RpcRequest rpcRequest, BeanCacheImpl beanCache) throws Exception{
String fullName = DispatcherHelper.transformName(rpcRequest.getServiceClassName()); String fullName = rpcRequest.getServiceClassName();
Class<?> clazz = Class.forName(fullName); Class<?> clazz = Class.forName(fullName);
String methodName = rpcRequest.getMethodName(); String methodName = rpcRequest.getMethodName();
Object[] args = rpcRequest.getArgs(); Object[] args = rpcRequest.getArgs();

2
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]); Class.forName(DBProperties.HSQL[1]);
String[][] DBs = new String[][]{ String[][] DBs = new String[][]{
DBProperties.HSQL, DBProperties.HSQL,

16
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 "";
}

11
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.corundumstudio.socketio.SocketIOClient;
import com.fanruan.RemoteClass;
import com.fanruan.ServerStater; import com.fanruan.ServerStater;
import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientCache;
import com.fanruan.cache.ClientWrapper; import com.fanruan.cache.ClientWrapper;
@ -62,11 +63,19 @@ public class Interceptor implements MethodInterceptor {
rpcRequest.setReply(false) rpcRequest.setReply(false)
.setBinding(false) .setBinding(false)
.setID(Commons.getID()) .setID(Commons.getID())
.setServiceClassName(clazz.getName())
.setMethodName(method.getName()) .setMethodName(method.getName())
.setArgs(objects) .setArgs(objects)
.setArgTypes(getArgTypes(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 // Set whether the rpcResponses of this rpcRequest need to carry return value
if(o instanceof MyResultSet){ if(o instanceof MyResultSet){
boolean flag = InterceptorUtils.isInReplyList(method.getName()); boolean flag = InterceptorUtils.isInReplyList(method.getName());

2
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.corundumstudio.socketio.SocketIOClient;
import com.fanruan.RemoteClass;
import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientCache;
import com.fanruan.servicejdbc.MyDataBaseMetaData; import com.fanruan.servicejdbc.MyDataBaseMetaData;
import com.fanruan.servicejdbc.statement.MyPreparedStatement; import com.fanruan.servicejdbc.statement.MyPreparedStatement;
@ -16,6 +17,7 @@ import java.util.concurrent.Executor;
/** /**
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.connection.MyConnection")
public class MyConnection implements Connection { public class MyConnection implements Connection {
private String ID; private String ID;

2
service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java

@ -1,5 +1,6 @@
package com.fanruan.servicejdbc.driver; package com.fanruan.servicejdbc.driver;
import com.fanruan.RemoteClass;
import com.fanruan.servicejdbc.connection.MyConnection; import com.fanruan.servicejdbc.connection.MyConnection;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
@ -11,6 +12,7 @@ import java.util.logging.Logger;
/** /**
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.driver.MyDriver")
public class MyDriver implements Driver { public class MyDriver implements Driver {
static public final int DRIVER_VERSION_MAJOR = 1; static public final int DRIVER_VERSION_MAJOR = 1;

3
service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java

@ -1,6 +1,8 @@
package com.fanruan.servicejdbc.resultset; package com.fanruan.servicejdbc.resultset;
import com.fanruan.RemoteClass;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -13,6 +15,7 @@ import java.util.Map;
/** /**
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.resultset.MyResultSet")
public class MyResultSet implements ResultSet { public class MyResultSet implements ResultSet {
private String sql; private String sql;

2
service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java

@ -1,5 +1,6 @@
package com.fanruan.servicejdbc.statement; package com.fanruan.servicejdbc.statement;
import com.fanruan.RemoteClass;
import com.fanruan.servicejdbc.resultset.MyResultSet; import com.fanruan.servicejdbc.resultset.MyResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
@ -14,6 +15,7 @@ import java.util.Properties;
/** /**
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.statement.MyPreparedStatement")
public class MyPreparedStatement implements PreparedStatement { public class MyPreparedStatement implements PreparedStatement {
private Properties info; private Properties info;

2
service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java

@ -1,11 +1,13 @@
package com.fanruan.servicejdbc.statement; package com.fanruan.servicejdbc.statement;
import com.fanruan.RemoteClass;
import com.fanruan.servicejdbc.resultset.MyResultSet; import com.fanruan.servicejdbc.resultset.MyResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
@RemoteClass(remoteClassName = "com.fanruan.agentjdbc.statement.MyStatement")
public class MyStatement implements Statement { public class MyStatement implements Statement {
private Properties info; private Properties info;

Loading…
Cancel
Save