问题
I've got the following embedded Jetty setup:
ServletContextHandler topHandler = new ServletContextHandler(server, contextPath);
// Set path of static resources
topHandler.setBaseResource(...);
// Bind dynamic content to /api
RootResource rootResource = new RootResource();
FilterHolder restApiHandler = new FilterHolder(rootResource);
for (Entry<String, String> parameter : initParams.entrySet())
restApiHandler.setInitParameter(parameter.getKey(), parameter.getValue());
topHandler.addFilter(restApiHandler, "/api/*", EnumSet.allOf(DispatcherType.class));
// Bind static content to /
ServletHolder staticResourceHandler = topHandler.addServlet(DefaultServlet.class, "/");
server.start();
And I found documentation on Hot Deployment at https://www.eclipse.org/jetty/documentation/9.4.19.v20190610/hot-deployment.html but I don't understand how to put these together.
How do I get Jetty to reload servlets after their class files are reloaded so I don't have to restart the server every time I modify a Java file during development?
回答1:
Hot Deployment is typically a feature of a WebAppContext
and the WAR concept that provides isolated ClassLoaders.
The ServletContextHandler
would need a custom ClassLoader to mimic the isolated classloader behaviors that the WebAppContext
provides.
Hot Deployment is a feature of the DeploymentManager
and an associated AppProvider
that does the scanning to detect changes (like on a file system).
You'll want a DeploymentManager
as a bean on your Server
.
And you'll want to select a AppProvider
(such as WebAppProvider
) to monitor a directory for changes and trigger new App updates back to the DeploymentManager
.
Next, you'll want to have your ServletContextHandler
declared entirely in the XML deployable format in that monitored directory.
The classes you are modifying need to come from somewhere that ISN'T part of the Server ClassLoader.
The XML deployable you are using will need to create this isolated custom classloader and load the classes from this new (non-server) location.
These combined are what you are looking at for Hot Deployment.
来源:https://stackoverflow.com/questions/57294096/hot-deployment-of-servlet-contexts-loaded-from-classpath