我有一个运行在Tomcat 8.0上的Java 8 Web应用程序。有一个在项目中定义的上下文监听器类,以下列方式:Tomcat 8上下文初始化两次
类:
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>
当启动应用程序,我在日志中看到上下文侦听器类(以及上下文侦听器实例化的每个其他类)中的每条日志消息都会打印两次:
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
这对我的应用程序来说太可怕了,因为它应该启动一个协调器线程,而这个线程反过来(如其名称所示)将协调其他不同的线程。
我试着阅读Tomcat的配置指南,并尝试使用autoDeploy和deployOnStartup属性以及不同的上下文设置,但无济于事。
的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>
的ResourceLink标签是用于该应用,以及用于某些其它应用JDBC数据源。
的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 "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
它完全 “原创”,只有端口被改变(因为有此服务器上运行的其他雄猫)。
应用程序本身不包含context.xml文件,所以我真的不明白为什么上下文被初始化两次。更奇怪的是,在Tomcat 7上它没有发生,我现在所做的只是将项目的Java版本更改为8,将Tomcat版本更改为8。
是否所有日志条目都重复?它看起来像你展示的部分。 – stdunbar
以调试模式启动它,并确定一个断点。 –
是的,所有日志条目都是重复的。我设法在8.5 Tomcat上的计算机上重现了这个问题(目标系统有8.0 Tomcat)。 –