tomcat的热部署实现原理:tomcat启动的时候会有启动一个线程每隔一段时间会去判断应用中加载的类是否发生变法(类总数的变化,类的修改),如果发生了变化就会把应用的启动的线程停止掉,清除引用,并且把加载该应用的WebappClassLoader设为null,然后创建一个新的WebappClassLoader来重新加载应用。 tomcat中热部署发现类变法之后要做的一系列停止工作的时序图如下:


 

上面时序图中只把关键的流转步骤画出来了,还有一些细节的处理没有完全画出来,这部分代码的继承的结构还是比较复杂的,debug上面的流程的时候,各种跳转。先解释一下上面的时序图,这个时序图是从WebappLoader的backgroundProcess()方法开始的,backgroundProcess()就是随tomcat启动的时候启动的线程中其中的一方法,是tomcat热部署代码的入口。 其中最关键的两步。就是WebappLoader的stopInternal()方法和WebappClassLoader的stop()方法。 在WebappLoader的stopInternal()中

protected void stopInternal() throws LifecycleException {
…..
// Throw away our current class loader
((Lifecycle) classLoader).stop();
……..
classLoader = null;
}

在 ((Lifecycle) classLoader).stop(); 中classLoader是WebappClassLoader的实例。也就是调用了WebappClassLoader的stop()方法。如下:

public void stop() throws LifecycleException {
// Clearing references should be done before setting started to
// false, due to possible side effects
clearReferences(); started = false; int length = files.length;
for (int i = 0; i < length; i++) {
files[i] = null;
} length = jarFiles.length;
for (int i = 0; i < length; i++) {
try {
if (jarFiles[i] != null) {
jarFiles[i].close();
}
} catch (IOException e) {
// Ignore
}
jarFiles[i] = null;
} notFoundResources.clear();
resourceEntries.clear();
resources = null;
repositories = null;
repositoryURLs = null;
files = null;
jarFiles = null;
jarRealFiles = null;
jarPath = null;
jarNames = null;
lastModifiedDates = null;
paths = null;
hasExternalRepositories = false;
parent = null; permissionList.clear();
loaderPC.clear(); if (loaderDir != null) {
deleteDir(loaderDir);
} }

可以看到stop()方法做了很多清除引用的工作。其中 clearReferences() 中

  protected void clearReferences() {
// De-register any remaining JDBC drivers
clearReferencesJdbc(); // Stop any threads the web application started
clearReferencesThreads(); // Check for leaks triggered by ThreadLocals loaded by this class loader
checkThreadLocalsForLeaks(); // Clear RMI Targets loaded by this class loader
clearReferencesRmiTargets(); // Null out any static or final fields from loaded classes,
// as a workaround for apparent garbage collection bugs
if (clearReferencesStatic) {
clearReferencesStaticFinal();
} // Clear the IntrospectionUtils cache.
IntrospectionUtils.clear(); // Clear the classloader reference in common-logging
if (clearReferencesLogFactoryRelease) {
org.apache.juli.logging.LogFactory.release(this);
} // Clear the resource bundle cache
// This shouldn’t be necessary, the cache uses weak references but
// it has caused leaks. Oddly, using the leak detection code in
// standard host allows the class loader to be GC’d. This has been seen
// on Sun but not IBM JREs. Maybe a bug in Sun’s GC impl?
clearReferencesResourceBundles(); // Clear the classloader reference in the VM’s bean introspector
java.beans.Introspector.flushCaches(); }

还进行了清除应用线程等工作。最后在WebappClassLoader的stopInternal()方法中执行了 classLoader = null; 那这个类加载器的实例就没有被引用了。 最后调用WebappLoader中的startInternal()方法,创建新的WebappClassLoader实例,然后开始重新加载应用。到此tomcat的热部署流程就完成了。

tomcat源码解读(1)–tomcat热部署实现原理的更多相关文章

  1. 【Tomcat 源码系列】Tomcat 整体结构

    一,前言 在开始看源码细节之前,首先要想好要看的问题.想好问题之后,我们该如何寻找要看的代码呢? 其实,这就好像去爬山的时候,突然想去上厕所,如果有一副地图,那么我们可以很快就找到厕所的位置.带着问题 ...

  2. tomcat源码--springboot整合tomcat源码分析

    1.测试代码,一个简单的springboot web项目:地址:https://gitee.com/yangxioahui/demo_mybatis.git 一:tomcat的主要架构:1.如果我们下 ...

  3. Vue 源码解读(3)—— 响应式原理

    前言 上一篇文章 Vue 源码解读(2)-- Vue 初始化过程 详细讲解了 Vue 的初始化过程,明白了 new Vue(options) 都做了什么,其中关于 数据响应式 的实现用一句话简单的带过 ...

  4. Tomcat源码解读系列(一)——server.xml文件的配置

    Tomcat是J2EE开发人员最常用到的开发工具,在Java Web应用的调试开发和实际部署中,我们都可以看到Tomcat的影子.大多数时候,我们可以将Tomcat当做一个黑盒来看待,只需要将编写的J ...

  5. Tomcat源码解读:ClassLoader的设计

    Tomcat是一个经典的web server,学习tomcat的源码对于我们是有很大的帮助的.前一段时间了解了tomcat的工作的大致流程,对我的新工作有了很大的帮助.刚学习了ClassLoader( ...

  6. SpringBoot启动tomcat源码解读

    一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...

  7. SpringBoot启动嵌入式tomcat源码解读

    一.SpringBoot自动拉起Tomcat SpringBoot框架是当前比较流行的java后端开发框架,与maven结合大大简化了开发人员项目搭建的步骤,我们知道SpringBoot的启动类启动后 ...

  8. tomcat源码分析(一)- tomcat源码导入IDEA并正常启动

    项目导入 代码下载 打开GitHub网站:https://github.com/apache/tomcat 下载对应的zip包 解压对应的压缩包(当然你也可以用工具对其进行解压) unzip tomc ...

  9. 源码解读SLF4J绑定日志实现的原理

    一.导读 我们使用log4j框架时,经常会用slf4j-api.在运行时,经常会遇到如下的错误提示: SLF4J: Class path contains multiple SLF4J binding ...

随机推荐

  1. tar --help

    pengdl@debian:~/test$ mkdir test1 pengdl@debian:~/test$ mkdir test2 pengdl@debian:~/test$ tar -xzf p ...

  2. 使用 Feedly RSS阅读器订阅技术大牛的博客

    这几天一直都在自己看书,可是书上面的东西都比较落后一点,而且没有大牛博文上的东西讲的深入,可是来回跳转各位大牛的博客又非常的麻烦,有一些公众账号虽然也会推荐一些知识内容,可是你应该有过看到多个公众号发 ...

  3. JAVA实现DES加密

    DES加密介绍       DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...

  4. coherence配置说明

    经过上篇 coherence初识 ,最近算是和coherence杠上了,针对coherence3.5.3这个版本,把学到的东西整理下 1. 这个jar包有点大,4M多,首先打开coherence.ja ...

  5. Java 文件名操作的相关工具类

    项目中的操作文件名的工具类: import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  6. Redux你的Angular 2应用--ngRx使用体验

    Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...

  7. ios中Raw文件系统常用文件夹

    1.[/Applications] 常用软件的安装目录 2.[/Applications/Preferences.app/zh_CN.lproj] 软件Preferences.app的中文汉化文件存放 ...

  8. js和jQuery创建元素和把元素插入到文档中所用的方法

    js创建元素: document.createElement(" 创建的元素");   //“创建的元素”指:p ,h1,div,span........ js插入元素: docu ...

  9. IIS6批量转移网站

    IIS6.0有个导出配置的功能,但你却找不到界面上的直接导入配置功能,需要用到操作系统自带的iiscnfg.vbs脚本. 1.导出当前的IIS网站配置 打开Internet信息服务(IIS)---&g ...

  10. Ghost版Win8.1系统企业版下载

    host版Win8.1系统企业版,下载完成后一定要使用校验工具验证GHO文件MD5值,如果不符请不要安装,不然安装失败后果自负.GHO文件路径一定不要带中文,否则无法安装.安装完成第一次进入桌面会黑屏 ...