釋義
所謂熱部署,就是在套用正在運行的時候升級軟體,卻不需要重新啟動套用。
對於Java應用程式來說,熱部署就是在運行時更新Java類檔案。在基於Java的套用伺服器實現熱部署的過程中,類裝入器扮演著重要的角色。大多數基於Java的套用伺服器,包括EJB伺服器和Servlet容器,都支持熱部署。類裝入器不能重新裝入一個已經裝入的類,但只要使用一個新的類裝入器實例,就可以將類再次裝入一個正在運行的應用程式。
Tomcat的熱部署
Tomcat的熱部署(以後就不用重啟了)
沒有熱部署和有熱部署的開發效率是天差地別的。這個問題還受很多第三方軟體包(Struts,Spring,Hibernate)的限制。本來可以熱部署,加入了第三方的包就不可以了。所以,先說明詳細的軟體環境,和程式配置是非常必要的。
虛擬機:java version "1.5.0_06"
Servlet Engine:Apache Tomcat/5.0.27
Eclipse:3.0.1
Myeclipse:3.8.3
應用程式情況:純正的servlet+jsp+javabean,資料庫連線使用JDBC-ODBC橋連線Access資料庫。沒有使用任何第三方軟體包,沒有使用Struts,Spring,Hibernate。\WebRoot\WEB-INF\lib下是空的。
配置方法:
ie登入http://Tomcat所在的伺服器IP:8080/ -> 點超連線“Tomcat Administration”-> 輸入用戶名密碼登入 ->在左側的功能樹中 -> Tomcat Server -> Service(Catalina) -> Host(localhost) -> Context(/要修改的web項目) ->右側新出現的頁面中 ->Reloadable設定為true -> Save按鈕 -> Commit Changes。
然後Tomcat日誌顯示:
debugging -- changes saved to conf/server.xml
- Removing web application at context path /test
- Processing Context configuration file URL file:D:\Program Files\Tomcat 5.0\conf\Catalina\localhost\test.xml
- Removing web application at context path /admin
- unregistering logger Catalina:type=Logger,path=/admin,host=localhost
- Processing Context configuration file URL file:D:\Program Files\Tomcat 5.0\conf\Catalina\localhost\admin.xml
- Initializing, config="org.apache.struts.util.LocalStrings", returnNull=true
- Initializing, config="org.apache.struts.action.ActionResources", returnNull=true
- Initializing, config="org.apache.webapp.admin.ApplicationResources", returnNull=true
- Removing web application at context path /webdav
- Processing Context configuration file URL file:D:\Program Files\Tomcat 5.0\conf\Catalina\localhost\webdav.xml
- Removing web application at context path /test
- Processing Context configuration file URL file:D:\Program Files\Tomcat 5.0\conf\Catalina\localhost\test.xml
……
這樣,設定就生效了。
開發時,修改.java檔案後,調用這個類時日誌提示:
- Reloading this Context has started
這是正在重新載入修改後的.class檔案。
如果沒有進行這個設定,修改.java檔案後,不拋出異常。系統使用沒有修改的.java檔案繼續運行。
不同版本的Tomcat的配置方法是不同的。這裡使用的是5.0.27
j2ee開發外掛程式(這裡使用Myeclipse),也可能導致熱部署失效。因為外掛程式必須要把編譯好的.class檔案從工作空間複製到Tomcat\webapps下的項目里。才能使Tomcat得到需要熱部署的檔案。
注意:如果項目中加入了Struts,Hibernate,Spring之類的第三方軟體,可能導致熱部署失效。
簡單例子
我的目錄結構是
d://hotdeploy//Client.java
d://hotdeploy//ServerItf.java
d://hotdeploy//server//ServerImpl.java
檔案內容依次為:
//file Client.java
import java . net . URL;
import java . net . URLClassLoader;
import java. io . BufferedReader;
import java . io . InputStreamReader;
public class Client {
static ClassLoader cl;
static ServerItf server;
public static void loadNewVersionOfServer() throws Exception {
URL[] serverURLs = new URL[] { new URL("file://d:/hotdeploy/server/") };
cl = new URLClassLoader(serverURLs);
server = (ServerItf) cl.loadClass("ServerImpl").newInstance();
}
public static void test() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System . in));
loadNewVersionOfServer();
while (true) {
System.out.PRint("Enter QUOTE, RELOAD, GC, or QUIT: ");
String cmdRead = br.readLine();
String cmd = cmdRead.toUpperCase();
if (cmd.equals("QUIT")) {
return;
} else if (cmd.equals("QUOTE")) {
System.out.println(server.getQuote());
} else if (cmd.equals("RELOAD")) {
loadNewVersionOfServer();
} else if (cmd.equals("GC")) {
System.gc();
System.runFinalization( );
}
}
}
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------
public interface ServerItf {
public String getQuote();
}
-------------------------------------------------------------------------
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "i love you";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
運行的命令依次為:
D:\hotdeploy>javac Client.java
D:\hotdeploy>javac ServerItf.java
D:\hotdeploy>javac -cp d:\hotdeploy d:\hotdeploy\server\ServerImpl.java
D:\hotdeploy>java Client
ServerImpl class 1641745 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
i love you
Enter QUOTE, RELOAD, GC, or QUIT:
-------------------------------------------------------------------------
編輯ServerImpl.java為:
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "you love me";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
打開另外一個dos視窗,運行javac -cp d:\hotdeploy d:\hotdeploy\server\ServerImpl.java
-------------------------------------------------------------------------
回到原先的doc視窗,依次運行
Enter QUOTE, RELOAD, GC, or QUIT: reload
ServerImpl class 12677476 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
you love me
Enter QUOTE, RELOAD, GC, or QUIT:
可以看到效果。