-
1. Re: Fundamental Question
jfuerth Jul 3, 2013 10:28 AM (in response to thewebman2002)Hi Mike,
It's a good question, and one that I've tripped over in the past myself. The answer is no, ErraiBus.get() is only applicable in client code.
Normally (as you've said already) you obtain an instance of the server MessageBus by means of @Inject on a field or constructor of your RPC or bus service class. Since both are created by the embedded Guice context in server-side Errai, the correct ServerMessageBusImpl instance is readily injectable from the context.
In a case like yours, where you want to initiate a broadcast from a worker thread (outside the Guice injection context) you have little recourse but to fall back on a singleton reference. [Un]fortunately, there is a way:
ErraiService service = ErraiServiceSingleton.getService();
MessageBus bus = service.getBus();
I'm not at all a fan of singletons like this; I can imagine a time when we may want several message buses operating independently (or perhaps in federation) within the same webapp, and the existence of an API like ErraiServiceSingleton.getService().getBus() is a big impediment to making that a reality. But for now, I think it's your best bet.
-Jonathan