Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 8 context initialized twice

I have a Java 8 web application running on Tomcat 8.0. There is a context listener class defined in the project, in the following way:

Class:

public class ...ContextListener implements ServletContextListener {
    private static Log log = LogFactory.getLog(...ContextListener.class);

    public void contextInitialized(ServletContext sce) {
        log.info("HOSTNAME:" + ...Utils.getHostName());
        ...
    }
}

web.xml:

<web-app ...>
    <listener>
        <listener-class>...ContextListener</listener-class>
    </listener>
</web-app>

When launching the application, I see in the logs that every log message in the context listener class (and in every other class instantiated by the context listener) is printed twice:

03/02/2017 17:47:07  INFO - HOSTNAME:...win
03/02/2017 17:47:07  INFO - HOSTNAME:...win
03/02/2017 17:47:07  INFO - Starting Coordinator ...
03/02/2017 17:47:07  INFO - Starting Coordinator ...
03/02/2017 17:47:07  INFO - Coordinator.start was called
03/02/2017 17:47:07  INFO - Coordinator.start was called
03/02/2017 17:47:07  INFO - Coordinator was started!
03/02/2017 17:47:07  INFO - Coordinator was started!
03/02/2017 17:47:11  INFO - Setting the workCounter=0
03/02/2017 17:47:11  INFO - Setting the workCounter=0

This is terrible for my application, because it is supposed to launch a single Coordinator thread, which in turn (as its name suggests) will coordinate other, different threads.

I tried reading Tomcat's configuration guide, and tried playing around with autoDeploy and deployOnStartup properties, as well as different context setups, but to no avail.

Tomcat/conf/context.xml:

<Context>
    ....
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <ResourceLink name="..." global="..." type="javax.sql.DataSource"/>
    <ResourceLink name="..." global="..." type="javax.sql.DataSource"/>
    <ResourceLink name="..." global="..." type="javax.sql.DataSource"/>
    <ResourceLink name="..." global="..." type="javax.sql.DataSource"/>
    <ResourceLink name="..." global="..." type="javax.sql.DataSource"/>
    ...
</Context>

The ResourceLink tags are JDBC data sources for this application as well as for some other applications.

Tomcat/conf/server.xml:

<Server port="9006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>

<Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

    <Resource name="..." auth="Container" type="javax.sql.DataSource"
           factory="...EncryptedDataSourceFactory"
           username="..." password="..."
           driverClassName="oracle.jdbc.driver.OracleDriver"
           url="..." maxWait="1000" removeAbandoned="true"
           maxActive="100" maxIdle="5" removeAbandonedTimeout="60"
           logAbandoned="true" validationQuery="SELECT 1 from DUAL"
           testOnBorrow="true" testWhileIdle="true" numTestsPerEvictionRun="3" timeBetweenEvictionRunsMillis="30000"
           minEvictableIdleTimeMilli="150000" defaultAutoCommit="true"/>      

    <Resource name="..." auth="Container" type="javax.sql.DataSource"
           factory="...EncryptedDataSourceFactory"
           username="..." password="..."
           driverClassName="oracle.jdbc.driver.OracleDriver"
           url="..." maxWait="1000" removeAbandoned="true"
           maxActive="100" maxIdle="5" removeAbandonedTimeout="60"
           logAbandoned="true" validationQuery="SELECT 1 from DUAL"
           testOnBorrow="true" testWhileIdle="true" numTestsPerEvictionRun="3" timeBetweenEvictionRunsMillis="30000"
           minEvictableIdleTimeMilli="150000" defaultAutoCommit="true"/>

  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="9081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="9444" />

    <Connector port="9010" protocol="AJP/1.3" redirectPort="9444" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

It is completely "original", only the ports are changed (because there are other tomcats running on this server).

The application itself doesn't contain a context.xml file, so I really don't understand why the context is initialized twice. Even stranger, on Tomcat 7 it didn't happen, all I did now was change the project's Java version to 8 and Tomcat version to 8.

I'd appreciate any help you could provide.

like image 589
Gábor Major Avatar asked Feb 24 '26 22:02

Gábor Major


1 Answers

Thanks to pedrofb's comment, I realized I had duplicate log entries only, and not double initialization. The log4j config was like this:

log4j.logger...package.subpackage1=INFO,LOGFILE
log4j.logger...package.subpackage2=INFO,LOGFILE

log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
...

And that's why log messages appeared twice.

like image 196
Gábor Major Avatar answered Feb 27 '26 12:02

Gábor Major