gpt4 book ai didi

java - Jetty 错误地报告状态 isRunning() 和 isFailed()

转载 作者:行者123 更新时间:2023-12-01 09:32:17 25 4
gpt4 key购买 nike

我使用以下代码启动嵌入式 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 应用程序在 ServletFilter.init() 方法期间抛出故意 RuntimeException

当我启动服务器时,我得到以下输出:

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(); 是同步的,我的应用程序在此调用期间失败。

如您所见,应用程序未启动。但是,状态值“失败”、“正在运行”、“已启动”不正确!

我的应用程序无法运行(故意),并且服务器在启动期间出现异常,但它报告一切正常

我需要的是:启动服务器,然后确保它正确启动了 web 应用程序,然后再继续。

如何测试服务器是否已启动并成功部署我的应用程序?

编辑:相关依赖项:

<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>

最佳答案

您正在查询错误的对象。服务器本身可以托管许多网络应用程序,并且本身不依赖于它们。其中一些应用程序可能正常,而另一些可能在启动时失败,因此服务器状态不会反射(reflect) Web 应用程序的状态。对于您的情况,服务器启动正常并且可以接收 http 请求。

尝试查询您的网络应用程序:

webapp.isStarted()

等等。它与 Server 类似地实现 AbstractLifeCycle

更新:

由于基本状态并不是真正的通信(也就是说,即使失败的应用程序也被认为是已启动),也许在这里使用isAvailable()会更好。我记得还使用了 getUnavailableException() 来获取初始化期间引发的确切异常。

关于java - Jetty 错误地报告状态 isRunning() 和 isFailed(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39310302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com