Compass
Compass

Reputation: 5937

How to Call a GWT method whenever an Async RPC is Made

I have a website that utilizes GWT that is secured using Spring with a timeout of... let's say an hour. As part of the process, I am trying to implement a client-side session timeout warning 5 minutes before the session expires.

I have the Javascript written out for it, which utilizes cookies. The timer works for initially setting the timer, and has a method for resetting the timer that can be used in GWT through native methods.

function resetSessionTimeout() { //update the cookie for the expected timeout
    var date = new Date();
    var timeoutTime = 55 * 60 * 1000;
    date.setTime(date.getTime() + timeoutTime);
    createCookie('timeout', date.getTime(), 1);
}

Whenever an RPC is made to the server, the server-based session timeout is refreshed, so the initial timer is no longer accurate after using any sort of async call. The function above would update the client side with the correct timeout time.

The option that definitely works would be to call a native void that calls $wnd.resetSessionTimeout() from every single method I have with an Async call. However, this is a lot of methods, and is poor scalability and smells of poor implementation.

Is there a way for me to set up the client portion to observe whenever an RPC has been made and received and then put the native void in there within the MVP level rather than at each method, i.e. are there RPC Observers or Listeners?

Upvotes: 0

Views: 536

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

You can do something like:

public class MyCallback<T> implements AsyncCallback<T> {

    public MyCallback() {
    }

    @Override
    public void onFailure(T result) {
        /*
         * Show standard error message. You can override it
         * with a more specific message where necessary.
         */
    }

    @Override
    public void onSuccess(T result) {
        execute(result);
        // Call your timer, etc.
    }

    protected void execute(T result) {
        // Override this method when making calls.
    }
}

Now you can replace all AsyncCallback objects with MyCallback. As an added bonus you won't have to override onFailure every time unless you want a specific error message.

Upvotes: 1

Related Questions