Redkale 入门教程 09 -- 文件上传下载

        Redkale 里大量使用了双亲委托模型,序列化的ConvertFactory、依赖注入的ResourceFactory、服务管理的WatchFactory均采用双亲委托模型。用于优先加载自定义的处理类,同时也保证两个同级的子Factory不会相互干扰。

ClassLoader类加载

        双亲委托模型最经典的例子就是JVM的类加载器ClassLoader。每个ClassLoader实例都有一个父类加载器的引用(不是继承关系,是包含关系),虚拟机内置的类加载器(Bootstrap ClassLoader)本身没有父类加载器,但可以用作其它ClassLoader实例的的父类加载器。当一个ClassLoader实例需要加载某个类时,它会试图亲自搜索某个类之前,先把这个任务委托给它的父类加载器,这个过程是由上至下依次检查的,首先由最顶层的类加载器Bootstrap ClassLoader试图加载,如果没加载到,则把任务转交给Extension ClassLoader试图加载,如果也没加载到,则转交给App ClassLoader 进行加载,如果它也没有加载得到的话,则返回给委托的发起者,由它到指定的文件系统或网络等URL中加载该类。如果它们都没有加载到这个类时,则抛出ClassNotFoundException异常。否则将这个找到的类生成一个类的定义,并将它加载到内存当中,最后返回这个类在内存中的Class实例对象。
        ClassLoader采用双亲委托有两个好处:避免类的重复加载和保证类的安全性。由类加载的顺序可以看出父加载器加载过的类在子加载器中不会被重复加载,同时也保证了安全性,一些非系统的class是不可靠的,若定义一个恶意的java.io.File类覆盖JDK自带的类会带来不安全性。而使用双亲委托机制的话该File类永远不会被调用,因为委托BootStrapClassLoader加载后会加载JDK中的File类而不会加载自定义的这个。

Redkale 双亲委托

        ConvertFactory、ResourceFactory、WatchFactory三者的双亲委托模型设计完全一样。下面以ConvertFactory为例说明,ConvertFactory的搜索顺序与ClassLoader相反,ClassLoader为了避免类的重复而先加载父加载器后加载子加载器,ConvertFactory为了优先加载自定义的Encoder和Decoder先搜索自身的ConvertFactory,找不到再从父ConvertFactory中搜索。

public final <E> Encodeable<W, E> findEncoder(final Type type) {
    Encodeable<W, E> rs = (Encodeable<W, E>) encoders.get(type);
    if (rs != null) return rs;
    return this.parent == null ? null : this.parent.findEncoder(type);
}

        当搜索不到Encoder、Decoder时,自身的ConvertFactory会自动创建一个ObjectEncoder、ObjectDecoder。

public final <E> Encodeable<W, E> loadEncoder(final Type type) {
    Encodeable<W, E> encoder = findEncoder(type);
    if (encoder != null) return encoder;
    if (type instanceof GenericArrayType) return new ArrayEncoder(this, type);
    Class clazz;
    if (type instanceof ParameterizedType) {
        final ParameterizedType pts = (ParameterizedType) type;
        clazz = (Class) (pts).getRawType();
    } else if (type instanceof TypeVariable) {
        TypeVariable tv = (TypeVariable) type;
        Type t = Object.class;
        if (tv.getBounds().length == 1) {
            t = tv.getBounds()[0];
        }
        if (!(t instanceof Class)) t = Object.class;
        clazz = (Class) t;
    } else if (type instanceof Class) {
        clazz = (Class) type;
    } else {
        throw new ConvertException("not support the type (" + type + ")");
    }
    encoder = findEncoder(clazz);
    if (encoder != null) return encoder;
    return createEncoder(type, clazz);
}

        大部分情况下Convert的处理对象会根据JavaBean类自定生成,而有些场景需要覆盖处理类,这样需要子ConvertFactory,如 Convert基本用法 例子中使用JsonFactory.root().createChild()重定义。且与JsonFactory.root()中的定义可以并存,也不会产出冲突。

        Redkale可以启动多个协议Server服务(配置文件中含多个server节点),为避免冲突,每个非SNCP的Server的ResourceFactory也是独立的。

public NodeServer(Application application, Server server) {
    this.application = application;
    this.resourceFactory = application.getResourceFactory().createChild();
    this.server = server;
    this.logger = Logger.getLogger(this.getClass().getSimpleName());
}

        双亲委托模型既可让同级子Factory保持独立,也可重用父Factory内的配置,因此在Redkale这种支持多Server、多种配置的场景下很是适合。


        转载请注明出处:https://redkale.org/article_parents.html