4

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)
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • If you have any servlet in your app it will not work. Can you try the servlet? But it seems that you are using web services, and web services are working? – user987339 Nov 19 '14 at 14:28
  • I have an other listener (ContextLoaderListener for Springframwork) and their order is given above. When ContextSecond throws and exceptions, this does not effect the first ContextListener(Spring) and webservices or rest services provided by springframwork keep servicing. – Ahmet Karakaya Nov 19 '14 at 14:34
  • What happens if your force call a `destroy()` rather than `throw new RuntimeException();` – Display Name is missing Nov 19 '14 at 18:29
  • which class belongs to destroy() method? How can I can invoke it? – Ahmet Karakaya Nov 19 '14 at 22:10
  • possible duplicate of [How can I make a ServletContextListener stop the Java EE application?](http://stackoverflow.com/questions/6657319/how-can-i-make-a-servletcontextlistener-stop-the-java-ee-application) – javabrett May 26 '15 at 04:18

0 Answers0