当前位置: 首页 > 面试题库 >

如果WebAppContext无法启动,如何取消启动或关闭码头

松国兴
2023-03-14
问题内容

我正在使用Maven Jetty插件,如果任何Bean失败,有没有一种方法可以防止服务器启动或关闭(例如,使用maven-jetty-plugin)?

这是一个例子:

2013-04-22 11:43:59.327:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@9a681a{/,file:/opt/bamboo/bamboo-home/xml-data/build-dir/APA-BC-JETTYAPA/src/main/webapp/,STARTING}{file:/opt/bamboo/bamboo-home/xml-data/build-dir/APA-BC-JETTYAPA/src/main/webapp/}
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/classes/applicationContext.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'staticDataService' for bean class [com.etermax.common.service.StaticDataService] conflicts with existing, non-compatible bean definition of same name and class [com.etermax.bingo.api.service.BingoStaticDataService]
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:778)
        at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:425)
        at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:770)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:275)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1312)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:722)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490)
        at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:275)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:175)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.server.Server.start(Server.java:346)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.server.Server.doStart(Server.java:294)
        at org.eclipse.jetty.maven.plugin.JettyServer.doStart(JettyServer.java:78)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:537)
        at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:352)
        at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:167)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: 
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'staticDataService' for bean class [com.etermax.common.service.StaticDataService] conflicts with existing, non-compatible bean definition of same name and class [com.etermax.bingo.api.service.BingoStaticDataService]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:312)
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:253)
        at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
        at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:778)
        at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:425)
        at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:770)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:275)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1312)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:722)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490)
        at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:275)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:175)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.server.Server.start(Server.java:346)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:90)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.server.Server.doStart(Server.java:294)
        at org.eclipse.jetty.maven.plugin.JettyServer.doStart(JettyServer.java:78)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:537)
        at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:352)
        at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:167)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
2013-04-22 11:43:59.349:INFO:oejs.ServerConnector:main: Started ServerConnector@e08f5f{HTTP/1.1}{0.0.0.0:8083}

我想关闭服务器。我知道有一种方法涉及在每个bean中编码destroy方法,但是我想为任何bean提供全局的东西。


问题答案:

要完成这项任务并不容易。从码头的源代码中找到所需的信息需要花费数小时的辛苦工作。

这是与我的简单测试应用程序一起使用的解决方案,该应用程序将ServletException放在HttpServlet
init方法上。成功的关键是将ServletHandler的startWithUnavailable属性设置为false。

pom.xml:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.10.v20130312</version>
    <configuration>
        <contextXml>src/main/resources/context.xml</contextXml>
        <webAppConfig>
            <throwUnavailableOnStartupException>true</throwUnavailableOnStartupException>
        </webAppConfig>
    </configuration>
</plugin>

context.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure>
    <Set name="throwUnavailableOnStartupException">true</Set>
    <Set name="servletHandler">
        <New class="org.eclipse.jetty.servlet.ServletHandler">
            <Set name="startWithUnavailable">false</Set>
        </New>
    </Set>
</Configure>

TestServlet.java:

public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        super.init();
        throw new ServletException("FAIL!");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    }
}


 类似资料:
  • 我有一个用例,我们必须监控使用Dropwizard指标并将其推送到服务器的各种服务统计信息。但是,如果服务出现故障并重新启动,则会重置指标。计数器将重置为 0,这将影响监视,因为百分位数或平均值等值会受到影响。 如何处理这种情况或用例?

  • iam使用带有kafa整数的jhipsterSpring靴 正在应用中。yml公司 云:流:默认绑定器:kafka函数:定义:memberSupplier;成员批准供应商;memberEventConsumer;memberApprovalEventConsumer绑定:memberSupplier-out-0:目标:memberEventConsumer-in-0:目标:memberApprov

  • 问题内容: 我正在尝试在我的c程序中检测从Linux关闭或重新启动。我发现程序可以使用signal(SIGTERM,handler)(SIGKILL,handler)。但是,如果用户也使用命令杀死该进程,这两个触发器也会触发。 他们说,在某些解决方案中,可以使用运行级别,但无法运行。在系统初始化运行级别之前,不知道该进程是否被杀死。我什至尝试将脚本放在rcx.d中,但仍然无法正常工作。 有人建议吗

  • 下面是我得到的audit4j:info初始化audit4j······audit4j:信息加载配置...AUDIT4J:信息验证配置...audit4j:info正在初始化处理程序...audit4j:info org.audit4j.core.handler.consoleAuditHandler已初始化。audit4j:info org.audit4j.core.handler.file.Fi

  • 启动电源 按下电源键。 第一次启动主机电源时,请按住电源键5秒钟。电源启动后PS键会亮蓝灯一次,并显示初始设定的画面。请遵循画面指示进行设定。 进入待命模式 按下电源键。此外,购买主机时的设定为,若持续1分钟未执行任何操作,主机将自动进入待命模式。若要解除待命模式,需按下电源键或PS键。 即使进入待命模式,也可使用网络功能。进入待命模式时,若收到最新信息,PS键会亮蓝灯。 主机可能会因目前正在播放

  • 在下面给出的代码中,我基本上使用了get()方法,并在文本视图中显示数据,如果文本视图为空,我想启动另一个活动,我尝试了许多选项,但都不起作用。