Java热部署

所谓热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。

  对于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   应用程序情况:纯正的sertlet+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只类的第三方软件,可能导致热部署失效。

java热部署的简单例子

  我的目录结构是   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:   可以看到效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值