通过tomcat源码查看其如何实现热部署

前言

看完《通过tomcat源码查看其如何实现应用相互隔离》, 关于tomcat是如何做到热部署的,相信不用说也能猜到个十之八九。简单讲就是定期检查是否需要热部署,如果需要,则将类装载器也重新装载,并且去重新装载其他相关类。

代码分析

首先来看一个后台的定期检查,该定期检查是StandardContext的一个后台线程,会做reload的check,过期session清理等等,这里的modified实际上调用了WebappClassLoader中的方法以判断这个class是不是已经修改。注意到他调用了StandardContext的reload方法。

- public void backgroundProcess() {
-         if (reloadable && modified()) {
-             try {
-                 Thread.currentThread().setContextClassLoader
-                     (WebappLoader.class.getClassLoader());
-                 if (container instanceof StandardContext) {
-                     ((StandardContext) container).reload();
-                 }
-             } finally {
-                 if (container.getLoader() != null) {
-                     Thread.currentThread().setContextClassLoader
-                         (container.getLoader().getClassLoader());
-                 }
-             }
-         } else {
-             closeJARs(false);
-         }
-     }

那么reload方法具体做了什么?非常简单,就是tomcat lifecycle中标准的启停方法stop和start,别忘了,start方法会重新造一个WebappClassLoader并且重复loadOnStartup的过程(查看“ 通过tomcat源码查看tomcat如何实现应用相互隔离 ”),从而重新加载了webapp中的类,注意到一般应用很大时,热部署通常会报outofmemory: permgen space not enough之类的,这是由于之前加载进来的class还没有清除而方法区内存又不够的原因

- public synchronized void reload() {
-
-         // Validate our current component state
-         if (!started)
-             throw new IllegalStateException
-                 (sm.getString("containerBase.notStarted", logName()));
-
-         // Make sure reloading is enabled
-         //      if (!reloadable)
-         //          throw new IllegalStateException
-         //              (sm.getString("standardContext.notReloadable"));
-         if(log.isInfoEnabled())
-             log.info(sm.getString("standardContext.reloadingStarted",
-                     getName()));
-
-         // Stop accepting requests temporarily
-         setPaused(true);
-
-         try {
-             stop();
-         } catch (LifecycleException e) {
-             log.error(sm.getString("standardContext.stoppingContext",
-                     getName()), e);
-         }
-
-         try {
-             start();
-         } catch (LifecycleException e) {
-             log.error(sm.getString("standardContext.startingContext",
-                     getName()), e);
-         }
-
-         setPaused(false);
-
-     }

参考

http://blog.csdn.net/liweisnake/article/details/8470285

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值