Where to put my clean-up code to run when app ends?

How can I get some code to run when the run of my Vaadin 7 app ends?

I need to do some clean-up such as free up resources, unregister a JDBC driver, and so on.

Application Lifecycle
chapter of the Book of Vaadin 7 tells how to write code that causes a session to be closed. But how do i write code that reacts to a session ending?

–Basil Bourque

You could implement HttpSessionListener and in its sessionDestroyed() method do whatever you need.


I haven’t tried it, but you should be able to do it with a SessionDestroyListener, which you can add to the service in the initialization of the servlet. You need a custom servlet to do that.

Something like:

class MyServlet extends VaadinServlet {
    protected void servletInitialized() throws ServletException {
        VaadinService.getCurrent().addSessionDestroyListener(new SessionDestroyListener() {

To Marko Grönroos: Your answer confused me because of subclassing
. How is subclassing
even possible much less recommended?

Anyways, I found the methods you mentioned on the VaadinService class API doc:

(SessionDestroyListener listener)

(SessionInitListener listener)

I’m still not clear on the lifecycle of Vaadin 7.

• What is the difference/relationship between

• For posterity: After some confusion, I see now that

Documentation for Vaadin 7.0.0 class VaadinServiceSession.
has been renamed and slightly modified to become VaadinSession by Vaadin 7.0.1 & 7.0.2.