[jetty-user] Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106
Howard Lewis Ship <[email protected]> Sun, 9 Dec 2012 17:44:54 -0800
| Newsgroups | gmane.comp.java.jetty.support |
|---|---|
| Message-ID | <CACd-vsNKMjd81g-Yckf2zxvs3j=QFdJvZkDmZPNpcJ66hM+2QQ@mail.gmail.com> |
--047d7b66f24b42462504d075b6a6
Content-Type: text/plain; charset=ISO-8859-1
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
--047d7b66f24b42462504d075b6a6
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Please ignore ... You can't shutdown cleanly from a request handling th=
read.<span></span><br><br>On Sunday, December 9, 2012, Howard Lewis Ship w=
rote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde=
r-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=A0receivi=
ng=A0a callback when the shutdown is required:</div><div><br></div><div><br=
></div><div>public class WebStartup</div><div>{</div><div>=A0 =A0 private s=
tatic final Logger LOGGER =3D Logger.getLogger(WebStartup.class.getName());=
</div>
<div><br></div><div>=A0 =A0 public static void main(String[] arguments) thr=
ows Exception</div><div>=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 final Server se=
rver =3D new Server(8080);</div><div><br></div><div>=A0 =A0 =A0 =A0 server.=
setGracefulShutdown(1000);</div>
<div>=A0 =A0 =A0 =A0 server.setStopAtShutdown(true);</div><div><br></div><d=
iv>=A0 =A0 =A0 =A0 final WebAppContext root =3D new WebAppContext();</div><=
div><br></div><div>=A0 =A0 =A0 =A0 SessionHandler handler =3D new SessionHa=
ndler();</div><div><br>
</div>
<div>=A0 =A0 =A0 =A0 // Set up for in-memory sessions only.</div><div><br><=
/div><div>=A0 =A0 =A0 =A0 handler.setSessionManager(new HashSessionManager(=
));</div><div><br></div><div>=A0 =A0 =A0 =A0 root.setContextPath("/&qu=
ot;);</div><div>=A0 =A0 =A0 =A0 root.setResourceBase(resourceURL("META=
-INF/webapp/"));</div>
<div><br></div><div>=A0 =A0 =A0 =A0 server.setHandler(root);</div><div><br>=
</div><div>=A0 =A0 =A0 =A0 LOGGER.info("Starting web client on port 80=
80.");</div><div><br></div><div>=A0 =A0 =A0 =A0 SmashRuntime.getServer=
Lifecycle().addShutdownCallback(</div>
<div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 new Runnable()</div><div>=A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 @Ov=
erride</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 public void run()<=
/div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {</div><div>=A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 try</div>
<div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {</div><div>=A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 for (Connector c : server.g=
etConnectors())</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 c.close();</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 }</div>
<div><br></div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
server.stop();</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }=
catch (Exception e)</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 thr=
ow new RuntimeException(e);</div>
<div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }</div><div>=A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
}</div><div>=A0 =A0 =A0 =A0 );</div><div><br></div><div>=A0 =A0 =A0 =A0 se=
rver.start();</div><div>=A0 =A0 =A0 =A0 server.join();</div><div><br></div>=
<div>=A0 =A0 =A0 =A0 LOGGER.info("Web client shutdown.");</div>
<div>=A0 =A0 }</div><div><br></div><div>=A0 =A0 private static String resou=
rceURL(String resourcePath)</div><div>=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 U=
RL resource =3D Thread.currentThread().getContextClassLoader().getResource(=
resourcePath);</div>
<div><br></div><div>=A0 =A0 =A0 =A0 if (resource =3D=3D null)</div><div>=A0=
=A0 =A0 =A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 throw new RuntimeException=
(String.format("Resource URL for '%s' is null.", resource=
Path));</div><div>=A0 =A0 =A0 =A0 }</div>
<div><br></div><div>=A0 =A0 =A0 =A0 return resource.toString();</div><div>=
=A0 =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 excepti=
on:</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>=A0 =A0 while (_threadsStarted.get()>0 &&am=
p; (System.currentTimeMillis()-start) < _maxStopTime)</div><div>=A0 =A0 =
=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 Thread.sleep(1); // Line 139</d=
iv><div>=A0 =A0 =A0 =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>(971) 678-5210<br><a href=3D"http://howardlewisship.com" target=3D"_bla=
nk">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>(971) 678-=
5210<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>(971) 678-5210<br><a href=3D"http://howardlewisship.com">http://howardl=
ewisship.com</a><br>
--047d7b66f24b42462504d075b6a6--