3

Suppose I have two web apps to deploy in Tomcat 6, packed as, let us say, A.war and B.war. Is there a way to force Tomcat, when restarting, to:

  • load B before A? or
  • load A after everything else? or
  • make A loadable only after B is loaded - that is, make B a dependency of A?

These are the questions. Below comes a background that may be too complex but also very useful.

Background

I am trying to deploy some portlets in Liferay. These portlets are legacy code made by people who did not knew a lot about how Liferay works and made, let us say, "amazing" decisions, such as to map Liferay Service Builder entity tables to new classes. This does not come too much in case, actually, but someone who knows Liferay may understand my point a bit better.

These portlets are seamed by Spring and mapped to database by Hibernate. To avoid the annoying requirement of editing context.xml (which is tiresome, error prone, easily forgettable, makes the persistence layer slow as hell etc.) I provided the Liferay datasource as a new bean...

<bean id="liferayDataSource" 
    class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
        <bean
            class="com.liferay.portal.kernel.util.InfrastructureUtil"
            factory-method="getDataSource" />
    </property>
</bean>

...and used this new bean as the datasource of the Entity Manager Factory:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="liferayDataSource"/>
    <property name="persistenceUnitName" value="liferay-db" />
</bean>

If I have the Liferay running and I deploy my portlets, it runs without problem because the Liferay resources are already available. The problem is, when I restart Liferay these portlets are loaded before the Liferay app going up, so the InfrastructureUtil class is not even loaded yet. So, I want to postpone the loading of the portlets.

I noted that each application has a directory under $CATALINA_HOME/temp/, usually called something like 0-this-annoying-portlet, 2-that-weird-portlet etc. I believe the number at the beginning is what determines the order, since it forces an alphabetical sorting of applications. Is that true? Can I set this number?

Of course, I am receptive to any solution but I am really curious about the possibilities of defining an ordering of application loading in Tomcat.

Dani
  • 3,744
  • 4
  • 27
  • 35
brandizzi
  • 26,083
  • 8
  • 103
  • 158

3 Answers3

1

The problem was that, in the Liferay 6.0 SP2 bundle, Tomcat was deploying an XML context file for each portlet. So when I deployed my annoying-portlet, a $TOMCAT/conf/Catalina/localhost/annoying-portlet.xml file is created. When Tomcat is restarted, these XML context files are read, the respective projects are loaded (incidentally in alphabetical order) and the portlet is started before Liferay, whose context name is ROOT.

As @MarceloBezerra first link made clear, the $TOMCAT/conf/Catalina/localhost/annoying-portlet.xml will have precedence (in the sense that all contexts with a file in $TOMCAT/conf/Catalina/localhost/ will be loaded before the contexts which just has a directory in $TOMCAT/webapps. The solution is to inhibit the deployment of the $TOMCAT/conf/Catalina/localhost/annoying-portlet.xml file.

How to do it? Well, after some time talking to the Liferay great staff, they discovered that the context XML deployment can be inhibited by adding a deployXML="false" attribute to the Host element from $TOMCAT_HOME/conf/server.xml:

<Host autoDeploy="true" 
    appBase="webapps" name="localhost" unpackWARs="true" 
    xmlNamespaceAware="false" xmlValidation="false">
brandizzi
  • 26,083
  • 8
  • 103
  • 158
1

Liferay can be forced to be the first application loaded defining its context in the file $TOMCAT/conf/server.xml, inside the tag:

<Host autoDeploy="true" 
appBase="webapps" name="localhost" unpackWARs="true" 
xmlNamespaceAware="false" xmlValidation="false">
   ....
   <Context path="" crossContext="true" docBase="ROOT">
       ....
   </Context>
   ....
</Host>

And deleting the file $TOMCAT/conf/Catalina/localhost/ROOT.xml

dpinya
  • 542
  • 7
  • 16