Spring-Loaded实现热更新

原创
2015/09/16 17:14
阅读数 1.9W

1.简介      

Spring-Loaded是基于javaAgent实现的一个代理,更多javaAgent可以参考:
java代理-javassist:http://my.oschina.net/OutOfMemory/blog/309283

官网:https://github.com/spring-projects/spring-loaded
当前的最新版:1.2.4.RELEASE

2.说明

目前最新版1.2.4.RELEASE实现了监听jar包的更新的机制,实现热更新jar包,目前官方提供的版本在linux上可以很好的运行,但在windows还存在bug,官网已经有人提出:https://github.com/spring-projects/spring-loaded/issues/145。如果想在windows上测试可以自己手动改一下源码。

3.参数

-Dspringloaded=watchJars=main.jar:other.jar
即watchJars选项,监听的多个jar之前用:分离

4.实例

以win为例,这里需要手动改一下源码,编译一下
测试目录:

lib:springloaded-1.2.4.RELEASE.jar
main-0.0.1-SNAPSHOT.jar测试的jar包,一般都是游戏逻辑jar包,测试

package org.main;

import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 * 
 */
public class App {
	public static void main(String[] args) throws InterruptedException {
		Reload reload = new Reload();
		while (true) {
			reload.load();
			
			TimeUnit.SECONDS.sleep(2);
		}
	}
}
package org.main;

public class Reload {
	public void load() {
		System.out.println("load.....");
	}
}
run.bat:


java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.1-SNAPSHOT.jar
pause
运行run.bat
D:\springloaded\win>java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify
 -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.
1-SNAPSHOT.jar
SL: [verbose mode on] Full configuration is:verbose;explain;watchJars=main-0.0.1
-SNAPSHOT.jar
SL: [explain mode on] Reporting on the decision making process within SpringLoad
ed
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=org/main/App classloader=Launcher$Ap
pClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun.misc.Launcher$Ap
pClassLoader)
====remainingPrefix=====jar:file:/D:/springloaded/win/main-0.0.1-SNAPSHOT.jar===
=jarname=====main-0.0.1-SNAPSHOT.jar
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor prePro
cess
信息: [explanation] Based on the name, type org/main/App is considered to be rel
oadable
2015-9-16 16:55:32 org.springsource.loaded.ReloadableType <init>
信息: New reloadable type: org.main.App (allocatedId=0) TypeRegistry(id=13256285
29,loader=sun.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.agent.Watcher addFile
信息: Now watching d:\springloaded\win\main-0.0.1-SNAPSHOT.jar
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=0)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: <TypeRegistry.getReloadableType(typeRegistryId=0,typeId=0) returning org.m
ain.App
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=org/springsource/loaded/SSMgr classl
oader=Launcher$AppClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun
.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry couldBeReloadable
信息: WhyNotReloadable? The type org/springsource/loaded/SSMgr is using a packag
e name 'org/springsource/loaded/' which is considered infrastructure and types w
ithin it are not made reloadable
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/lang/InterruptedException class
loader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=org/main/Reload classloader=Launcher
$AppClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun.misc.Launcher
$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor prePro
cess
信息: [explanation] Based on the name, type org/main/Reload is considered to be
reloadable
2015-9-16 16:55:32 org.springsource.loaded.ReloadableType <init>
信息: New reloadable type: org.main.Reload (allocatedId=1) TypeRegistry(id=13256
28529,loader=sun.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=1)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: <TypeRegistry.getReloadableType(typeRegistryId=0,typeId=1) returning org.m
ain.Reload
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=org/springsource/loaded/ISMgr classl
oader=Launcher$AppClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun
.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry couldBeReloadable
信息: WhyNotReloadable? The type org/springsource/loaded/ISMgr is using a packag
e name 'org/springsource/loaded/' which is considered infrastructure and types w
ithin it are not made reloadable
load.....
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit classl
oader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$1 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$2 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$3 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$4 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$5 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$6 clas
sloader=null typeRegistry=null
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=java/util/concurrent/TimeUnit$7 clas
sloader=null typeRegistry=null
load.....
load.....
load.....

修改jar中的类文件,将Reload中的load....改成reload....  重新打包,并覆盖当前的jar,观察日志:

2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher run
信息: Observed last modification time change for d:\springloaded\win\main-0.0.1-
SNAPSHOT.jar (lastScanTime=1442393851501)
2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher 
......省略
reload.....
reload.....

热更新完成!

参考:http://www.blogjava.net/landon/archive/2015/07/01/425994.html

展开阅读全文
加载中
点击加入讨论🔥(6) 发布并加入讨论🔥
6 评论
16 收藏
0
分享
返回顶部
顶部