Stop deployment when Exception occurs in ServletContextListener

孤街浪徒 提交于 2019-12-13 15:06:45

问题


I have following web.xml configuration where exception is thrown at com.mkyong.context.ContextSecond in contextInitialized() method. In that case deployment does not stop and app.war keep servicing. How I can stop deployment when exception occurs?

Using 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();

    }

}

Log:

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

来源:https://stackoverflow.com/questions/27012166/stop-deployment-when-exception-occurs-in-servletcontextlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!