cloud.tianai.rpc.demo.api.TestProxy Maven / Gradle / Ivy
package cloud.tianai.rpc.demo.api;
import cloud.tianai.remoting.api.RemotingChannelHolder;
import cloud.tianai.remoting.api.Request;
import cloud.tianai.remoting.api.Response;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class TestProxy implements InvocationHandler {
private RemotingChannelHolder remotingChannelHolder;
private Class> interfaceClass;
public T newProxy(Class interfaceClass, RemotingChannelHolder channelHolder) {
this.remotingChannelHolder = channelHolder;
this.interfaceClass = interfaceClass;
return (T)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class>[]{interfaceClass}, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 包装成Handler
Request request = new Request();
request.setVersion("v1");
request.setRequestParam(args);
request.setMethodName(method.getName());
request.setInterfaceType(interfaceClass);
request.setReturnType(method.getReturnType());
request.setHeartbeat(false);
CompletableFuture