我已经按照文档中的方法创建了一个带有servlet部署的服务器服务器:
http://undertow.io/documentation/servlet/deployment.html
我现在正试图在servlet中停止服务器和应用程序。
调用server.stop()
会使指定端口上的服务器不可用,但是当我再次调用server.start()
时,应用程序立即可用(如果已停止,则需要一些时间才能启动再次)。我还可以看到在调用server.stop
时没有释放服务器的内存。我尝试了各种组合,包括调用deploymentManager.undeploy()
all具有相同的效果。
我搜索了文档中的任何线索,然后空了。我想知道是否有其他人有任何线索?下面是当前状态下的源代码(请注意,我不是Java开发人员,所以会有很多花哨的东西):
package org.locee;
import io.undertow.Undertow;
import io.undertow.Undertow.Builder;
import io.undertow.Handlers;
import io.undertow.util.Headers;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.HttpHandler;
import static io.undertow.servlet.Servlets.defaultContainer;
import javax.servlet.ServletException;
public class LoceeUndertowServer {
private DeploymentManager deploymentManager;
private Undertow undertowServer;
private DeploymentInfo servletInfo;
private int port;
private String host;
public LoceeUndertowServer( DeploymentInfo servletInfo, int port, String host ) {
this.servletInfo = servletInfo;
this.port = port;
this.host = host;
}
public void start() throws ServletException {
deploymentManager = defaultContainer().addDeployment( servletInfo );
deploymentManager.deploy();
HttpHandler httpHandler = deploymentManager.start();
PathHandler pathHandler = Handlers.path( Handlers.redirect( "/" ) ).addPrefixPath( "/", httpHandler );
Builder builder = Undertow.builder();
builder.addHttpListener( port, host );
builder.setHandler( pathHandler );
undertowServer = builder.build();
undertowServer.start();
}
public void shutdown() throws ServletException {
deploymentManager.undeploy();
defaultContainer().removeDeployment( servletInfo );
undertowServer.stop();
}
public void restart() throws ServletException {
shutdown();
start();
}
}
答案 0 :(得分:1)
您将保留所谓的Undertow组件的强引用以及它们也直接引用的任何内容。引用存储在以下字段中:
private DeploymentManager deploymentManager;
private Undertow undertowServer;
private DeploymentInfo servletInfo;
这不是Undertow的问题,而是更多JVM如何运作的问题。垃圾收集器从一组根开始(将它们视为JVM上的线程)并遍历对其他对象的所有引用。一旦完成它的遍历,则回收所有不可访问的对象。这是整个过程的逻辑视图。收集时间取决于JVM以及几代人。如果将它们放在#shutdown()方法中,以下代码将删除对组件的引用:
deploymentManager = null;
undertowServer = null;
servletInfo = null;
注意:执行此操作后,您将无法重新启动。
答案 1 :(得分:0)
PathHandler path = Handlers.path()
.addPrefixPath("/quitQuit", new QuitHandler());
Undertow.Builder builder = Undertow.builder()
.setHandler(path)
.addHttpListener(8090, "0.0.0.0");
...
public class QuitHandler implements HttpHandler {
@Override
public void handleRequest(HttpServerExchange hse) throws Exception {
// some log info...
System.exit(0);
}
}