2

I have a number of war projects deployed in a single tomcat 5.5 container. They consume each other's services through http, and thus I need to make sure that, when Tomcat is restarted, they are deployed in an specific order. After a couple of hours googlin' around, no luck.

Anyone knows how to setup tomcat 5.5 to deploy wars on restart in an specific order?

Thanks in advance

artejera
  • 23
  • 1
  • 1
  • 3

5 Answers5

6

Don't listen to "Frankly Speaking" and "Re-Structure", they're obviously working in an environment where they are in full control. I work on a massively distributed system, we have web applications scattered on hundreds of machines, some of these web applications are from other vendors so I can't "Re-structure".

In order for many services to start up, they need to talk to other services to get configuration information. If that service is not there, the new service can't start. In production, there are things that are really never down (load balanced, HA, etc), but when I need to get a dev environment set up on my laptop, I run into this problem.

  • The easiest solution I found was to name my webapps alphabetically in the order they are to start (or add an extra letter to the beginning of the webapp if you want the webapp name to be reasonable similar to your production webapps).

  • You can also use multiple Tomcat installations (organize your webapps and start the Tomcat instances in the proper sequence), but that's a lot of overhead.

  • A final option would be to use your startup script to deploy your .war files in the appropriate order (with enough sleep time in between to make it work).

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
JasBro
  • 69
  • 1
  • 2
  • 2
    And all is well until something takes longer than you expected to start, or something's added that you didn't anticipate (or you port to an OS that lists files in a different order; last i heard, alphabetical order is not guaranteed!). Then race conditions and errors abound. Maybe you're happy dealing with that, but i wouldn't be. Better to fix the app than duct-tape it like this. Period. – cHao Nov 17 '11 at 10:40
  • Agree with cHao - "frankly speaking" :) – RN. Dec 06 '12 at 17:46
3

It is true that tomcat does not provide any way to enforce deployment order.

Tomcat deploys webapps in following order:

1.Any Context Descriptors will be deployed first.

2.Exploded web applications not referenced by any Context Descriptor will then be deployed. If they have an associated .WAR file in the appBase and it is newer than the exploded web application, the exploded directory will be removed and the webapp will be redeployed from the .WAR

3.WAR files will be deployed

Here is a proposed solution:

If you want to specify the deployment order then define a context for your web app in $CATALINA_BASE/conf/[enginename]/[hostname]/MyApp.xml

Tomcat scans $CATALINA_BASE/conf/[enginename]/[hostname]/ by performing File listFiles() which returns a File array sorted by hash value (OS dependent).

You may use the following code to check in which order webapps will be deployed

File file = new File("/opt/tomcat/conf/Catalina/localhost");
        File[] files = file.listFiles();
        for (File f : files)
        {
            System.out.println("Filename: " + f.getName());
        }

Naming deployment descriptor appropriately will solve your problem.

Prasoon Dwivedi
  • 119
  • 1
  • 7
1

Re-structure your apps into a core-plus-addons. Put the core code into the shared/lib folder and the webapps can access it from there.

jqa
  • 1,340
  • 6
  • 17
0

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>

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

-7

Frankly speaking, you need to revisit your architecture.

Your applications are too tightly coupled with each other.

You should probably have some controller application and have all applications register with it or something like that.

This is just a shot in the dark without knowing too much about your specific problem

RN.
  • 997
  • 4
  • 14
  • 31
  • it's not always as simple as restructuring. What if the person (like me) has a mock server that needs to be deployed before project code? Please answer the question about how to control the deployment order. – vinnybad Aug 06 '12 at 22:00
  • Doesn't answer the question. – user43685 Dec 06 '12 at 14:17
  • 1
    You are right- it doesn't. So if its a down vote on that- right on. That said - all i am saying is- that if you have an architecture which depends upon a certain order by which the apps have to come up - its a smell. Not sure what that means- but it ain't right. I am sure you can find a solution to the problem posted. I am just pointing out you are chasing the wrong problem., And once again- agree i didn't answer the question. – RN. Dec 06 '12 at 17:45
  • There can be many reasons other than the asker's scenario where somebody wants a specific order of deployment. For an example I wanted for my war A to deploy before B because, A is the one responsible for running the Schema Upgrade Tasks. If B runs first, then B fails deployment altogether. – retromuz Jan 06 '17 at 07:38
  • Doesn't answer the question at all. I managed the same issue by renaming .war files. – retromuz Feb 11 '17 at 21:12