見:http://blog.sina.com.cn/s/blog_69398ed9010191jg.html
另:http://ekisstherain.iteye.com/blog/1701463
jrebel 、JavaRebel是什么,見另一博客:jrebel/JavaRebel
開發環境
1.?
2.?
3.?
4.?
5.?
?
第一步:修改struts.properties來實現熱加載Struts2的配置文件
(注:開發環境是在Struts2下)
在src目錄下新建一個文件struts.properties,打開編輯,加入以下語句
#Whether Struts is in development mode or not
struts.devMode=true
#Whether the localization messages should automatically be reloaded
struts.i18n.reload=true
#Whether to reload the XML configuration or not
struts.configuration.xml.reload=true
上面的語句分別為是否為struts開發模式、是否國際化信息自動加載、是否加載xml配置(true,false),修改后重啟服務器后,就能體現效果。在我們修改Struts2的配置信息的時候就不需再重啟服務器了,而且Struts2的配置還包括include標簽(防止配置文件膨脹),例如下面的配置:
?
?
?
上面的struts.properties會對每個配置文件都產生作用。
對于上面的測試,我實際上發現:上面的配置在純Struts2開發中,是可以實現對配置文件和國際化文件熱加載功能的,但我在SSH開發中并不能實現功能,而只能實現對國際化的熱加載,不能對配置文件的熱加載。
?
第二步:解決類的熱加載
?
方法一:修改MyEclipse 中的Tomcat配置,加入JRebel的應用
既然要使用JRebel,那就要我們下載JRebel,到JRebel的官網上面下載最新的JRebel包,需要說明的是JRebel并非免費的產品,提供30天的試用期限,不過網上可以搜索到破解版的喲。
下載后解壓,就可以看到jrebel.jar了。
使用配置?
Jrebel的使用一般都依賴于服務器,就那tomcat來說,主要是配置JDK的Optional Java VM arguments(打開Myeclipse中的windows->Preferences->MyEclipse …->Servers->Tomcat->
選擇配置的Tomcat,單擊,展開JDK選項),加入下面的語句
-noverify -javaagent:D:\Jars\jrebel-2.1a\jrebel.jar
說明一下,“-noverify -javaagent:”是固定的,后面加上的是jrebel.jar在你電腦上面的物理路徑,請修改為你的電腦配置,不然不會成功。
這一步是為了解決Java類文件熱加載的問題,實際上,有一個更簡單的方法吧,并不需要使用JRebel包,而是我們進行適當的配置
?
方法二:
自動加載修改后的項目不需重啟服務器(只對Java文件,對配置文件不可以)
在WebRoot下的META-INF文件夾中新建一個名為context.xml文件,在里面寫
"true">
注意大小寫
方法二的思想是源自張孝祥老師的講解,不需要修改tomcat的配置,減少tomcat的啟動時間,最重要的是,能夠完成一樣的作用——對Java類進行熱加載,但是有的時候,特別是在SSH開發中,會出現異常,建議不要使用這種方法,而是使用第一種方法使用JRebel。
?
這兩步下來, 可以在新加入類以及Struts配置修改后, 完全無需重啟或者重新發布即可立即測試! 在JAR包暴多的情況下, 可以讓我們不再等待10到20秒了。
?
第三步:測試是否有效
測試類的熱加載(具體的測試請大家完成,我僅貼出我的測試結果)
我在一個已知的登錄系統中,加入一個自己留的后臺用戶,不重啟服務器登錄
if("TestJRebel".equals(operator.getOperatorName()))
?
?
?
?
另附:jrebel熱部署配置
1. 配置tomcat服務器:修改tomcat的部署配置為:從不自動發布,禁用Web模塊的自動從新裝入為:禁用
2. 在tomcat運行配置上添加vm參數:-noverify -javaagent:D:\RUNTIME\jrebel\jrebel.jar -Dmyproject.root=D:/project/operamasks/workspaces/xxxx -Drebel.disable_update=true
?? 其中:-javaagent的值表示jrebel的jar包路徑
?? -Dmyproject.root的值表示當前的需要熱部署的工程路徑
3. 在工程的src源文件夾下添加名稱為:rebel.xml的配置文件,內容格式如下:
?
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <application?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://www.zeroturnaround.com"?xsi:schemaLocation="http://www.zeroturnaround.com?http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">??
- ????<classpath>??
- ????????<dir?name="${myproject.root}/bin">??
- ????????</dir>??
- ????????<dir?name="${myproject.root}/web/WebContent/WEB-INF/classes">??
- ????????</dir>??
- ????</classpath>??
- ????<web>??
- ????????<link?target="/">??
- ????????????<dir?name="${myproject.root}/web/WebContent">??
- ????????????</dir>??
- ????????</link>??
- ????</web>??
- </application>??
?
?
其中:myproject.root就是tomcat vm參數中指定的工程路徑,你也可以使用絕對路徑
這個文件定義的就是jrebel要監控的具體文件夾,即class文件和資源文件(比如:jsp等)
4.最后,啟動tomcat服務器的成功提示:
?
Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.hD#new V( false )
Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.bH#public boolean a(byte abyte0[])
#############################################################
?JRebel 3.0-M1 (200910151623)
?(c) Copyright ZeroTurnaround, Ltd, 2007-2009. All rights reserved.
?A rough estimate: Over the last 5 days JRebel?
?prevented the need for at least 36 redeploys/restarts.
?Using industry standard build and redeploy times,?
?JRebel saved you between 1 and 2 hours.
?This product is licensed to? Java Hack Organization
?for unlimited number of developer seats on site.
#############################################################
JRebel: A newer version '5.0.1' is available for download?
JRebel: from?http://www.zeroturnaround.com/download.
2012-10-19 10:34:29 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\RUNTIME\java\jdk1.6.0_11\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin/client;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;D:\RUNTIME\java\jdk1.6.0_11\bin;D:\RUNTIME\java\jdk1.6.0_11\jre\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;D:\RUNTIME\MySQL\MySQL Server 7.0\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\MacType;C:\PROGRA~1\DISKEE~1\DISKEE~1\;d:\Program Files (x86)\DBank\ClickUp;C:\Program Files (x86)\DBank\ClickUp
2012-10-19 10:34:29 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:XXX' did not find a matching property.
2012-10-19 10:34:29 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2012-10-19 10:34:29 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 324 ms
2012-10-19 10:34:29 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2012-10-19 10:34:29 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.35
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.
=============================== [JRebel Spring Framework Plugin] ===============================
Plugins are contributed by third party and can cause compatibility problems.
If you have any troubles set -Drebel.spring_plugin=false to disable it.
------------------------------------------------------------------------------------------
Description: Supports adding new beans and adding new bean dependencies using
annotations or XML. Singletons will be reconfigured after the change. It also
supports adding or changing Spring MVC controllers or handlers.
=============================== [/JRebel Spring Framework Plugin] ==============================
=============================== [JRebel AspectJ Plugin DISABLED] ==============================
You can enable AspectJ Plugin by setting -Drebel.aspectj_plugin=true.
------------------------------------------------------------------------------------------
Description: Allows the AspectJ load-time weaving (javaagent launched by aj5 or
-javaagent:aspectjweaver.jar) to be used with JavaRebel. Note that AspectJ
weaver will still show "java.lang.Exception: AspectJ5 does not weave hotswapped
class" in the beginning, but this can be safely ignored.
=============================== [/JRebel AspectJ Plugin DISABLED] =============================
2012-10-19 10:34:30 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.
...
?
?
到此為止,你成功配置jrebel了。
值得注意的是:
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.
和
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.
表示在運行期間jrebel會監控這些位置的class文件和資源文件的變化情況!