Reputation: 546
In an embedded Jetty container I have a servlet which pushes data to the clients via long-polling, and I would like to inform the clients when the server is being shut down by sending them a message before their connection is closed. When a client receives this message it is supposed to warn the user and stop making http requests.
Unfortunately, the first thing Jetty does when Server.stop() is called is closing all connectors, so the shutdown message is not sent. The only solution I have found so far is to register a LifeCycle.Listener on the server, which is called back before the connections are closed.
Is there a better way to achieve this? I would have expected the servlet to be informed via a callback of the imminent server shutdown, before the connections were closed.
Upvotes: 2
Views: 923
Reputation: 29824
I guess what you do is the only way. Looking at the code of Jetty 8.1, calling stop() calls
try
{
if (_state == __STOPPING || _state == __STOPPED)
return;
setStopping();
doStop();
setStopped();
}
catch (Exception e)
{
setFailed(e);
throw e;
}
catch (Error e)
{
setFailed(e);
throw e;
}
An setStopping() is
private void setStopping()
{
LOG.debug("stopping {}",this);
_state = __STOPPING;
for (Listener listener : _listeners)
listener.lifeCycleStopping(this);
}
So a registered LifeCycle.Listener is the only thing really called before anything else.
Upvotes: 2