You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

HotSwapAgent使用指南:如何处理WAR包及依赖JAR中的类热替换?

Hey there! I’ve spent a fair amount of time working with HotSwapAgent for runtime class reloading, so let’s tackle your two questions with practical, actionable steps:

1. HotSwapping Classes in a Dependent Tool JAR

HotSwapAgent can handle changes in dependent JARs, but you need to make sure the agent is monitoring those JARs and you’re replacing the classes correctly. Here’s how:

  • First, ensure your HotSwapAgent is configured to watch resources:
    When starting your container, add the Javaagent parameter with watchResources=true to let the agent track changes in JAR files:
    java -javaagent:/path/to/hotswap-agent.jar=autoHotswap=true,watchResources=true [your container startup command]
    
  • Replace the class in the target JAR:
    1. Compile the modified class file from your tool JAR’s source code.
    2. Locate where the tool JAR is stored in your container: for most web apps, this is WEB-INF/lib inside your deployed WAR, or a shared library directory (like Tomcat’s lib folder or Weblogic’s domain lib).
    3. Use a file archiver (like 7-Zip or the jar command-line tool) to open the JAR and replace the old .class file with your new compiled one.
  • Trigger a reload if needed:
    If the agent doesn’t pick up the change automatically, you can manually trigger a reload. If you have the HotSwapAgent console enabled, run:
    reload com.your.tool.package.YourModifiedClass
    
    Alternatively, use your IDE’s hot-deploy feature to sync the modified class directly into the JAR (many IDEs like IntelliJ/Eclipse support this when configured correctly).

Note: Remember HotSwap’s core limitation—you can only modify method bodies. If you’re adding/removing fields, methods, or changing inheritance, HotSwap won’t handle this, and you’ll need to restart the container.

2. Configuring Tomcat & Weblogic to Accept JAR Changes

Each container has its own settings to disable caching and enable runtime detection of JAR modifications. Let’s break them down:

Tomcat Configuration

  • Disable resource caching:
    Edit your app’s META-INF/context.xml (or the global conf/context.xml in Tomcat) to add this resource configuration, which prevents Tomcat from caching JAR contents:
    <Context>
      <Resources cachingAllowed="false" cacheMaxSize="0"/>
    </Context>
    
  • Adjust default servlet settings:
    In conf/web.xml, find the default servlet entry and set readonly to false to allow modifications to WEB-INF files:
    <servlet>
      <servlet-name>default</servlet-name>
      <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
      <init-param>
        <param-name>readonly</param-name>
        <param-value>false</param-value>
      </init-param>
      <!-- Keep other params as is -->
    </servlet>
    
  • Verify auto-deploy settings:
    Ensure autoDeploy="true" and deployOnStartup="true" are set in conf/server.xml under the Host node (this is the default, but double-check if you’ve modified it before).

Weblogic Configuration

  • Enable hot deploy mode:
    First, start Weblogic in Development Mode (Production Mode disables hot deploy by default). Add this JVM parameter to your startup script:
    -Dweblogic.ProductionModeEnabled=false
    
    Then, edit your domain’s config.xml to enable hot deploy for the server:
    <Server>
      <Name>your-server-name</Name>
      <HotDeployEnabled>true</HotDeployEnabled>
    </Server>
    
  • Configure class loading and refresh intervals:
    In your app’s WEB-INF/weblogic.xml, add these settings to prioritize WEB-INF/lib classes and set a refresh interval for detecting changes:
    <weblogic-web-app>
      <container-descriptor>
        <prefer-web-inf-classes>true</prefer-web-inf-classes>
        <refresh-period-seconds>5</refresh-period-seconds> <!-- Check for changes every 5 seconds -->
      </container-descriptor>
    </weblogic-web-app>
    
  • Add HotSwapAgent to Weblogic’s startup:
    Include the Javaagent parameter in Weblogic’s startup script:
    java -javaagent:/path/to/hotswap-agent.jar=autoHotswap=true -Dweblogic.debug.DebugHotSwap=true -jar startWebLogic.jar
    

Pro Tip: For shared libraries in Weblogic, you’ll need to enable hot deploy for the library itself via the Admin Console (under Deployments > Your Shared Library > Configuration > General > Enable Hot Deploy).


内容的提问来源于stack exchange,提问作者Prateek Chachra

火山引擎 最新活动