Archive for February, 2011

Solve webapp random startup errors (connection time out) and slowness

February 16th, 2011

When developping Java web apps, you may sometime see random startup errors like this one :

java.lang.IllegalStateException: Unable to instantiate container.
at org.apache.tiles.web.startup.TilesListener.contextInitialized(TilesListener.java:60)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:549)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1282)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:518)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499)
(...)

Most of the time your application works, but sometimes it just does not start.

This is because of the dtd resolution mecanism: if you use the reference of a dtd which is not in the webapp classpath, the container will try to get it from internet.

If the remote site is down (in this case http://tiles.apache.org), your app will no longer start.

Sadly, it’s easy to get in this situation.

  • Upgrade a library without updating xml headers
  • Copy/Paste a snippet from the elsewhere

… and the container will start to fetch dtds from the internet.

In my case, the previous error was caused by :

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>
(...)
</tiles-definitions>

instead of :

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
(...)
</tiles-definitions>

With tiles 2.0.6 in the classpath.

Conclusion
Be sure to use the exact version of the dtd in your classpath. Don’t pickup examples from the internet without double-checking the dtd.
This will prevent longer startup times and potential random errors.

Note: this post dedicated to Olivier and Jimmy :-)