Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In order to support multiple parameters of lambda, is this too compli… #37

Merged
merged 1 commit into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Created by luxiaoxun on 2016-03-16.
*/
public class ObjectProxy<T,P> implements InvocationHandler, RpcService<T,P> {
public class ObjectProxy<T, P> implements InvocationHandler, RpcService<T, P, SerializableFunction<T>> {
private static final Logger logger = LoggerFactory.getLogger(ObjectProxy.class);
private Class<T> clazz;
private String version;
Expand Down Expand Up @@ -77,10 +77,10 @@ public RpcFuture call(String funcName, Object... args) throws Exception {
}

@Override
public RpcFuture call(RpcFunction<T, P> fn, Object... args) throws Exception {
public RpcFuture call(SerializableFunction<T> tSerializableFunction, Object... args) throws Exception {
String serviceKey = ServiceUtil.makeServiceKey(this.clazz.getName(), version);
RpcClientHandler handler = ConnectionManager.getInstance().chooseHandler(serviceKey);
RpcRequest request = createRequest(this.clazz.getName(), fn.getName(), args);
RpcRequest request = createRequest(this.clazz.getName(), tSerializableFunction.getName(), args);
RpcFuture rpcFuture = handler.sendRequest(request);
return rpcFuture;
}
Expand All @@ -92,7 +92,6 @@ private RpcRequest createRequest(String className, String methodName, Object[] a
request.setMethodName(methodName);
request.setParameters(args);
request.setVersion(version);

Class[] parameterTypes = new Class[args.length];
// Get the right class type
for (int i = 0; i < args.length; i++) {
Expand All @@ -117,26 +116,6 @@ private RpcRequest createRequest(String className, String methodName, Object[] a

private Class<?> getClassType(Object obj) {
Class<?> classType = obj.getClass();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方,其实有个问题:就是服务端接口定义的时候是Integer,而客户端调用的时候如果传的是int,是匹配不上的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从jdk相关反射显示上来看,func(Integer i) 跟func(int i) 这是两个方法,所以如果服务端跟客户端方法签名不一致会导致服务端找不到调用方法。

String typeName = classType.getName();
switch (typeName) {
case "java.lang.Integer":
return Integer.TYPE;
case "java.lang.Long":
return Long.TYPE;
case "java.lang.Float":
return Float.TYPE;
case "java.lang.Double":
return Double.TYPE;
case "java.lang.Character":
return Character.TYPE;
case "java.lang.Boolean":
return Boolean.TYPE;
case "java.lang.Short":
return Short.TYPE;
case "java.lang.Byte":
return Byte.TYPE;
}

return classType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package com.netty.rpc.client.proxy;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

/**
* lambda method reference
* g-yu
*
* @param <T>
* @param <P>
*/
@FunctionalInterface
public interface RpcFunction<T, P> extends Serializable {
public interface RpcFunction<T, P> extends SerializableFunction<T> {
/**
* have parameter
*
* @param t
* @param p
* @return
*/
P apply(T t, P p);
Object apply(T t, P p);


default String getName() throws Exception {
Method write = this.getClass().getDeclaredMethod("writeReplace");
write.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) write.invoke(this);
return serializedLambda.getImplMethodName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.netty.rpc.client.proxy;
@FunctionalInterface
public interface RpcFunction2<T, P1, P2> extends SerializableFunction<T> {
Object apply(T t, P1 p1, P2 p2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Created by luxiaoxun on 2016/3/16.
* @author g-yu
*/
public interface RpcService<T,P> {
public interface RpcService<T,P,FN extends SerializableFunction<T>> {
RpcFuture call(String funcName, Object... args) throws Exception;

/**
Expand All @@ -16,6 +16,6 @@ public interface RpcService<T,P> {
* @return
* @throws Exception
*/
RpcFuture call(RpcFunction<T, P> fn, Object... args) throws Exception;
RpcFuture call(FN fn, Object... args) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.netty.rpc.client.proxy;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

public interface SerializableFunction<T> extends Serializable {
default String getName() throws Exception {
Method write = this.getClass().getDeclaredMethod("writeReplace");
write.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) write.invoke(this);
return serializedLambda.getImplMethodName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface HelloService {
String hello(String name);

String hello(Person person);

String substring(String str, Integer indexOf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public String hello(String name) {
public String hello(Person person) {
return "Hello " + person.getFirstName() + " " + person.getLastName();
}

@Override
public String substring(String str, Integer indexOf) {
return str.substring(0, indexOf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public String hello(String name) {
public String hello(Person person) {
return "Hi " + person.getFirstName() + " " + person.getLastName();
}

@Override
public String substring(String str, Integer indexOf) {
return str.substring(0,indexOf) + "v2";
}
}
9 changes: 8 additions & 1 deletion netty-rpc-test/src/test/java/com/app/test/ServiceTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.app.test.service.HelloService;
import com.netty.rpc.client.RpcClient;
import com.netty.rpc.client.handler.RpcFuture;
import com.netty.rpc.client.proxy.RpcFunction;
import com.netty.rpc.client.proxy.RpcFunction2;
import com.netty.rpc.client.proxy.RpcService;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -28,8 +30,13 @@ public void say() {

@Test
public void mr() throws Exception {
RpcService<HelloService, String> helloService = rpcClient.createAsyncService(HelloService.class, "1.0");
RpcService<HelloService, String, RpcFunction<HelloService,String>> helloService = rpcClient.createAsyncService(HelloService.class, "1.0");
RpcFuture result = helloService.call(HelloService::hello, "World");
Assert.assertEquals("Hello World", result.get());

RpcService<HelloService, String, RpcFunction2<HelloService, String, Integer>> helloServicev1 = rpcClient.createAsyncService(HelloService.class, "1.0");
RpcFuture resultv1 = helloServicev1.call(HelloService::substring, "World", 2);
Assert.assertEquals("Wo", resultv1.get());

}
}