I’m building a large application and I would like to split it in several modules like Core Module for initialization, users management, etc…, Customer Module, Production Module, etc…
I want to split it in multiples GWT modules (not using GWT splitting technique) and share an EventBus for broadcast some events like LoginEvent, LogoutEvent. I don’t want uses the code splitting technique because I want reduce the compile time and re-compile only the module that I modified. This allow also to enable or disable a module by commenting the script tag in the HTML host page.
I’ve write the following code with using JSNI:
CoreModule’s EntryPoint:
private static SimpleEventBus eventBus = null;
public void onModuleLoad() {
export();
getEventBus().addHandler(MyEvent.TYPE, new MyEventHandler() {
@Override
public void onEvent(MyEvent myEvent) {
Window.alert(myEvent.getMessage());
}
});
}
public static SimpleEventBus getEventBus() {
if (eventBus == null)
eventBus = new SimpleEventBus();
return eventBus;
}
public static native void export() /*-{
$wnd.getEventBus = $entry(@testExporter.client.TestExporter::getEventBus());
}-*/;
CustomerModule’s EntryPoint:
public void onModuleLoad() {
Button button = new Button("Click me");
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
getEventBus().fireEvent(new MyEvent("Button clicked !"));
}
});
RootPanel.get().add(button);
}
public static native SimpleEventBus getEventBus() /*-{
// Create a useless eventBus because the GWT compiler make a call to a null instance
var eventBus = @com.google.gwt.event.shared.SimpleEventBus::new()();
eventBus = $wnd.getEventBus();
return eventBus;
}-*/;
But I’ve the following exception in Firebug when executing in the browser:
uncaugth exception [object Object]
I copied also the MyEvent and MyEventHandler classes that implements/interfaces a customer event.
P.S.: I know also the technique that consist to comment the other modules references to avoid to compile it.
You cannot share code between different GWT compiled modules, unless you make some parts of your code available via jsni and call these exported methods via jsni, like you are trying in your query.
But be aware that: first, shared classes would be incompatible because each compilation would rename the classes/methods in a different way, and second, each compilation would remove different dead code pieces.
So in your case the
SimpleEventBus
returned in yourwindow.getEventBus
exported method is not known in other modules, although the other modules are usingSimpleEventBus
as wellThe easiest way to do what you want, is to use GWT-exporter. First select correctly the js-api you want to export in each module, how you want to name it, and implement
Exportable
and annotate methods conveniently. Second take in account which objects would you use for the communication, because some of then could be incompatible. I would use primitive types, javascript object, and functions which are supported in GWT-exporterI think that with GWT-exporter, for shared classes, if you annotate them in the same namespace and you export the same methods, hopefully you could use then in all modules but I'm not sure.
So export a js API via jsni or gwt-exporter and transfer pure primitive or js objects between them.
A simpler answer is to not use multiple entry points.
==========================================
If what you are trying to achieve is breaking you code into manageable units but want to use all of them in the same page, you can:
To create a module without entry point just remove the
from your gwt.xml files except for the "Application" one
You then need to include these modules into the "Application" module using
You then need to compile all of them together in one GWT build for module "com.yourpackage.Application".
When you do that make sure that both the compiled *.class and the source .java files for all your modules are available on the classpath.
Your "Application" entry point just needs to initialize and use the objects from the other modules
You can use the Frames and setup communication between the modules via WebMessage protocol. It will help only if the modules in one page and modules in separated war.