知识库--StandardContext + Reloading(60)

来源:互联网 发布:好压 mac 编辑:程序博客网 时间:2024/06/05 10:22

Support for Reloading

The StandardContext class defines the reloadable property to indicate whether reloading of the application is enabled. When reloading is enabled, the application will be reloaded if the web.xml file changes or if one of the files in the WEB-INF/classes directory is recompiled.

The StandardContext class depends on its loader to reload the application. In Tomcat 4 the WebappLoader class, the implementation class of Loader in StandardContext,* has a thread that checks the timestamps of all class and JAR files in the WEB-INF directory*. All you need to do to start this thread is associate the WebappLoader with the StandardContext by calling its setContainer method. Here is the implementation of the setContainer method of the WebappLoader class in Tomcat 4:

public void setContainer(Container container){    //Deregister from the old Container if any    if((this.container != null) && (this.container instanceof Context))        ((Context)this.container).removePropertyChangeListener(this);    //Process this property change    Container odlContainer = this.container;    this.container = container;    support.firePropertyChange("container",oldContainer,this.container);    //Register with the new Container if any    if((this.container != null) && (this.container instanceof Context)){        setReloadable(((Context)this.container).getReloadable()));        ((Context)this.container).addPropertyChangeListener(this);    }}

Take a look at the last if block. If the container is a context, the setReloadable method will be called. This means, the value of the reloadable property of the WebappLoader instance will be the same as the value of the reloadable property of the StandardContext instance.

Here is the setReloadable method implementation of the WebappLoader class:

public void setReloadable(boolean reloadable){    //Process this property change    boolean oldReloadable =  this.reloadable;    this.reloadable = reloadable;    support.firePropertyChange("reloadable",new Booleann(oldRreloadable),new Boolean(this.reloadable));    //Start or stop our background thread if required  是否重新加载    if(!started)        return;    if(!oldReloadable && this.reloadable){        threadStart();    }else if(oldReloadable && !this.reloadable){        threadStop();    }}

If the reloadable property changes from false to true, the threadStart method is called. If it changes from true to false, the threadStop method is called. The threadStart method starts a dedicated thread that continuously checks the timestamps of the class and JAR files in WEB-INF. The threadStop method stops this thread.//检查时间戳

0 0