我在这个 GWT SimpleEventbus 的简单实现上被困了好几个小时。
这个设置有什么问题吗?这可能是什么原因?
我正在使用 web.binderyEventbus
import com.google.web.bindery.event.shared.SimpleEventBus;
Eventbus 在我的入口点类中初始化。
public void onModuleLoad() {
ApplicationEvents = new SimpleEventBus();
然后将其注入到 Singleton LoadData 和 MainView 中(UIBinder 类没有 MVP)。
LoadData.initLoadData(ApplicationEvents);
view = new MainView(ApplicationEvents);
initLoadData 基本上是构造函数的别名,只是它不返回任何内容。
public class LoadData {
private final SimpleEventBus eventBus;
private LoadData(final SimpleEventBus bus) {
eventBus = bus;
...
}
public static void initLoadData(final SimpleEventBus bus){
if (instance == null){
instance = new LoadData(bus);
}
}
像这样将 Eventbus 注入 MainView。
public class MainView extends Composite {
private final SimpleEventBus eventbus;
public MainView(final SimpleEventBus bus) {
eventbus = bus;
...
然后注册处理程序。
eventbus.addHandler(EntriesReceiveEvent.TYPE, new EntriesReceiveEventHandler() {
@Override
public void onEntriesReceive(EntriesReceiveEvent event) {
...
}
});
在方法结束时,我让它调用 LoadData 中的一个方法。
LoadData.getLoadData().fetchYears(); //alias for loadIndex
LoadData.java中的 loadIndex 方法
private void loadIndex() {
System.out.println(eventBus.toString());
if(indexEntries == null){
AsyncCallback..
public void onSuccess(Map<Short, IndexEntry> result) {
eventBus.fireEvent(new EntriesReceiveEvent());
//^^ LoadData.java:190
EntriesReceiveEvent.java
import com.google.gwt.event.shared.GwtEvent;
public class EntriesReceiveEvent extends GwtEvent<EntriesReceiveEventHandler> {
public static Type<EntriesReceiveEventHandler> TYPE =
new Type<EntriesReceiveEventHandler>();
@Override
public Type<EntriesReceiveEventHandler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(EntriesReceiveEventHandler handler) {
handler.onEntriesReceive(this);
}
}
它是处理程序 EntriesReceiveEventHandler.java
import com.google.gwt.event.shared.EventHandler;
public interface EntriesReceiveEventHandler extends EventHandler {
void onEntriesReceive(EntriesReceiveEvent event);
}
最后是例外。
11:36:57.417 [ERROR] [] Uncaught exception escaped
com.google.web.bindery.event.shared.UmbrellaException: Exception caught: null
at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:203)
at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
at com.SoftwareEngineering.client.LoadData$2.onSuccess(LoadData.java:190)
at com.SoftwareEngineering.client.LoadData$2.onSuccess(LoadData.java:1)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
我认为 Eventbus 可能不一样,因为我不知道他们是否都在与同一个 Eventbus 交谈。然而,这个网站证明我的担心是错误的。
附带问题:在这种情况下,您认为 Singleton不好吗?这是一个相当小的项目,处理所有 RPC 请求的 LoadData 需要全局可用。
感谢您的任何帮助!