Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 483
nsr Avatar asked Oct 24 '25 17:10

nsr


1 Answers

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);
like image 100
Joakim Erdfelt Avatar answered Oct 26 '25 05:10

Joakim Erdfelt