简体   繁体   English

在不重启服务器的情况下启动或停止码头 ServerConnector

[英]Start or Stop a jetty ServerConnector without server restart

I use jetty server to handle client requests and have a requirement where I need to start or stop one or few ServerConnectors on demand without restarting the server to which these connectors are attached to.我使用 jetty 服务器来处理客户端请求,并且需要根据需要启动或停止一个或几个 ServerConnector,而无需重新启动这些连接器所连接到的服务器。

For example.例如。

ServerConnector httpConnector;
ServerConnector httpsConnector;
Server server;
server.addConnector(httpConnector);
server.addConnector(httpsConnector);
server.start()

Need to start/stop/cancel a particular connector without forcing server to restart.需要启动/停止/取消特定连接器而不强制服务器重新启动。

You would have to do the following ...您必须执行以下操作...

// TODO: Have a reference to the Connector you are going to work with

// Remove from server first.
server.removeConnector(connector);

// Stop the connector.
// NOTE: Existing connections will still live on.  
// This will only stop accepting new connections.
connector.stop(); // might take a while (waiting for acceptors to close)

// TODO: do what you want with this connector

// Eg: harshly close existing connections
connector.getConnectedEndPoints().forEach((endpoint)-> {
    endpoint.close();
});

// Re-add stopped connector if you want
// This will start connector as well
// This can fail (eg: if port is already in use)
server.addConnector(connector);

You can call stop on the connector to stop it and start to start it again.您可以在连接器上调用stop来停止它并start重新启动它。 To get the relevant connector there are two solutions:要获得相关的连接器,有两种解决方案:

  • Keep the created connector-instances somewhere and call the methods将创建的连接器实例保存在某处并调用方法
  • Get all connectors of a server by calling getConnectors , iterate over them, find the one you want to stop and call stop or start on it to stop or start it.通过调用getConnectors获取服务器的所有连接器,遍历它们,找到要停止的连接器并调用stopstart来停止或启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM