JAVA两个模块间如何请求,GWT:如何在两个模块之间共享一个java对象(即:EventBus)...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值