首页  

tomcat 热加载 热部署     所属分类 tomcat 浏览量 1259
热部署 重新部署应用
热加载 重新加载class

热加载 

context.xml
< Context reloadable="true"/>

监听 class 文件改变,包括WEB-INFO/classe,WEB-INFO/lib,WEB-INFO/web.xml等文件,
若发生更改,则局部进行加载,不清空session ,不释放内存。开发中用的多,但要考虑内存溢出的情况。

监听class文件变化

Context 启动时 启动后台线程 定时运行 Loader 的  backgroundProcess

StandardContext
// The Loader implementation with which this Container is associated.
// public class WebappLoader extends LifecycleMBeanBase implements Loader, PropertyChangeListener
private Loader loader = null;

A Loader represents a Java ClassLoader implementation 
that can be used by a Container to load class files (within a repository associated with the Loader) 
that are designed to be reloaded upon request, 
as well as a mechanism to detect whether changes have occurred in the underlying repository.
 
public interface Loader {
    // Execute a periodic task, such as reloading, etc. 
    // This method will be invoked inside the classloading context of this container. 
    public void backgroundProcess();

    public ClassLoader getClassLoader();
    public Context getContext();
    
... 



    
    public void backgroundProcess() {
        if (reloadable && modified()) {
            try {
                Thread.currentThread().setContextClassLoader
                    (WebappLoader.class.getClassLoader());
                if (context != null) {
                    context.reload();
                }
            } finally {
                if (context != null && context.getLoader() != null) {
                    Thread.currentThread().setContextClassLoader
                        (context.getLoader().getClassLoader());
                }
            }
        }
    }
    
    

热部署 server.xml < Context docBase="xxx" path="/xxx" autoDeploy="true"/> < Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> 整个项目重新部署,会清空session ,释放内存。 周期事件监听器 HostConfig public class HostConfig implements LifecycleListener 父类 backgroundProcess public void backgroundProcess() { ... fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null); } @Override public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); if (host instanceof StandardHost) { setCopyXML(((StandardHost) host).isCopyXML()); setDeployXML(((StandardHost) host).isDeployXML()); setUnpackWARs(((StandardHost) host).isUnpackWARs()); setContextClass(((StandardHost) host).getContextClass()); } } catch (ClassCastException e) { log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) { check(); } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) { beforeStart(); } else if (event.getType().equals(Lifecycle.START_EVENT)) { start(); } else if (event.getType().equals(Lifecycle.STOP_EVENT)) { stop(); } } /** * Check status of all webapps. */ protected void check() { if (host.getAutoDeploy()) { // Check for resources modification to trigger redeployment DeployedApplication[] apps = deployed.values().toArray(new DeployedApplication[0]); for (int i = 0; i < apps.length; i++) { if (!isServiced(apps[i].name)) checkResources(apps[i], false); } // Check for old versions of applications that can now be undeployed if (host.getUndeployOldVersions()) { checkUndeploy(); } // Hotdeploy applications deployApps(); } } public void check(String name) { DeployedApplication app = deployed.get(name); if (app != null) { checkResources(app, true); } deployApps(name); }
jsp热部署 JspServlet JspServletWrapper JspCompilationContext JDTCompiler JasperLoader public class JspServlet extends HttpServlet implements PeriodicEventListener { public class JasperLoader extends URLClassLoader EmbeddedServletOptions private int modificationTestInterval = 4; JDTCompiler public class JDTCompiler extends org.apache.jasper.compiler.Compiler isOutDated

上一篇     下一篇
java有意思的陷阱

synchronized知识点

TCP 重传 滑动窗口 流量控制 拥塞控制

arthas 使用 ognl 设置日志级别

ThreadLocal 与 SimpleDateFormat

hashCode和identifyHashCode区别