Sending events to e4 applications from outside of e4

e4 uses an event bus to let different pieces (e.g. parts) of an e4 application communicate. Architecturally, this leads to low coupling (doubleplusgood) due to the fact that the various pieces only need to know the event topics (String constants), which are ideally defined centrally in a common bundle. If you happen to never leave the e4 application realm you are lucky and can stop reading here.
For the rest of us though – who face the requirement to send e4 application events outside of e4 – the problem is that e4 does not use the org.osgi.service.event.EventAdmin directly. It comes with its own This alone would not pose an unsolvable problem (one could simply get the IEventBroker instead of EventAdmin from OSGi), but IEventBroker does not get registered with the OSGi service registry (see bug #391097). Luckily for us, the e4 team – even though they decided against reusing the OSGi org.osgi.service.event.EventAdmin directly – implemented IEventBroker as a wrapper around EventAdmin. So in order to send an event to e4, one simply has to get/lookup the OSGi EventAdmin from the OSGi service register (either programmatically or with DS).

If one’s use case also requires sending a payload along with the event, the Dictionary/Map of org.osgi.service.event.Event has to contain the element under the special key

Map<String, Object> map = new HashMap<String, Object>();
map.put(IEventBroker.DATA, payload); 				

Event event = new Event(MyE4EventConstants.TOPIC_SOME_TOPIC, map);

ServiceReference<EventAdmin> serviceReference = 
EventAdmin ea = context.getService(serviceReference);
This entry was posted in Other and tagged . Bookmark the permalink.