[英]Start or Stop a jetty ServerConnector without server restart
我使用 jetty 服務器來處理客戶端請求,並且需要根據需要啟動或停止一個或幾個 ServerConnector,而無需重新啟動這些連接器所連接到的服務器。
例如。
ServerConnector httpConnector;
ServerConnector httpsConnector;
Server server;
server.addConnector(httpConnector);
server.addConnector(httpsConnector);
server.start()
需要啟動/停止/取消特定連接器而不強制服務器重新啟動。
您必須執行以下操作...
// 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);
您可以在連接器上調用stop
來停止它並start
重新啟動它。 要獲得相關的連接器,有兩種解決方案:
getConnectors
獲取服務器的所有連接器,遍歷它們,找到要停止的連接器並調用stop
或start
來停止或啟動它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.