Jiasheng Qin
Jiasheng Qin

Reputation: 55

Detach event in Vaadin web app not getting called

My question stems from an issue almost identical to the one here (which did not end up getting a satisfactory answer):

https://vaadin.com/forum/thread/13932610

Like this person, I expected that upon closing the browser that my app was open in, a detach event would proc; however, this did not happen. I've tried adding a detach listener, overriding the detach method, and doing both at the same time, but none of them were successful. As for how I know the detach event was not called, my detach event is a simple print statement - that does not show up in the output.

Note that like in the aforementioned thread, I've already set the heartbeat interval (2 seconds in my case) and set closeIdelSessions to be true. So, I thought I would just have to wait six seconds, but that's certainly not been the case.

Upvotes: 1

Views: 1063

Answers (1)

Tatu Lund
Tatu Lund

Reputation: 10643

When I try this (find the essential parts of the code below), the detach() is eventually being called. I run this with Jetty, and I did not touch its default. It tooks some ~45 minutes after closing the Browser, when I saw "Detach called" logged on console. So yes, the time is lengthy. The reason is that the last UI is cleaned up only after HttpSession is expired (which depends on application container etc. settings). If you want to do forced clean up quicker, you need to use https://vaadin.com/directory/component/cleanupservlet-add-on

@Push
@SuppressWarnings("serial")
public class DemoUI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = DemoUI.class, heartbeatInterval=5, closeIdleSessions=true)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    public void detach() {
        System.out.println("Detach called");                            
    }

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        ...
    }

Upvotes: 3

Related Questions