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:
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 withwatchResources=trueto 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:
- Compile the modified class file from your tool JAR’s source code.
- Locate where the tool JAR is stored in your container: for most web apps, this is
WEB-INF/libinside your deployed WAR, or a shared library directory (like Tomcat’slibfolder or Weblogic’s domain lib). - Use a file archiver (like 7-Zip or the
jarcommand-line tool) to open the JAR and replace the old.classfile 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:
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).reload com.your.tool.package.YourModifiedClass
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.
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’sMETA-INF/context.xml(or the globalconf/context.xmlin Tomcat) to add this resource configuration, which prevents Tomcat from caching JAR contents:<Context> <Resources cachingAllowed="false" cacheMaxSize="0"/> </Context> - Adjust default servlet settings:
Inconf/web.xml, find thedefaultservlet entry and setreadonlytofalseto allow modifications toWEB-INFfiles:<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:
EnsureautoDeploy="true"anddeployOnStartup="true"are set inconf/server.xmlunder theHostnode (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:
Then, edit your domain’s-Dweblogic.ProductionModeEnabled=falseconfig.xmlto 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’sWEB-INF/weblogic.xml, add these settings to prioritizeWEB-INF/libclasses 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




