• dubbo源码阅读-ProxyFactory(十一)之JavassistFactory


    说明

    详见《dubbo源码阅读-ProxyFactory(十一)之JdkProxyFactory》

    接口定义

    详见《dubbo源码阅读-ProxyFactory(十一)之JdkProxyFactory》

    类图

    JavassistProxyFactory

    /**
     * JavaassistRpcProxyFactory
     */
    public class JavassistProxyFactory extends AbstractProxyFactory {
    
        @Override
        @SuppressWarnings("unchecked")
        public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
            //<1>javasist动态生成Proxy再调用newinstance
            return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
        }
    
        @Override
        public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
            //<2>使用javasisit动态生成Wrapper 感兴趣可以看生成规则
            final Wrapper wrapper = Wrapper.getWrapper(proxy.getClass().getName().indexOf('$') < 0 ? proxy.getClass() : type);
            return new AbstractProxyInvoker<T>(proxy, type, url) {
                @Override
                protected Object doInvoke(T proxy, String methodName,
                                          Class<?>[] parameterTypes,
                                          Object[] arguments) throws Throwable {
                    return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
                }
            };
        }
    
    }

    服务消费者代理

    <1>动态生成Proxy

    Prxy只是一个工厂

    /**
     * @author liqiang
     * @date 2020/3/17 11:27
     * @Description: (what)
     * (why)
     * (how)
     */
    public class Proxy0 extends Proxy {
        /**
         * get instance with special handler.
         *
         * @param handler
         * @return instance.
         */
        @Override
        public Object newInstance(java.lang.reflect.InvocationHandler h) {
    //<3>
    return new proxy0(h); } }

    <3>proxy0

    public class proxy0 implements com.alibaba.dubbo.rpc.service.EchoService, com.alibaba.dubbo.demo.DemoService {
        public static java.lang.reflect.Method[] methods;
        private java.lang.reflect.InvocationHandler handler;
    
        public proxy0(java.lang.reflect.InvocationHandler arg0) {
            handler = arg0;
        }
    
    //我服务接口暴露的方法 @Override
    public java.lang.String sayHello(java.lang.String arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (String) arg0; Object ret = handler.invoke(this, methods[0], args); return (java.lang.String) ret; } //我服务接口暴露的方法 @Override public void save(vo.TestVo arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (vo.TestVo) arg0; Object ret = handler.invoke(this, methods[1], args); } /** * echo test. * * @param message message. * @return message. */ @Override public java.lang.Object $echo(java.lang.Object arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (java.lang.Object) arg0; Object ret = handler.invoke(this, methods[2], args); return (java.lang.Object) ret; } }

    服务发布者代理

    <2>动态生成的Wrapper

    /**
     * @author liqiang
     * @date 2020/3/17 10:56
     * @Description: (what)
     * (why)
     * (how)
     */
    class Wrapper1 extends com.alibaba.dubbo.common.bytecode.Wrapper {
        public static String[] pns;
        public static java.util.Map pts;
        public static String[] mns;
        public static String[] dmns;
        public static Class[] mts0;
        public static Class[] mts1;
    
    
        @Override
        public String[] getPropertyNames() {
            return pns;
        }
    
    
        @Override
        public boolean hasProperty(String n) {
            return pts.containsKey(n);
        }
    
        @Override
        public Class getPropertyType(String n) {
            return (Class) pts.get(n);
        }
    
    
        @Override
        public String[] getMethodNames() {
            return mns;
        }
    
    
        @Override
        public String[] getDeclaredMethodNames() {
            return dmns;
        }
    
    //    /**
    //     * get method name array.
    //     *
    //     * @return method name array.
    //     */
    //    @Override
    //    public String[] getMethodNames() {
    //        return new String[0];
    //    }
    //
    //    /**
    //     * get method name array.
    //     *
    //     * @return method name array.
    //     */
    //    @Override
    //    public String[] getDeclaredMethodNames() {
    //        return new String[0];
    //    }
    
    
        @Override
        public void setPropertyValue(Object o, String n, Object v) {
            provider.DemoServiceImpl w;
            try {
                w = ((provider.DemoServiceImpl) o);
            } catch (Throwable e) {
                throw new IllegalArgumentException(e);
            }
            throw new com.alibaba.dubbo.common.bytecode.NoSuchPropertyException("Not found property "" + n + "" filed or setter method in class provider.DemoServiceImpl.");
        }
    
        @Override
        public Object getPropertyValue(Object o, String n) {
            provider.DemoServiceImpl w;
            try {
                w = ((provider.DemoServiceImpl) o);
            } catch (Throwable e) {
                throw new IllegalArgumentException(e);
            }
            throw new com.alibaba.dubbo.common.bytecode.NoSuchPropertyException("Not found property "" + n + "" filed or setter method in class provider.DemoServiceImpl.");
        }
    
        @Override
        public Object invokeMethod(Object o, String n, Class[] p, Object[] v) throws java.lang.reflect.InvocationTargetException {
            
            //这个是暴露接口的实现类
            provider.DemoServiceImpl w;
            try {
                //复制
                w = ((provider.DemoServiceImpl) o);
            } catch (Throwable e) {
                throw new IllegalArgumentException(e);
            }
            try {
                //以下是我暴露的2个方法
                if ("save".equals(n) && p.length == 1) {
                    w.save((vo.TestVo) v[0]);
                    return null;
                }
                if ("sayHello".equals(n) && p.length == 1) {
                    return (String) w.sayHello((java.lang.String) v[0]);
                }
            } catch (Throwable e) {
                throw new java.lang.reflect.InvocationTargetException(e);
            }
            throw new com.alibaba.dubbo.common.bytecode.NoSuchMethodException("Not found method "" + n + "" in class provider.DemoServiceImpl.");
        }
    }
  • 相关阅读:
    B2C电子商务网站的突围——再议什么是B2C网站,互联网营销 狼人:
    迅雷网站设计浅析,互联网营销 狼人:
    构建一个高性能的网页抓取器,互联网营销 狼人:
    Velocity China 2010大会回顾,互联网营销 狼人:
    尝试使用GraphicsMagick的缩略图功能,互联网营销 狼人:
    美妙的模电2013/4/18
    找素数 素数就是不能再进行等分的整数。比如:7,11。而9不是素数,因为它可以平分为3等份。
    WMI技术介绍和应用——查询硬件信息
    码农如何快速打造一个有设计感的网站 How to Make Your Site Look HalfDecent in Half an Hour
    poj321101背包
  • 原文地址:https://www.cnblogs.com/LQBlog/p/12509644.html
Copyright © 2020-2023  润新知