Question

I'm developping an audit service for a GWT-based application with the RequestFactory framework. I have some trouble to audit the user logout using a ClosingHandler. Here's my code:

A sum up of my audit service:

private static final int MAX_CACHE_SIZE = 15;
private int cacheSize = 0;
private AuditServiceRequestContext context;

@Override
public void audit(String event, String details) {
    if (context == null)
        context = createContext();
    AuditServiceRequestContext cxt = createContext();
    context.append(cxt);

    AuditProxy proxy = cxt.create(AuditProxy.class);
    /* intialize the proxy with event and details */
    cxt.persist(proxy);

    if (++cacheSize >= MAX_CACHE_SIZE)
        flush();
}

public void flush() {
    context.fire();
    cacheSize = 0;
    context = null;
}

How I currently handle the log out event:

Window.addWindowClosingHandler(new ClosingHandler() {
                        @Override
                        public void onWindowClosing(ClosingEvent event) {
                            audit.audit("logout", "the user has closed the app");
                            audit.flush();
                        }
                        });

The data are persisted but the request fails because of the HTTP request on /gwtRequest doesn't return any response (status canceled on the chrome's developer tools).
Any idea to solve this issue ?

EDIT:

Strangely, there is no error using a CloseHandler with Window#addCloseHandler(CloseHandler). Don't understand why, but it works (and if someone can explain it to me, I really enjoy) :D

Was it helpful?

Solution

When you're navigating away from the page, the browser cancels ongoing requests. Because you make yours at window closing, you cannot even be sure the request was sent over the wire and reached your server. There's no workaround.

One possibility, but which is likely to fail too, is to open a new window so you can safely make requests there, and then close that window when you're done. It's likely to fail however as such windows are likely to be blocked by browsers' popup blockers (built-in or addons).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top