首页 文章

如何在调试模式下设置tomcat工作?

提问于
浏览
0

我使用上下文xml将应用程序部署到tomcat . 我希望tomcat在调试模式下工作,我的意思是如果我在函数内部改变一些东西,比如改变String a =“123”;字符串a =“456”; tomcat应该在不重新加载应用程序的情况下获得更改 .

%CATALINA_HOME%/ conf / Catalina / localhost下的web01.xml

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="d:/document/workspace3.6/web01/WebContent" path="/web01" reloadable="false" debug="1" ></Context>

但是现在tomcat没有像我预期的那样工作,如果我用新版本替换旧的类文件,我必须重新启动tomcat才能获得更改 .

为什么tomcat没有重新加载类,我该如何让它作为调试模式工作?

我现在不使用Eclipse . 并且我不想设置reloadable =“true”,如果类更改,这将重新加载整个应用程序 .

我用的是tomcat5.5.23

4 回答

  • 0

    你实际上混淆了“调试”和热部署的概念 . 您可以将Tomcat配置为调试模式,然后远程调试在tomcat中运行的应用程序,这样当您在代码中添加断点时,调试器将跳转到该断点并停止执行 .

    您实际需要的是有可能热部署应用程序 . 使用tomcat,如果你修改.java文件,然后将它们复制到tomcat的工作目录中,你就会得到你想要的,即能够改变类中的某些东西,并让运行的tomcat部署的应用程序将它带入帐户无需重新部署整个应用程序 . 您可以通过配置tomcat应用程序上下文(在tomcat server.xml文件中或在项目特定的context.xml文件中)使应用程序将项目代码编译到的目录作为工作目录来自动执行此操作 .

    这是一个实际的例子:

    假设您在目录c:\ myProject中有一个maven项目 . 您在c:\ myProject \ src中有源文件,然后在编译它时,您将获得war文件和战争文件内容的分解目录,分别位于c:\ myProject \ target \ myProject.war和C:\ myProject的\目标\ myProject的 . 现在,如果您配置tomcat,以便为myProject tomcat上下文配置工作目录为c:\ myProject \ target \ myProject,那么每次修改.java文件时,都会更新.class对应文件目标(现在也在工作)dir,而tomcat会将其考虑在内 .

    我实际上已经使用这样的设置来开发tomcat,但它并不是最好的 . 首先,tomcat将仅对某些修改进行热部署,例如在现有方法的主体中修改某些内容时 . 其他修改将不会被考虑在内,例如添加新方法 - 为此,您必须进行完全重新部署以将其考虑在内 .

    一个更好的解决方案是使用maven和maven jetty插件 . 这个东西真的可以按照你想要的方式运行:你对一类jsp文件做的任何修改都会立即考虑到,并且在jetty中正在运行的应用程序中可见 .

  • 0
  • 1

    要做你想做的事情,你需要一些像java rebel或类似的东西,我知道有一些开源alternatives做同样的事情 .

  • 3

    好的,这是一个实际的例子:

    我有 cnas-war maven项目 . 一旦我用Maven构建它,我得到以下目录:

    c:/_andrei/work/cnas/cnas-war/target\cnas-war-0.0.1-SNAPSHOT
    

    在这里,我拥有通常会在.war文件中打包的所有内容,如.class文件,.jsp文件,.jar文件等 . 实际上,.war文件爆炸了 .

    我还有一个专门为这场战争部署的Tomcat 5.5,巧妙地放置在tomcat_cnas文件夹中 . 在Tomcat配置文件( conf\server.xml )中,我有以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Example Server Configuration File -->
    <!-- Note that component elements are nested corresponding to their
         parent-child relationships with each other -->
    <!-- A "Server" is a singleton element that represents the entire JVM,
         which may contain one or more "Service" instances.  The Server
         listens for a shutdown command on the indicated port.
    
         Note:  A "Server" is not itself a "Container", so you may not
         define subcomponents such as "Valves" or "Loggers" at this level.
     -->
    <Server port="8125" shutdown="SHUTDOWN">
      <!-- Comment these entries out to disable JMX MBeans support used for the 
           administration web application 
      <Listener className="org.apache.catalina.core.AprLifecycleListener" />
      <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/> -->
      <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <!-- Global JNDI resources -->
      <GlobalNamingResources>
        <!-- Test entry for demonstration purposes -->
        <Environment name="simpleValue" type="java.lang.Integer"
        value="30" />
        <Resource auth="Container"
        configurationDirectory="c:/cnas-content"
        factory="com.genia.toolbox.web.jndi_config.StringContainerFactory"
        name="string/activitymanagerConfigurationContainer"
        type="com.genia.toolbox.web.jndi_config.StringContainer" />
        <Resource name="string/activitymanagerConfigurationContainer"
        auth="Container"
        type="com.genia.toolbox.web.jndi_config.StringContainer"
        factory="com.genia.toolbox.web.jndi_config.StringContainerFactory"
        configurationDirectory="c:/cnas-content" />
      </GlobalNamingResources>
      <!-- Define the Tomcat Stand-Alone Service -->
      <Service name="Catalina">
        <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
        <Connector acceptCount="100" connectionTimeout="20000"
        disableUploadTimeout="true" enableLookups="false"
        maxHttpHeaderSize="8192" maxSpareThreads="75" maxThreads="150"
        minSpareThreads="25" port="8081" redirectPort="8443" />
        <!-- Define the top level container in our container hierarchy -->
        <Engine defaultHost="localhost" name="Catalina">
          <!-- for activitymanager -->
          <Host name="localhost" appBase="webapps" unpackWARs="true"
          autoDeploy="true" xmlValidation="false"
          xmlNamespaceAware="false">
            <Context path="/cnas"
            docBase="c:/_andrei/work/cnas/cnas-war/target/cnas-war-0.0.1-SNAPSHOT/"
            workDir="c:/_andrei/work/cnas/cnas-war/target/work-cnas/">
              <ResourceLink name="string/configurationContainer"
              global="string/activitymanagerConfigurationContainer"
              type="com.genia.toolbox.web.jndi_config.StringContainer" />
              <Resource name="bean/cnasConfig" auth="Container"
              type="com.genia.toolbox.projects.cnas.war.config.CnasConfig"
              factory="org.apache.naming.factory.BeanFactory"
              classpath="false" fileSystem="true"
              applicationFileLocation="c:/cnas-content/application.properties" />
              <Resource name="bean/cnasApplicationData"
              auth="Container"
              type="com.genia.toolbox.projects.cnas.war.config.CnasConfig"
              factory="org.apache.naming.factory.BeanFactory"
              classpath="false" fileSystem="true"
              applicationFileLocation="c:/cnas-content/cnas_application_data.xml" />
            </Context>
            <!--Context docBase="C:/travail/workspace/cnas/cnas-ws-proxy/target/webapp" path="/proxy">
    
                    <Resource name="bean/params" 
                        auth="Container"
                        type="fr.genia.cnas.config.Parameters" 
                        factory="org.apache.naming.factory.BeanFactory"
                        log4jFile="" 
                        serviceUrl="" 
                        debugMode="true" >
    
                    </Resource>
    
                </Context-->
          </Host>
        </Engine>
      </Service>
    </Server>
    

    正如您所看到的,在“context”标签中,我有一个指向快照目录的docBase属性(在maven构建之后战争爆炸的目录) . 现在,通过此设置,并将此项目导入Eclipse,如果我执行maven构建,然后启动此Tomcat,则将部署并运行war . 此时,如果我在Eclipse中修改.java文件中的方法内容(并保存),那么Tomcat将自动考虑该代码,并且应用程序的行为会有所不同,无需任何额外的重新部署 . 希望这可以帮助

相关问题