Tomcat Initialization Health Check Lifecycle Listener
There is no easy way to check from a shell script if all Tomcat components (i.e.: Server, Service(s), Connector(s), Executor(s), Container and Context(s)) initialized properly during startup.
This lifecycle listener waits for a Lifecycle.AFTER_START_EVENT
from the Server component. When received, the LifeCycleState
of each Tomcat component is checked.
Optionally, it is possible to notify an external process (e.g.: the startup script) via a FIFO or with a UDP packet and/or trigger a server shutdown upon failure.
This version (8.0.32) was built and tested against Tomcat 8.0.32, but should work with newer versions as well.
git clone https://github.com/goldendeal/tc-init-health-check-listener.git
cd tc-init-health-check-listener
mvn package
<dependency>
<groupId>gr.xe</groupId>
<artifactId>tc-init-health-check-listener</artifactId>
<version>8.0.32</version>
<packaging>jar</packaging>
</dependency>
Copy tc-init-health-check-listener/target/tc-init-health-check-listener-<version>.jar
to ${CATALINA_HOME}/lib
or ${CATALINA_BASE}/lib
In your server.xml add a server lifecycle listener:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005">
<Listener className="gr.xe.dc.tomcat.listeners.InitHealthCheckListener"/>
...
</Server>
Attribute | Type | Default Value | Description |
---|---|---|---|
shutdownOnFailure | boolean | true | Trigger server shutdown on failure |
notifyFIFO | String | none | A FIFO where the initialization status will be written |
notifyAddress | String | localhost | Destination address of the UDP packet containing the initialization status |
notifyPort | Integer | Same as Server shutdown port | Destination port of the UDP packet containing the initialization status |
The following have been tested with bash.
If using notifyFIFO:
read -t <timeout> STATUS <> /path/to/FIFO
if [ $? -ne 0 ]; then
echo "Timed out"
exit 1
fi
if [ "${STATUS}" -eq 0 ]; then
echo "Success"
exit 0
else
echo "Failure"
exit 1
fi
If using notifyAddress and notifyPort:
STATUS=$(timeout <timeout> socat UDP-RECVFROM:<notify port> STDOUT)
if [ $? -ne 0 ]; then
echo "Timed out"
exit 1
fi
if [ "${STATUS}" -eq 0 ]; then
echo "Success"
exit 0
else
echo "Failure"
exit 1
fi