2

We're using Jetty 9.3 with a single application in ROOT.war with a web-xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <listener>
        <listener-class>path.AppEventListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>MyService</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- ***NOTE*** added next line per minus's answer -->
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>path.MyService</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

If the MyService application fails to initialize correctly on startup, we would like Jetty to shut down immediately, but we can't seem to figure out how to do so. This question sounds like it might be relevant, but doesn't seem to make any change in our hands, possibly because we use ant without ivy as the build tool.

The (undesired) behavior we currently see is that Jetty doesn't attempt to initialize the application until it's needed, and then throws an exception.

Update based on minus's answer:

Note I updated the web.xml above per said answer.
I added a jetty-env.xml file to WEB-INF as described in this question:

$ jar tf webapps/ROOT.war  | grep -v lib | grep -v class
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/web.xml
WEB-INF/jetty-env.xml
$ jar xf webapps/ROOT.war WEB-INF/jetty-env.xml
$ cat WEB-INF/jetty-env.xml 
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="throwUnavailableOnStartupException">true</Set>
</Configure>

Note I also updated the DOCTYPE line for Jetty 9.3.

At this point the application starts up immediately and throws an error, but Jetty stays up. I see the following in the logs:

2017-03-13 18:36:59.597:WARN:ROOT:main: unavailable
2017-03-13 18:36:59.599:WARN:oejw.WebAppContext:main: Failed startup of  context o.e.j.w.WebAppContext@6767c1fc{/,file:///tmp/jetty-0.0.0.0-20002-ROOT.war-_-any-593353947747896998.dir/webapp/,UNAVAILABLE}{/ROOT.war}
Community
  • 1
  • 1
elhefe
  • 3,404
  • 3
  • 31
  • 45

1 Answers1

2

First of all you should set <load-on-startup>1</load-on-startup> on the servlet.

<servlet>
    <servlet-name>MyService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>path.MyService</param-value>
    </init-param>
</servlet>

So this servlet is loaded first, then to stop the server you could read Jetty: Detect if Webapp failed to start.

UPDATE:

Shutting down the server from within the application is a bit rough, I would start a process (upon failure) that calls a shutdown script.

I would provide (as an administrator of the server) the appropriate shutdown script (for example stop.jar ).

This way you will shut the server if you want (by providing the script), or ignore the failure by not providing it, thus keeping control of the server status on the administrator side and not bestowing this responsibility to application.

Community
  • 1
  • 1
minus
  • 2,646
  • 15
  • 18
  • Thanks for the help. The application now starts up immediately and throws an exception, but the jetty server keeps running - I've updated my answer with the current state. – elhefe Mar 14 '17 at 01:59