我使用以下代码启动嵌入式Jetty:
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();
我部署的Web应用程序在RuntimeException
方法期间会抛出 故意 ServletFilter.init()
。
当我启动服务器时,我得到以下输出:
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 SelectChannelConnector@0.0.0.0:8080
Failed: false
Running: true
Started: true
Stopped: false
从我所看到的情况来看,server.start();
是同步的,我的应用在此次通话中失败了。
正如您所看到的,应用程序无法启动。但是,状态值"失败","正在运行","已启动"不正确!
我的应用无法运营(故意),服务器在启动时遇到异常,但报告一切正常。
我需要的是:启动服务器,然后确保它正确启动了webapp,然后再继续。
如何测试服务器是否已启动并成功部署了我的应用?
编辑:相关依赖项:
<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>
答案 0 :(得分:1)
您正在查询错误的对象。服务器本身可以托管许多Web应用程序,并且它本身并不依赖于它们。其中一些应用程序可能没问题,有些可能在启动时失败,因此服务器状态不会反映Web应用程序的状态。在您的情况下,服务器启动正常,可以接收http请求。
尝试查询您的网络应用:
webapp.isStarted()
等。它将AbstractLifeCycle
类似地Server
实现。
<强>更新强>
由于基本状态并非真正具有交际性(即使即使失败的应用程序被视为started
),也许在这里使用isAvailable()
更好。我还记得使用getUnavailableException()
来获取初始化期间抛出的确切异常。