Question

I am using Jetty server and I want to put one check at the time of server start. For that I have created one listener class and put its entry in web.xml as per following:

Listener Class:

public class LicenseCheck extends ContextLoaderListener
{
    private static final Logger log = Logger.getLogger(LicenseCheck.class);

    public LicenseCheck()
    {
        log.info("Checking license");
    }
}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


  <!-- spring context listener -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
  </context-param>


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

  <listener>
        <listener-class>com.license.LicenseCheck</listener-class>
    </listener>


   <!-- Index pages -->
   <welcome-file-list>
     <welcome-file>index.html</welcome-file>
   </welcome-file-list>

</web-app>

But after adding this, my application cannot start and its throwing following exception:

HTTP ERROR: 503

Problem accessing /. Reason:

      Service Unavailable 

Powered by Jetty://

If I remove the listener's entry from web.xml it works fine.

Please provide solution for this.

Was it helpful?

Solution

I Got the solutions from log:

Log says:

2014-04-03 18:50:41.969:WARN::Failed startup of context org.mortbay.jetty.webapp.WebAppContext@58afc4dd{/SpringMVCRest,D:\Workspace\SpringMVCRest\WebContent} java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml! at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:265) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:549) at org.mortbay.jetty.servlet.Context.startContext(Context.java:136) at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1282) at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:518) at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130) at org.mortbay.jetty.Server.doStart(Server.java:224) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at runjettyrun.Bootstrap.main(Bootstrap.java:97) 2014-04-03 18:50:42.047:INFO::Started SelectChannelConnector@0.0.0.0:8080

So I found 2 solutions:

  1. Remove the inheritance of ContextLoaderListener in my class.
  2. Keep the inheritance and remove entry of ContextLoaderListener from web.xml

Both worked for me.

OTHER TIPS

Listeners are called in the order you specify it in web.xml.

    <listener>
        <listener-class>com.license.LicenseCheck</listener-class>
    </listener>

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

    public class LicenseCheck  implements   ServletContextListener {
        public void contextInitialized(ServletContextEvent event) {      
        /* This method is called when the servlet context is         
            initialized(web app is deployed/started).          
            You can initialize servlet context related data here.      
        */     
        //Write your logic to check license expiry  
        }    

        public void contextDestroyed(ServletContextEvent event) {      
            /* This method is invoked when the servlet context ( web app) is undeployed or server shuts down.      
            */                  
            //Do nothing
        }   
    }

This way LicenseCheck will execute first and check expiry even before applicationContext is loaded by ContextLoaderListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top