Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat - starting webapps in a specific order

I know that Tomcat and the Servlet spec do not support starting webapps in a particular order.

However, this seems to me like a common use case, and I'm wondering if someone has discovered a clever workaround for it.

I have webapp A which uses Spring Remoting to expose a shared service, of which webapp B is a client. Webapp B cannot initialize unless webapp A is running. However, my Tomcat is always starting the webapps linearly, starting with webapp B.

For infrastructure reasons I have to have these running on the same Tomcat server.

Any ideas?

Thanks, Roy

UPDATE -

Turns out that in my particular case, order doesn't matter. The reason is this: say I use one of the methods below to start app A before app B. So app A starts, but, since Spring remoting is using the HTTP Invoker, the HTTP port is not yet open (it won't open until all apps are started). So A will start, and B will hang, because the port it's looking for is not yet available. Doh.

End result was two separate Tomcat instances.

like image 638
Roy Truelove Avatar asked Jan 09 '12 19:01

Roy Truelove


People also ask

What is webapps folder in Tomcat?

The webapps directory is where deployed applications reside in Tomcat. The webapps directory is the default deployment location, but this can be configured with the appBase attribute on the <Host> element.

Can I run two web apps in same Tomcat if so how?

Simply drop both war files into Tomcat's webapps folder. That is all you need to do. By default, Tomcat expands ("explodes" some say) each war (technically a zip file) into a folder and automatically deploys the app for you. This happens on the fly if Tomcat is already running, or on startup when you launch Tomcat.


2 Answers

That's quite easy to achieve if you don't care hacking a bit of tomcat code and creating your own Host instance

1) Create a subClass of org.apache.catalina.core.StandardHost, say MyHost:

    class MyHost extends org.apache.catalina.core.StandardHost{
        public MyHost (){
        super();
        //changing HashMap for a predictable ordered Map :)
        this.children = new LinkedHashMap();
        }
    } 

2) register your class on your server's xml Host tag ()

Incredible as it may seem, it solves the problem as long as you have all your web app declared in the correct order inside of Host tag:

    <Host>
     <context app1>
     <context app2>
   </Host>

Thaen app1 will start before app2, no matter which SO you used.

like image 26
Luiz Henrique Martins Lins Rol Avatar answered Sep 21 '22 08:09

Luiz Henrique Martins Lins Rol


We have the same problem and to solve it we're relying on the fact (slippery, I know) that applications are started in the order they are defined in <tomcat_home>/conf/server.xml.

This of course has a disadvantage of hardcoding apps in the server.xml but we can live with it.

like image 55
mindas Avatar answered Sep 20 '22 08:09

mindas