Re: [jetty-user] Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106
Joakim Erdfelt <[email protected]> Mon, 10 Dec 2012 09:09:26 -0700
| Newsgroups | gmane.comp.java.jetty.support |
|---|---|
| Message-ID | <CAG4zZZAh3HCho3eH9F0UKLD-_qz3NgW+a9S2NaZvbhZX9tK4yA@mail.gmail.com> |
--f46d042f96901836bf04d081ca32
Content-Type: text/plain; charset=ISO-8859-1
Something to try ...
new Thread(new Runnable() {
public void run() {
Thread.sleep(1000);
server.stop(); // or System.exit()
}
}).start();
Have the stop on the server occur from a different thread.
--
Joakim Erdfelt <[email protected]>
webtide.com <http://www.webtide.com/>
Developer advice, services and support
from the Jetty & CometD experts
eclipse.org/jetty - cometd.org
On Sun, Dec 9, 2012 at 6: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
>
--f46d042f96901836bf04d081ca32
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Something to try ...<div><br></div><div>new Thread(new Runnable() {</div><d=
iv>=A0 =A0 public void run() {</div><div>=A0 =A0 =A0 =A0Thread.sleep(1000);=
</div><div>=A0 =A0 =A0 =A0server.stop(); // or System.exit()</div><div>=A0 =
=A0 }</div><div>}).start();</div>
<div><br></div><div>Have the stop on the server occur from a different thre=
ad.</div><div><br clear=3D"all"><div><div>--</div><div>Joakim Erdfelt <<=
a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</=
a>></div>
<div><a href=3D"http://www.webtide.com/" target=3D"_blank">webtide.com</a><=
/div><div><span>Developer advice, services and support</span><br><span>from=
the Jetty & CometD experts</span></div><div><a href=3D"http://eclipse.=
org/jetty/" target=3D"_blank">eclipse.org/jetty</a>=A0-=A0<a href=3D"http:/=
/cometd.org/" target=3D"_blank">cometd.org</a></div>
</div><br>
<br><br><div class=3D"gmail_quote">On Sun, Dec 9, 2012 at 6: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=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><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>
--f46d042f96901836bf04d081ca32--