How to control webapp deployment order when restarting tomcat

前端 未结 5 631
终归单人心
终归单人心 2020-12-20 03:09

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

相关标签:
5条回答
  • 2020-12-20 03:42

    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).

    0 讨论(0)
  • 2020-12-20 03:47

    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.

    0 讨论(0)
  • 2020-12-20 03:51

    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

    0 讨论(0)
  • 2020-12-20 04:00

    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.

    0 讨论(0)
  • 2020-12-20 04:01

    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.

    0 讨论(0)
提交回复
热议问题