Tomcat - starting webapps in a specific order

前端 未结 9 618
既然无缘
既然无缘 2020-12-05 07:51

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 i

相关标签:
9条回答
  • 2020-12-05 08:36

    Can it be that they are starting in the order as they are deployed in?

    So if i have four apps: 1, 2, 3, 4 and i deploy them in the following order: 1, 2, 4 (restart tomcat), 3 (restart). That it caches something somewhere to make the projects always startup in that order? So 1, 2, 4, 3. It happens over here. I'm unsure, but maybe it helps someone.

    0 讨论(0)
  • 2020-12-05 08:45

    Here is another trick on Linux.

    Some of our webservice applications fail to deploy, because of erroneous WSDL. This happens if they are deployed or started after a number of other applications. The order in which they are started depends on the order in which context xml's are found in /opt/apache-tomee/conf/Catalina/localhost

    Can be verified using "ls -1f". A plain "ls" gives a sorted output.

    This used to be the order in which files were added to that directory, but with ext4 filesystems, the order is based on a hash of the filename. This can be disabled as follows:

    # tune2fs -O ^dir_index /dev/xyz
    

    Now you can at least decide yourself in which order they will be started. Reordering: move all files to a temporary folder, move them back in the desired sequence.

    0 讨论(0)
  • 2020-12-05 08:51

    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.

    0 讨论(0)
  • 2020-12-05 08:52

    Old thread, but...

    Another way to work around this is to create a custom HostConfig class which sorts the web apps the way you need.

    public class OrderedHostConfig extends HostConfig {
    
        @Override
        protected String[] filterAppPaths(String[] unfilteredAppPaths) {
            String[] files = super.filterAppPaths(unfilteredAppPaths);
            Arrays.sort(files, compare());
            return files;
        }
    
        private Comparator<String> compare() {
            return (o1, o2) -> {
            // Your own implementation 
            };
        }
    }
    

    Then you can reference this class in your server.xml under the Host definition.

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" hostConfigClass="your.package.OrderedHostConfig">
    

    You need to compile this into a jar and save it in Tomcat's /lib directory. In my case:

    /var/lib/tomcat8/lib

    I like this method because:

    1. The order is enforced only where it's relevant, but there is no need to manage all the webapps manually in Host definitions using Context

    2. Searching for the .war file name in the codebase will also reference this class, which makes it easier to find if the file is renamed.

    3. No changing of private final fields for tomcat 8, see here

    0 讨论(0)
  • 2020-12-05 08:52

    I know this question is a bit old, but I found it when trying to do the same thing and thought I'd update with a better solution...

    You can define mulitple services in your server.xml, which run on different ports. The services are started sequentially according to the order they appear in the server.xml. This means that you can have - for example - a configuration service running in the first service and then the apps that depend on it in the second (I use the default Catalina one for the rest of them...)

    You can see more info here: http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q27

    And this is the service that I include before the Catalina Service:

    <Service name="ConfigService">
        <Connector port="8081" protocol="HTTP/1.1"
            connectionTimeout="20000"
            redirectPort="8444" />
        <Engine name="ConfigServiceEngine" defaultHost="localhost">
            <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true"
                xmlValidation="false" xmlNamespaceAware="false">
    
                <Context path="/" reloadable="true" docBase="/path/to/your/service/directory" />
            </Host>
        </Engine>
    </Service>
    

    As you can see, I use docbase rather than appBase, but you should be able t configure a different appBase if you prefer...

    NB it's important to change the name of both the service and the engine.

    HTH

    0 讨论(0)
  • 2020-12-05 08:53

    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.

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