Tomcat Startup Takes Forever

The Problem

So you install your new machine, with Java, Tomcat and MySql and then you start everything. You use the very latest version of these products (currently Tomcat 8 and JDK1.8) and you expect the best. However, Tomcat starts very very slowly - anything between 2 minutes and 30 minutes.

Since you worked with Tomcat in the last four - five years or even more and usually it would start in seconds, you start checking everything, including memory, firewall (iptables) etc. Nothing.

Then you figure out it is not that it does not start at all, it starts eventually but it takes forever.

Here is the cause, at least this is what happened to me:

org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /apps/apache-tomcat-8.0.14/webapps/ROOT
org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [343,120] milliseconds.
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /apps/apache-tomcat-8.0.14/webapps/ROOT has finished in 343,943 ms
org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]

So the issue is that the SecureRandom class from JDK takes forever to generate a new value.

The fix

Add a system setting that would change the random generator for the SecureRandom
-Djava.security.egd=file:/dev/./urandom

Personally I added a line like that in catalina.sh, just under block of comments at the beginning

JAVA_HOME=/apps/jdk1.8.0_25
CATALINA_HOME=/apps/apache-tomcat-8.0.14
JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"

Now it works perfect:

org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
org.apache.catalina.startup.Catalina.start Server startup in 1054 ms