I ended up doing the following:
Both Tomcats are always started. The application is also started on both of the Tomcats.
A special synchronization service
was hand-written. This service is part of the application and started along with the application startup. It is responsible for determination of the node that is currently active and which one should be active.
My application used a Spring context for managing my services lifecycle. I separated it into two different contexts: a wrapper
Spring context and a child
Spring context.
The wrapper
context was started along with the application startup, always available and working on both Tomcats. The synchronization service was configured as a bean in the wrapper
context.
The child
context actually included all services that my application provided for external clients. This is the context that I designated to be started only when current node is active and stopped when becoming passive.
How was the child
context swithched on / off when active / passive?
This context wasn't started automatically along the application startup. When the synchronization service
concluded that the current node should become active, an event was raised that caused the child
context to get started, thus making all services available on that node. And the other way around: when the synchronization service
concluded that the current node shuold become passive, a proper event was raise that caused the child
context to shutdown, making all services unavailable on that node.
Another important feature that was added was an interceptor
for incoming requests. An incoming request hitting the one of the Tomcats, actually implies that the load balancer / web server has decided that the target node is the currently available or preferred node. In such a case, even if it happened that the target node was the passive one, it should now become active. So the intercepror
would raise the same event that the synchronization service
did, again making the child
context to start and the services available on that node (and the synchronization serivce
s on both Tomcats identified the switch and the formerly active node would passivate).