Search code examples
ajaxgwtgwt-rpcevent-bus

Google Web Toolkit (GWT) EventBus event firing/handling


Background Story:
I am developing a GWT application, using the standard MVP design pattern, and also using RPC to get data from my custom data handling servlet (does a lot behind the scenes). Anyway, my goal is to create a very simple custom caching mechanism, that stores the data returned from the RPC callback in a static cache POJO. (The callback also sends a custom event using the SimpleEventBus to all registered handlers.) Then when I request the data again, I'll check the cache before doing the RPC server call again. (And also send a custom event using the EventBus).

The Problem:
When I send the event from the RPC callback, everything works fine. The problem is when I send the event outside the RPC callback when I just send the cached object. For some reason this event doesn't make it to my registered handler. Here is some code:

public void callServer(final Object source)
{
    if(cachedResponse != null)
    {
        System.err.println("Getting Response from Cache for: "+ source.getClass().getName());

        //Does this actually fire the event?
        eventBus.fireEventFromSource(new ResponseEvent(cachedResponse),source);

    }
    else
    {
        System.err.println("Getting Response from Server for: "+ source.getClass().getName());
        service.callServer(new AsyncCallback<String>(){

            @Override
            public void onFailure(Throwable caught) {
                System.err.println("RPC Call Failed.");
            }

            @Override
            public void onSuccess(String result) {
                cachedResponse = result;
                eventBus.fireEventFromSource(new ResponseEvent(cachedResponse),source);
            }
        });
    }
}


Now I have two Activities, HelloActivity and GoodbyeActivity (taken from: GWT MVP code)
They also print out messages when the handler is called. Anyway, this is the output I get from the logs: (Not correct)

Getting Response from Cache for: com.hellomvp.client.activity.HelloActivity Response in GoodbyeActivity from: com.hellomvp.client.activity.HelloActivity Getting Response from Cache for: com.hellomvp.client.activity.GoodbyeActivity Response in HelloActivity from: com.hellomvp.client.activity.GoodbyeActivity


What I expect to get is this:

Getting Response from Cache for: com.hellomvp.client.activity.HelloActivity
Response in HelloActivity from: com.hellomvp.client.activity.HelloActivity
Getting Response from Cache for: com.hellomvp.client.activity.GoodbyeActivity
Response in GoodbyeActivity from: com.hellomvp.client.activity.GoodbyeActivity


And I will get this expected output if I change the above code to the following: (This is the entire file this time...)

package com.hellomvp.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellomvp.events.ResponseEvent;

public class RequestManager {

private EventBus eventBus;
private String cachedResponse;
private HelloServiceAsync service = GWT.create(HelloService.class);

public RequestManager(EventBus eventBus)
{
    this.eventBus = eventBus;
}

public void callServer(final Object source)
{
    if(cachedResponse != null)
    {
        System.err.println("Getting Response from Cache for: "+ source.getClass().getName());

        service.doNothing(new AsyncCallback<Void>(){

            @Override
            public void onFailure(Throwable caught) {
                System.err.println("RPC Call Failed.");
            }

            @Override
            public void onSuccess(Void result) {
                eventBus.fireEventFromSource(new ResponseEvent(cachedResponse),source);
            }
        });
    }
    else
    {
        System.err.println("Getting Response from Server for: "+ source.getClass().getName());
        service.callServer(new AsyncCallback<String>(){

            @Override
            public void onFailure(Throwable caught) {
                System.err.println("RPC Call Failed.");
            }

            @Override
            public void onSuccess(String result) {
                cachedResponse = result;
                eventBus.fireEventFromSource(new ResponseEvent(cachedResponse),source);
            }
        });
    }
}
}


So the point it out, the only change is that I created a new RPC call that does nothing, and send the event in its callback, with the cached data instead, and it causes the application to work as expected.

So the Question:
What am I doing wrong? I don't understand why 'eventBus.fireEvent(...)' Needs to be in an RPC Callback to work properly. I'm thinking this is a threading issue, but I have searched Google in vain for anything that would help.

I have an entire Eclipse project that showcases this issue that I'm having, it can be found at: Eclipse Problem Project Example



Edit: Please note that using eventBus.fireEventFromSource(...) is only being used for debugging purposes, since in my actual GWT Application I have more than one registered Handler for the events. So how do you use EventBus properly?


Solution

  • If I understand your problem correctly you are expecting calls to SimpleEventBus#fireEventFromSource to be routed only to the source object. This is not the case - the event bus will always fire events to all registered handlers. In general the goal of using an EventBus is to decouple the sources of events from their handlers - basing functionality on the source of an event runs counter to this goal.

    To get the behavior you want pass an AsyncCallback to your caching RPC client instead of trying to use the EventBus concept in a way other than intended. This has the added benefit of alerting the Activity in question when the RPC call fails:

    public class RequestManager {
      private String cachedResponse = null;
      private HelloServiceAsync service = GWT.create(HelloService.class);
    
      public void callServer(final AsyncCallback<String> callback) {
        if (cachedResponse != null) {
          callback.onSuccess(cachedResponse);
        } else {
          service.callServer(new AsyncCallback<String>(){
            @Override
            public void onFailure(Throwable caught) {
              callback.onFailure(caught);
            }
    
            @Override
            public void onSuccess(String result) {
              cachedResponse = result;
              callback.onSuccess(cachedResponse);
            }
          });
        }
      }
    }
    

    And in the Activity:

    clientFactory.getRequestManager().callServer(new AsyncCallback<String>() {
      @Override
      public void onFailure(Throwable caught) {
        // Handle failure.
      }
    
      @Override
      public void onSuccess(String result) {
        helloView.showResponse(result);
      }
    });