複製鏈接
請複製以下鏈接發送給好友

熱部署

鎖定
熱部署,就是在應用正在運行的時候升級軟件,卻不需要重新啓動應用。
中文名
熱部署
外文名
Hot deployment

目錄

熱部署釋義

對於Java應用程序來説,熱部署就是在運行時更新Java類文件。在基於Java的應用服務器實現熱部署的過程中,類裝入器扮演着重要的角色。大多數基於Java的應用服務器,包括EJB服務器和Servlet容器,都支持熱部署。類裝入器不能重新裝入一個已經裝入的類,但只要使用一個新的類裝入器實例,就可以將類再次裝入一個正在運行的應用程序。
Tomcat的熱部署
Tomcat的熱部署(以後就不用重啓了)
沒有熱部署和有熱部署的開發效率是天差地別的。這個問題還受很多第三方軟件包(StrutsSpringHibernate)的限制。本來可以熱部署,加入了第三方的包就不可以了。所以,先説明詳細的軟件環境,和程序配置是非常必要的。
虛擬機: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數據庫。沒有使用任何第三方軟件包,沒有使用StrutsSpringHibernate。\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:
可以看到效果。