[jetty-user] Problem with orderly shutdown of Jetty 8.1.8.v20121106

Howard Lewis Ship <[email protected]> Sun, 9 Dec 2012 17:43:48 -0800
Newsgroups gmane.comp.java.jetty.support
Message-ID <CACd-vsOYOC1fESTobW4U7f8FNDivTgVHYSboD5Ec-KyuFZhbdQ@mail.gmail.com>
--047d7b343a8a56508a04d075b25c
Content-Type: text/plain; charset=ISO-8859-1

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

--047d7b343a8a56508a04d075b25c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>I&#39;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&#39;m try=
ing to coordinate a clean shutdown of all of them before exitting.</div>

<div><br></div><div>In any case, I&#39;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(&quot;/&qu=
ot;);</div><div>=A0 =A0 =A0 =A0 root.setResourceBase(resourceURL(&quot;META=
-INF/webapp/&quot;));</div>

<div><br></div><div>=A0 =A0 =A0 =A0 server.setHandler(root);</div><div><br>=
</div><div>=A0 =A0 =A0 =A0 LOGGER.info(&quot;Starting web client on port 80=
80.&quot;);</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(&quot;Web client shutdown.&quot;);</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(&quot;Resource URL for &#39;%s&#39; is null.&quot;, 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()&gt;0 &amp;&am=
p; (System.currentTimeMillis()-start) &lt; _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">http://howardlewisship.com</a><br>

--047d7b343a8a56508a04d075b25c--