Tomcat - starting webapps in a specific order

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


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.


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.


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.