The Vaadin Wiki is temporarily in read only mode due to large recent spam attacks.

Cleaning up resources in a UI

(Redirected from Cleaning up resources in an UI)
Tags: vaadin 7.0.0
WARNING: This wiki page was last edited over a year ago and might be outdated.

Vaadin

UI
s that are open on the client side send a regular heartbeat to the server to indicate they are still alive, even though there is no ongoing user interaction. When the server does not receive a valid heartbeat for a given UI, it will eventually remove that UI from the session.

By default, heartbeats are sent every five minutes, and the server closes an UI after three missed heartbeats. The heartbeat interval can be customized by providing an init parameter named

heartbeatInterval
in your web.xml, setting its value to the desired interval in seconds:

#!xml
<web-app>
    <context-param>
        <param-name>heartbeatInterval</param-name>
        <param-value>120</param-value>
    </context-param>

    <!-- Or per-servlet: -->
    <servlet>
        <!-- ... -->
        <init-param>
            <param-name>heartbeatInterval</param-name>
            <param-value>120</param-value>
        </init-param>
    </servlet>
</web-app>

To do custom cleanup in your UIs,

DetachListener
s can be registered:

#!java
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest request) {
        addDetachListener(new DetachListener() {

            @Override
            public void detach(DetachEvent event) {
                releaseSomeResources();
            }
        });
    }

    private void releaseSomeResources() {
        // ...
    }

Of course, your UI could also implement

DetachListener
itself:

#!java
public class MyUI extends UI implements DetachListener {
    protected void init(VaadinRequest request) {
        addDetachListener(this);
    }

    @Override
    public void detach(DetachEvent event) {
        // do cleanup
    }
}

If you'd like to share a listener instance between multiple UIs, the current UI can be queried:

#!java
    public void detach(DetachEvent event) {
        // do cleanup
        event.getConnector().getUI();

        // or equivalent:
        UI.getCurrent();
    }

Because heartbeat requests are just like any other request from the servlet container's viewpoint, each heartbeat extends the lifetime of the underlying HttpSession. This means that as long as there is an open UI, the session never expires even though there is no user interaction. This is desirable in some situations, but in others it may be not.

You can control this behavior by setting an init parameter named

closeIdleSessions
to
true
. When it is set to true (the default is
false
), the session will be closed if no UI is active. Before the session is closed, the detach methods will be called, and cleanup is performed.

#!xml
<context-param> <!-- or init-param -->
    <param-name>closeIdleSessions</param-name>
    <param-value>true</param-value>
</context-param>
0 Attachments
26787 Views
Average (2 Votes)
Comments
What is the difference between implementing the detach() method in UI (inherited from ClientConnector) and registering a detach listener?
Posted on 8/5/13 6:37 AM.
Functionally, none at all.
Posted on 4/25/14 10:04 AM in reply to Agata Vackova.