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.

The
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.

Hi,

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 {
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        VaadinService.getCurrent().addSessionDestroyListener(new SessionDestroyListener() {
            ...
        };
    }
}

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

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


addSessionDestroyListener
(SessionDestroyListener listener)

addSessionInitListener
(SessionInitListener listener)

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

• What is the difference/relationship between
VaadinSession
and
VaadinService
?

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

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