Events Only start when a component handler is fired !!

16 views
Skip to first unread message

Ahmad Igbaria

unread,
Nov 28, 2010, 4:34:59 AM11/28/10
to GWTEventService
Hello,

I have been trying to create a demo app using GWTEventService, its
very simple here is the onModuleLoad function:
public void onModuleLoad() {
myEventService = GWT.create(MyEventService.class);
final RemoteEventServiceFactory theRemoteEventHandlerFactory =
RemoteEventServiceFactory.getInstance();
myRemoteEventService =
theRemoteEventHandlerFactory.getRemoteEventService();

myRemoteEventService.addListener(null, new
RemoteEventListener() {
public void apply(Event anEvent) {
Log.info("User-Specific Event is caught");
if (anEvent instanceof MessageEvent) {
Log.info("A Message was received: " +
((MessageEvent) anEvent).toString());
} else {
Log.warn("UnHandled event was caught!");
}
}
});

myEventService.runLocalEvent(new AsyncCallback<Void>() {
public void onFailure(Throwable throwable) {
Log.error("Error: " + throwable.getMessage());
}

public void onSuccess(Void aVoid) {
Log.info("Success");
}
});
}

the above code won't work even if its 100% correct !!

but when i make the "runLocalEvent" start from a click handler it
works:

public void onModuleLoad() {
myEventService = GWT.create(MyEventService.class);
final RemoteEventServiceFactory theRemoteEventHandlerFactory =
RemoteEventServiceFactory.getInstance();
myRemoteEventService =
theRemoteEventHandlerFactory.getRemoteEventService();

myRemoteEventService.addListener(null, new
RemoteEventListener() {
public void apply(Event anEvent) {
Log.info("User-Specific Event is caught");
if (anEvent instanceof MessageEvent) {
Log.info("A Message was received: " +
((MessageEvent) anEvent).toString());
} else {
Log.warn("UnHandled event was caught!");
}
}
});

Button button = new Button("Click Me");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
myEventService.runLocalEvent(new AsyncCallback<Void>()
{
public void onFailure(Throwable throwable) {
Log.error("Error: " + throwable.getMessage());
}

public void onSuccess(Void aVoid) {
Log.info("Success");
}
});
}
});

RootPanel.get().add(button);
}


is this a bug? or I'm doing it wrong?

Thanks
Ahmad Igbaria

sven.s

unread,
Nov 30, 2010, 4:58:17 PM11/30/10
to GWTEventService
Hi,

addListener(...) starts an async request like all other requests /
server calls via GWT. When an event is sent directly after the
addListener method, it could be that the event is added before the
listener is registered (race-condition). To avoid that you should
ensure that the listener is registered before sending events via your
service. That is possible by adding the listener earlier or for
example by using the callback of the addListener method to synchronize
your server calls if it is necessary to send an event directly with
the registration of the listener.

Regards,

Sven S.
Reply all
Reply to author
Forward
0 new messages