Reputation: 5518
I start embedded Jetty with this code:
Server server = new Server(8080);
MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
WebAppContext webapp = new WebAppContext("/home/igor/projects/javalite/activeweb-simple/src/main/webapp/", "/");
server.setHandler(webapp);
server.start();
Thread.sleep(10000);
System.out.println("Failed: " + server.isFailed());
System.out.println("Running: " + server.isRunning());
System.out.println("Started: " + server.isStarted());
System.out.println("Stopped: " + server.isStopped());
server.join();
The Webapp I'm deploying throws an intentional RuntimeException
during ServletFilter.init()
method.
When I start the server, I get the following output:
526 [main] WARN org.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext{/,file:/home/igor/projects/javalite/activeweb-simple/src/main/webapp/},/home/igor/projects/javalite/activeweb-simple/src/main/webapp/
org.javalite.activeweb.InitException: Failed to create and init a new instance of class: app.config.AppBootstrap: class java.lang.RuntimeException:Fail!
at app.config.AppBootstrap.init(AppBootstrap.java:32)
at org.javalite.activeweb.RequestDispatcher.initAppConfig(RequestDispatcher.java:134)
at org.javalite.activeweb.RequestDispatcher.initApp(RequestDispatcher.java:70)
at org.javalite.activeweb.RequestDispatcher.init(RequestDispatcher.java:64)
at org.eclipse.jetty.servlet.FilterHolder.doStart(FilterHolder.java:118)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:770)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:282)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at app.controllers.SimpleTest.test(SimpleTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.RuntimeException: Fail!
... 42 more
543 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started [email protected]:8080
Failed: false
Running: true
Started: true
Stopped: false
From what I can see, the server.start();
is syncronous, and my app is failing during this call.
As you can see, the app is not starting. However, the status values "failed", "running", "started" are not correct!
My app is not operational (intentionally) and the server got an exception during start, but it is reporting that all good.
What I need is: start the server and then ensure that it correctly started the webapp before proceeding further.
How do I test that the server started and successfully deployed my app?
EDIT: Relevant dependencies:
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.20.v20160902</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>8.1.20.v20160902</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
Upvotes: 0
Views: 650
Reputation: 2617
You are querying the wrong object. The server itself can host many web apps and by itself is not dependent on them. Some of those apps might be fine and some might fail on startup, so server status does not reflect the status of the web app. Server, in your case, started fine and can receive http requests.
Try querying your web app:
webapp.isStarted()
etc. It implements AbstractLifeCycle
similarily as Server
.
Update:
Since the basic statuses are not really communicative (that is, even failed app is considered started
), perhaps using isAvailable()
is better here. I remember using also getUnavailableException()
to get the exact exception that was thrown during initialization.
Upvotes: 1