-->

Start webapps in tomcat in a determinate order

2020-02-12 03:33发布

问题:

I have 2 webapps:

WebApp 2 depends from WebApp 1

If WebApp 1 is not running, WebApp 2 fails

Can I define in tomcat that I always want that webapp 1 starts before webapp 2?

回答1:

According to the Apache wiki (at http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q27):

There is no expected startup order. Neither the Servlet spec nor Tomcat define one. You can't rely on the apps starting in any particular order.



回答2:

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());strong text



回答3:

Liferay seems to have overridden HostConfig in a way that makes this possible. The basic idea is to extend HostConfig, and then override either deployApps or the individual methods deployDescriptors, depoyWars and deployDirectories to sort applications in the way you want them. Then modify Tomcat's conf/server.xml by adding the attribute hostConfigClass to the Host element.

See http://www.javadocs.com/docs/com.liferay.portal/support-tomcat/6.2.0/com/liferay/support/tomcat/startup/PortalHostConfig.java for details.