Re: [jetty-user] Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106
Jesse McConnell <[email protected]> Mon, 10 Dec 2012 08:22:42 -0600
| Newsgroups | gmane.comp.java.jetty.support |
|---|---|
| Message-ID | <CAPHPUsLOYxz+K6qG5+wiw7FRRvj-W3axL2F8R4bxsqU5mj_NuA@mail.gmail.com> |
--90e6ba211fbf6431d504d0804c91 Content-Type: text/plain; charset=UTF-8 Take a look at the ShutdownHandler, that might do what you're looking for cheers, jesse -- jesse mcconnell [email protected] On Sun, Dec 9, 2012 at 7:44 PM, Howard Lewis Ship <[email protected]> wrote: > Please ignore ... You can't shutdown cleanly from a request handling > thread. > > > On Sunday, December 9, 2012, Howard Lewis Ship wrote: > >> I'm attempting to shutdown an instance of Jetty in an orderly way; Jetty >> is one of a number of services running in my server, and I'm trying to >> coordinate a clean shutdown of all of them before exitting. >> >> In any case, I'm starting the server, and receiving a callback when the >> shutdown is required: >> >> >> public class WebStartup >> { >> private static final Logger LOGGER = >> Logger.getLogger(WebStartup.class.getName()); >> >> public static void main(String[] arguments) throws Exception >> { >> final Server server = new Server(8080); >> >> server.setGracefulShutdown(1000); >> server.setStopAtShutdown(true); >> >> final WebAppContext root = new WebAppContext(); >> >> SessionHandler handler = new SessionHandler(); >> >> // Set up for in-memory sessions only. >> >> handler.setSessionManager(new HashSessionManager()); >> >> root.setContextPath("/"); >> root.setResourceBase(resourceURL("META-INF/webapp/")); >> >> server.setHandler(root); >> >> LOGGER.info("Starting web client on port 8080."); >> >> SmashRuntime.getServerLifecycle().addShutdownCallback( >> new Runnable() >> { >> @Override >> public void run() >> { >> try >> { >> for (Connector c : server.getConnectors()) >> { >> c.close(); >> } >> >> server.stop(); >> } catch (Exception e) >> { >> throw new RuntimeException(e); >> } >> } >> } >> ); >> >> server.start(); >> server.join(); >> >> LOGGER.info("Web client shutdown."); >> } >> >> private static String resourceURL(String resourcePath) >> { >> URL resource = >> Thread.currentThread().getContextClassLoader().getResource(resourcePath); >> >> if (resource == null) >> { >> throw new RuntimeException(String.format("Resource URL for >> '%s' is null.", resourcePath)); >> } >> >> return resource.toString(); >> } >> } >> >> This works to a point: >> >> The server does shutdown but the code throws an exception: >> >> [WARN] AbstractLifeCycle FAILED org.eclipse.jetty.server.Server@48433545: >> java.lang.InterruptedException: sleep interrupted >> java.lang.InterruptedException: sleep interrupted >> at java.lang.Thread.sleep(Native Method) >> at >> org.eclipse.jetty.util.thread.QueuedThreadPool.doStop(QueuedThreadPool.java:139) >> at >> org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) >> at >> org.eclipse.jetty.util.component.AggregateLifeCycle.doStop(AggregateLifeCycle.java:107) >> at >> org.eclipse.jetty.server.handler.AbstractHandler.doStop(AbstractHandler.java:69) >> at >> org.eclipse.jetty.server.handler.HandlerWrapper.doStop(HandlerWrapper.java:108) >> at org.eclipse.jetty.server.Server.doStop(Server.java:338) >> at >> org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) >> at smash.example.webclient.WebStartup$1.run(WebStartup.java:52) >> at >> com.annadaletech.smash.ServerLifecycleImpl.requestShutdown(ServerLifecycleImpl.java:32) >> >> >> Looking at the code for QueuedThreadPool: >> >> while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) >> < _maxStopTime) >> { >> Thread.sleep(1); // Line 139 >> } >> >> >> >> -- >> Howard M. Lewis Ship >> >> Creator of Apache Tapestry >> >> The source for Tapestry training, mentoring and support. Contact me to >> learn how I can get you up and productive in Tapestry fast! >> >> (971) 678-5210 >> http://howardlewisship.com >> >> >> -- >> Howard M. Lewis Ship >> >> Creator of Apache Tapestry >> >> The source for Tapestry training, mentoring and support. Contact me to >> learn how I can get you up and productive in Tapestry fast! >> >> (971) 678-5210 >> http://howardlewisship.com >> > > > -- > Howard M. Lewis Ship > > Creator of Apache Tapestry > > The source for Tapestry training, mentoring and support. Contact me to > learn how I can get you up and productive in Tapestry fast! > > (971) 678-5210 > http://howardlewisship.com > --90e6ba211fbf6431d504d0804c91 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Take a look at the ShutdownHandler, that might do what you're looking f= or<div><br></div><div>cheers,</div><div>jesse</div><div class=3D"gmail_extr= a"><br clear=3D"all"><div>--<br>jesse mcconnell<br><a href=3D"mailto:jesse.= [email protected]" target=3D"_blank">[email protected]</a></div> <br> <br><br><div class=3D"gmail_quote">On Sun, Dec 9, 2012 at 7:44 PM, Howard L= ewis Ship <span dir=3D"ltr"><<a href=3D"mailto:[email protected]" target= =3D"_blank">[email protected]</a>></span> wrote:<br><blockquote class=3D"= gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-= left:1ex"> Please ignore ... You can't shutdown cleanly from a request handling th= read.<div class=3D"HOEnZb"><div class=3D"h5"><span></span><br><br>On Sunday= , December 9, 2012, Howard Lewis Ship wrote:<br><blockquote class=3D"gmail= _quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:= 1ex"> <div>I'm attempting to shutdown an instance of Jetty in an orderly way;= Jetty is one of a number of services running in my server, and I'm try= ing to coordinate a clean shutdown of all of them before exitting.</div> <div><br></div><div>In any case, I'm starting the server, and=C2=A0rece= iving=C2=A0a callback when the shutdown is required:</div><div><br></div><d= iv><br></div><div>public class WebStartup</div><div>{</div><div>=C2=A0 =C2= =A0 private static final Logger LOGGER =3D Logger.getLogger(WebStartup.clas= s.getName());</div> <div><br></div><div>=C2=A0 =C2=A0 public static void main(String[] argument= s) throws Exception</div><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2= =A0 =C2=A0 final Server server =3D new Server(8080);</div><div><br></div><d= iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 server.setGracefulShutdown(1000);</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 server.setStopAtShutdown(true);</div><div>= <br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 final WebAppContext root =3D new= WebAppContext();</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Sess= ionHandler handler =3D new SessionHandler();</div><div><br> </div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 // Set up for in-memory sessions only.</di= v><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 handler.setSessionManager= (new HashSessionManager());</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 = =C2=A0 root.setContextPath("/");</div><div>=C2=A0 =C2=A0 =C2=A0 = =C2=A0 root.setResourceBase(resourceURL("META-INF/webapp/"));</di= v> <div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 server.setHandler(root);</d= iv><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 LOGGER.info("Starti= ng web client on port 8080.");</div><div><br></div><div>=C2=A0 =C2=A0 = =C2=A0 =C2=A0 SmashRuntime.getServerLifecycle().addShutdownCallback(</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 new Runnable()= </div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 @= Override</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 public void run()</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 try</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for (Connector c : server.= getConnectors())</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 c.close();</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }</di= v> <div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 server.stop();</div><div>=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 } catch (Exception e)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 throw new RuntimeException(e);</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 );</div><div><br></di= v><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 server.start();</div><div>=C2=A0 =C2=A0 = =C2=A0 =C2=A0 server.join();</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 = =C2=A0 LOGGER.info("Web client shutdown.");</div> <div>=C2=A0 =C2=A0 }</div><div><br></div><div>=C2=A0 =C2=A0 private static = String resourceURL(String resourcePath)</div><div>=C2=A0 =C2=A0 {</div><div= >=C2=A0 =C2=A0 =C2=A0 =C2=A0 URL resource =3D Thread.currentThread().getCon= textClassLoader().getResource(resourcePath);</div> <div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (resource =3D=3D null)</= div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 throw new RuntimeException(String.format("Resource U= RL for '%s' is null.", resourcePath));</div><div>=C2=A0 =C2=A0= =C2=A0 =C2=A0 }</div> <div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return resource.toString();= </div><div>=C2=A0 =C2=A0 }</div><div>}</div><div><br></div><div>This works = to a point:</div><div><br></div><div>The server does shutdown but the code = throws an exception:</div> <div><br></div><div><div>[WARN] AbstractLifeCycle FAILED org.eclipse.jetty.= server.Server@48433545: java.lang.InterruptedException: sleep interrupted</= div><div>java.lang.InterruptedException: sleep interrupted</div><div><span = style=3D"white-space:pre-wrap"> </span>at java.lang.Thread.sleep(Native Met= hod)</div> <div><span style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.util= .thread.QueuedThreadPool.doStop(QueuedThreadPool.java:139)</div><div><span = style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.util.component.= AbstractLifeCycle.stop(AbstractLifeCycle.java:89)</div> <div><span style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.util= .component.AggregateLifeCycle.doStop(AggregateLifeCycle.java:107)</div><div= ><span style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.server.h= andler.AbstractHandler.doStop(AbstractHandler.java:69)</div> <div><span style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.serv= er.handler.HandlerWrapper.doStop(HandlerWrapper.java:108)</div><div><span s= tyle=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.server.Server.do= Stop(Server.java:338)</div> <div><span style=3D"white-space:pre-wrap"> </span>at org.eclipse.jetty.util= .component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)</div><div><spa= n style=3D"white-space:pre-wrap"> </span>at smash.example.webclient.WebStar= tup$1.run(WebStartup.java:52)</div> <div><span style=3D"white-space:pre-wrap"> </span>at com.annadaletech.smash= .ServerLifecycleImpl.requestShutdown(ServerLifecycleImpl.java:32)</div></di= v><div><br></div><div><br></div><div>Looking at the code for QueuedThreadPo= ol:</div> <div><br></div><div><div>=C2=A0 =C2=A0 while (_threadsStarted.get()>0 &a= mp;& (System.currentTimeMillis()-start) < _maxStopTime)</div><div>= =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 Thread.sleep(1); // Line 139</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 }= </div> </div><div><br></div><div><br></div><div><br></div>-- <br>Howard M. Lewis S= hip<br><br>Creator of Apache Tapestry<br><br>The source for Tapestry traini= ng, mentoring and support. Contact me to learn how I can get you up and pro= ductive in Tapestry fast!<br> <br><a href=3D"tel:%28971%29%20678-5210" value=3D"+19716785210" target=3D"_= blank">(971) 678-5210</a><br><a href=3D"http://howardlewisship.com" target= =3D"_blank">http://howardlewisship.com</a><br> <br><br>-- <br>Howard M. Lewis Ship<br><br>Creator of Apache Tapestry<br><b= r>The source for Tapestry training, mentoring and support. Contact me to le= arn how I can get you up and productive in Tapestry fast!<br><br><a href=3D= "tel:%28971%29%20678-5210" value=3D"+19716785210" target=3D"_blank">(971) 6= 78-5210</a><br> <a href=3D"http://howardlewisship.com" target=3D"_blank">http://howardlewis= ship.com</a><br> </blockquote><br><br>-- <br>Howard M. Lewis Ship<br><br>Creator of Apache T= apestry<br><br>The source for Tapestry training, mentoring and support. Con= tact me to learn how I can get you up and productive in Tapestry fast!<br> <br><a href=3D"tel:%28971%29%20678-5210" value=3D"+19716785210" target=3D"_= blank">(971) 678-5210</a><br><a href=3D"http://howardlewisship.com" target= =3D"_blank">http://howardlewisship.com</a><br> </div></div></blockquote></div><br></div> --90e6ba211fbf6431d504d0804c91--