Search code examples
servletswarservletcontextlistener

ServletContextListener execution order for different wars


Is it possible to control order of ServletContextListener execution in different wars?

Suppose there are 3 separate, independent modules A, B, and C in a web service. They can be initialized in any order, and there is no consequences. However, if an optional module D is present, it needs to be initialized first and may impact other modules. Is this configuration possible through Spring? Only thing that comes to mind is having a global ServletContextListener that polls if an init class for every module exists on class path but that solution seems pretty messy.


Solution

  • Managing the startup of applications can be done when they all WAR's are bundled in EAR. It can done by adding <initialize-in-order>true</initialize-in-order> in application.xmkl.

    If all WAR's are independent then there is no standard way of doing it. But, there are container specific features that allow you to do that.

    References:

    If you want to define dependency between the servlet's of an application. You can define Absolute ordering in web.xml or relative ordering in web-fragment.xml.

    Here is how absolute ordering works:

    <web-app>
    ...
        <absolute-ordering>
          <name>A</name>
          <others/>
          <name>B</name>
        <absolute-ordering>
    </web-app> 
    

    If there is no absolute ordering in web.xml, the container will look for relative ordering in web-fragment.xml. It looks something like this:

    <ordering>
        <before>
        <others />
        </before>
    </ordering>