在ServletContextListener中发生异常时停止部署

时间:2014-11-19 08:32:28

标签: java servlets weblogic

我有以下web.xml配置,其中在contextInitialized()方法的com.mkyong.context.ContextSecond抛出异常。在这种情况下,部署不会停止,app.war将继续提供服务。发生异常时如何停止部署?

使用weblogic 12c

    Archetype Created Web Application

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>com.mkyong.context.ContextFirst</listener-class>
</listener>

<listener>
    <listener-class>com.mkyong.context.ContextSecond</listener-class>
</listener>

ContextFirst .java

package com.mkyong.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextFirst implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("ContextFirst:contextDestroyed");

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {

        System.out.println("ContextFirst:contextInitialized");

    }

}

ContextSecond .java

package com.mkyong.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextSecond implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("ContextSecond:contextDestroyed");

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {

        System.out.println("ContextSecond:contextInitialized");
        throw new RuntimeException();

    }

}

日志:

<Nov 19, 2014 1:52:36 PM EET> <Warning> <Deployer> <BEA-149124> <Failures were detected while initiating remove task for application "webservices". Error is: "[Deployer:149001]No a
pplication named "webservices" exists for operation "undeploy".">
ContextFirst:contextInitialized
ContextSecond:contextInitialized
<Nov 19, 2014 1:53:05 PM EET> <Warning> <HTTP> <BEA-101162> <User defined listener com.mkyong.context.ContextSecond failed: java.lang.RuntimeException.
java.lang.RuntimeException
        at com.mkyong.context.ContextSecond.contextInitialized(ContextSecond.java:18)
        at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:678)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)

0 个答案:

没有答案