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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With