Problem with orderly shutdown of Jetty 8.1.8.v20121106

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with orderly shutdown of Jetty 8.1.8.v20121106

Howard Lewis Ship
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
Reply | Threaded
Open this post in threaded view
|

Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106

Howard Lewis Ship
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
Reply | Threaded
Open this post in threaded view
|

Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106

Jesse McConnell
Take a look at the ShutdownHandler, that might do what you're looking for

cheers,
jesse

--
jesse mcconnell
[hidden email]



On Sun, Dec 9, 2012 at 7:44 PM, Howard Lewis Ship <[hidden email]> 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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(971) 678-5210
http://howardlewisship.com

Reply | Threaded
Open this post in threaded view
|

Re: Problem with orderly shutdown of Jetty 8.1.8.v20121106

Joakim Erdfelt-9
In reply to this post by Howard Lewis Ship
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 <[hidden email]>
Developer advice, services and support
from the Jetty & CometD experts



On Sun, Dec 9, 2012 at 6:44 PM, Howard Lewis Ship <[hidden email]> 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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(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!

<a href="tel:%28971%29%20678-5210" value="+19716785210" target="_blank">(971) 678-5210
http://howardlewisship.com