Java Listener not starting Under Tomcat
Question
In my Tomcat logs (catalina) I am getting the following error preventing my application from starting up:
SEVERE: Error listenerStart 24-Mar-2009 13:23:10 org.apache.catalina.core.StandardContext start SEVERE: Context [/exampleA] startup failed due to previous errors
I do not know why I am getting this. In my web.xml I have the following
<listener>
<listener-class>
uk.co.a.listener.SessionListener
</listener-class>
</listener>
<listener>
<listener-class>
uk.co.a.listener.SessionAttributeListener
</listener-class>
</listener>
When I comment out the listeners it starts up fine. The code for the listners are below:
public class SessionAttributeListener implements HttpSessionAttributeListener {
static Log log = LogFactory.getLog(SessionAttributeListener.class.getName());
public void attributeAdded(HttpSessionBindingEvent hsbe) {
log.debug("VALUE attributeAdded to THE SESSION:" + hsbe.getName());
}
public void attributeRemoved(HttpSessionBindingEvent hsbe) {
log.debug("VALUE attributeRemoved from THE SESSION:" + hsbe.getName());
}
public void attributeReplaced(HttpSessionBindingEvent hsbe) {
log.debug("VALUE attributeReplaced in THE SESSION:" + hsbe.getName());
}
}
and
public class SessionListener implements HttpSessionListener {
static Log log = LogFactory.getLog(SessionListener.class.getName());
private static int activeSessions = 0;
public void sessionCreated(HttpSessionEvent evt)
{
activeSessions++;
log.debug("No. of active sessions on:"+
new java.util.Date()+" : "+activeSessions);
}
public void sessionDestroyed (HttpSessionEvent evt)
{
activeSessions--;
}
}
Why is this not starting? Or where can I look for more information?
UPDATE
There only seems to be a problem with SessionAttributeListener from starting up. The SessionListener was not starting up because the <listener> were declared after the <servlet>
UPDATE
There was a problem with the JAR file used. The class for SessionAttributeListener was not included. When it was included the application started.
UPDATE
The AttributeListener does not seem to be running. When it is used the code fails. Is there a simple way to check if a listener is running?
Solution
re your update reading "The AttributeListener does not seem to be running. When it is used the code fails. Is there a simple way to check if a listener is running?" have you tried adding a static initialiser? something like
static {
log.debug("static initialiser called");
}
that way the first time that the class is referenced you should get a log record.
OTHER TIPS
Since I ended up finding my route to a solution elsewhere, I thought it would be useful to update this issue with that information.
It's all to do with the underlying exception which causes the dreaded 'SEVERE: Error listenerStart' message not being logged anywhere, and how to configure the logging to produce the exception.
From here there is a very clear description of the logging issue and a solution.
In my case I went for an even more cut down version, adding
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/web-app].level = FINE
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/web-app].handlers = java.util.logging.ConsoleHandler
to the tomcat conf/logging.properties, and replacing /web-app with the appropriate web application context path.
And magically the hidden exception appeared and told me the Java 6 runtime didn't want to know about code compiled by Java 7. Embarrassing, but easily fixed.
This is applicable to Tomcat 7. Your mileage may vary.
When you encounter "startup failed due to previous errors" in your Tomcat logs, you will either find an exception further up in the log that is causing this issue or you need to fully configure logging within Tomcat such that the exception may be written to the logs. Once you have the root cause written to your logs, resolution is usually trivial.