GWT: How to share a java Object (ie: EventBus) between two modules

喜夏-厌秋 提交于 2019-12-23 05:27:34

问题


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.


回答1:


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:

  • create an "Application.gwt.xml" module with an entry point (equivalent to your initialization module, if I understand correctly)
  • create "UserManagement.gwt.xml" module without an entry point class
  • create other XXX modules without entry points

To create a module without entry point just remove the

<entry-point class='xxx'/>

from your gwt.xml files except for the "Application" one

You then need to include these modules into the "Application" module using

<inherits name="com.yourpackage.Module1Name" />
<inherits name="com.yourpackage.Module2Name" />

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




回答2:


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 your window.getEventBus exported method is not known in other modules, although the other modules are using SimpleEventBus as well

The 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-exporter

I 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.




回答3:


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.



来源:https://stackoverflow.com/questions/15604082/gwt-how-to-share-a-java-object-ie-eventbus-between-two-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!