When you have a asynchronous event bus, and fire events, lets say within the model which get catched in the UI you have probably the following problem:
The registered handler gets executed in a worker thread, but all UI swing changes need to be executed within the AWT event thread.
This means you need to envelope all your handler clode in EventQueue.invokeLater(...)
.
This looks like a lot of boiler plate code. I wonder if there is a smarter solution for that problem.
What about an extension to the guava event bus that marks a handler for execution within a special thread? This could be marked with a annotion e.g. @ExecuteWithinEDT
:
class EventBusChangeRecorder {
@Subscribe @ExecuteWithinEDT void recordCustomerChange(ChangeEvent e) {
recordChange(e.getChange());
}
}
You can create an EventBus that dispatches only on the AWT thread:
The handlers registered with an async event bus are executed on whatever thread the provided
Executor
chooses to run them on, not necessarily a worker thread.What I've done is created an implementation of
Executor
that runs stuff on the event queue thread. It's pretty simple:You can then just create your
EventBus
with that:Then all handlers will be executed on the event queue thread.
Edit:
An example of forwarding events:
Just subscribe that to your main event bus and post all events to the main event bus, but subscribe all UI components to the UI event bus.