play中的热启动

原创
2014/01/21 17:30
阅读数 368

java启动参数:

-javaagent:"../play-1.2.4/framework/play-1.2.4.jar"

play-1.2.4.jar元信息文件:

Premain-Class: play.classloading.HotswapAgent
Can-Redefine-Classes: true

HotswapAgent类:

public class HotswapAgent {

    static Instrumentation instrumentation;
    public static boolean enabled = false;

    public static void premain(String agentArgs, Instrumentation instrumentation) {
        HotswapAgent.instrumentation = instrumentation;
        HotswapAgent.enabled = true;
    }

    public static void reload(ClassDefinition... definitions) throws UnmodifiableClassException, ClassNotFoundException {
        instrumentation.redefineClasses(definitions);
    }
}

通过java代理,使得虚拟机在启动后,具备了热加载的能力。


自定义类装载器

public class ApplicationClassloader extends ClassLoader {

    public void detectChanges() {

        for (ApplicationClass applicationClass : modifiedWithDependencies) {
            if (applicationClass.compile() == null) {   《=======先编译
            } else {
                applicationClass.enhance();                  《=======后增强


        if (newDefinitions.size() > 0) {
            Cache.clear();
            if (HotswapAgent.enabled) {
                try {
                    HotswapAgent.reload(newDefinitions.toArray(new ClassDefinition[newDefinitions.size()]));  《=======然后再加载,完成一次热启动                } catch (Throwable e) {
                    throw new RuntimeException("Need reload");
                }
            } else {
                throw new RuntimeException("Need reload");
            }
        }

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部