简体   繁体   中英

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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