[英]Tomcat 8 context initialized twice
I have a Java 8 web application running on Tomcat 8.0. 我有一个在Tomcat 8.0上运行的Java 8 Web应用程序。 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.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. 这对我的应用程序来说很糟糕,因为它应该启动单个Coordinator线程,而该线程又会协调其他不同的线程(顾名思义)。
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的配置指南,并尝试使用autoDeploy和deployOnStartup属性以及其他上下文设置,但无济于事。
Tomcat/conf/context.xml: 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. ResourceLink标记是此应用程序以及某些其他应用程序的JDBC数据源。
Tomcat/conf/server.xml: 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>
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. 应用程序本身不包含context.xml文件,因此我真的不明白为什么将上下文初始化两次。 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.
甚至更陌生,在Tomcat 7上也没有发生,我现在要做的就是将项目的Java版本更改为8,将Tomcat版本更改为8。
I'd appreciate any help you could provide. 谢谢您能提供的任何帮助。
Thanks to pedrofb's comment, I realized I had duplicate log entries only, and not double initialization. 感谢pedrofb的评论,我意识到我只有重复的日志条目,而不是双重初始化。 The log4j config was like this:
log4j配置是这样的:
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. 这就是为什么日志消息出现两次的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.