Getting EHCache to shutdown cleanly in a webapp

I was seeing an annoying issue with a webapp having trouble shutting down cleanly. I would issue the tomcat stop command and most everything would shutdown, but I’d get this error:

Exception in thread "Multicast Heartbeat Receiver Thread" java.lang.NullPointerException
 at org.slf4j.impl.Log4jLoggerAdapter.error(
 at net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver$

After the error, the Multicast Heartbeat Receiver would not shutdown and would just hang. Eventually you would have to kill off the java process manually.

Doing a quick search found this page. With a small snippet added to the web.xml to add a shutdown listener everything shutdowns cleanly now.