How do I hook a method call on the server shutdown?

I tried to use ServletContextListener since Vaadin uses Springboot but I wasn’t successful. I also looked at VaadinServiceInitListener but that didn’t seem right. I also looked at overriding a method in the Application (aka) SpringBootServletInitializer but the closest I found was onStartup(). I also looked at the online documentation but wasn’t sure. Any help would be appreciated.

/**
* Adds a service destroy listener that gets notified when this service is
* destroyed.
*


* The listeners may be invoked in a non-deterministic order. In particular,
* it is not guaranteed that listeners will be invoked in the order they
* were added.
*
* @param listener
* the service destroy listener to add
* @return a handle that can be used for removing the listener
* @see #destroy()
* @see ServiceDestroyListener
*/
public Registration addServiceDestroyListener(ServiceDestroyListener listener)

But it really depends on what you are actually doing whether it is correct to do this using Vaadin API’s or some other API’s like Spring API’s.

Note, if your class where you need this is a ApplicationScope Service bean, you could also use @PreDestry method there for cleanup.

Thank you. That helped a lot.