OSGi declarative services and the event bus – Eclipse RCP Cookbook


OSGi services are used to provide functionality via separate bundles. They are also used to decouple functionality, so it is possible to exchange the implementation at runtime. With the introduction of OSGi declarative services and Eclipse 4 it became more popular to use OSGi services in Eclipse RCP applications.

The communication system in Eclipse 4 is the global event bus. It supports communication between application components and is also used for decoupling, since bundles only need to know the event bus for communication, not the bundles they need to communicate with.

This recipe shows how to use the event bus in an Eclipse 4 based application, how to create a simple service using OSGi declarative services, and how to communicate via event bus in an OSGi service. For this, a part will be added to the application that shows log messages which are sent via event bus.


This recipe is based on the Eclipse RCP Cookbook – Basic Recipe. To get started fast with this recipe, we have prepared the basic recipe for you on GitHub.

To use the prepared basic recipe, import the project by cloning the Git repository:

  • File → Import → Git → Projects from Git
  • Click Next
  • Select Clone URI
  • Enter URI https://github.com/fipro78/e4-cookbook-basic-recipe.git
  • Click Next
  • Select the master branch
  • Click Next
  • Choose a directory where you want to store the checked out sources
  • Click Next
  • Select Import existing projects
  • Click Next
  • Click Finish

Note: With exception to the part implementation, this recipe does not depend on the used UI toolkit. So you can also use the JavaFX version of the basic recipe. If you want to follow this recipe in the JavaFX version, use the following URI to clone the prepared basic recipe: https://github.com/fipro78/e4-cookbook-basic-recipe-fx.git.


Step 1: Sending events to the event bus

The Eclipse event service can be used to send events to the event bus. It is implemented via the IEventBroker interface and can get injected. In this step the application is modified to send log events on specific actions.

  • Update the bundle dependencies
    • Open the file MANIFEST.MF in the project de.codecentric.eclipse.tutorial.inverter
    • Switch to the Dependencies tab
    • Add the following packages to the Required Plug-ins
      • org.eclipse.e4.core.services
  • Open the InverterPart in the project de.codecentric.eclipse.tutorial.inverter
    • Get the IEventBroker injected
    • Modify the listeners on the button and the input field to post an event that contains a String with the log message for the topic “TOPIC_LOGGING”.
      IEventBroker broker;
      public void postConstruct(Composite parent) {
      	button.addSelectionListener(new SelectionAdapter() {
      		public void widgetSelected(SelectionEvent e) {
      			broker.post("TOPIC_LOGGING", "triggered via button");
    • For the JavaFX version this means to add the posting of the event to the onAction EventHandler.
      IEventBroker broker;
      public void postConstruct(Composite parent) {
      	button.setOnAction(event -> {
      		broker.post("TOPIC_LOGGING", "triggered via button");

Note: via IEventBroker#post() the event is sent asynchronously. If you need to send the event synchronously, use IEventBroker#send().

Step 2: Receiving events from the event bus

The recommended way of receiving events from the event bus is to use dependency injection. Using the annotations @EventTopic and @UIEventTopic for method parameters will cause method execution if an event for the specified topic is fired on the event bus. The difference between the two annotations is that using @UIEventTopic will execute the method in the UI thread.

In this step a log view is added to the application to show the log messages that were sent to the event bus.

  • Create the package de.codecentric.eclipse.tutorial.app.part in the project de.codecentric.eclipse.tutorial.app
  • Create a part that shows the log messages
    • Open the file Application.e4xmi in the project de.codecentric.eclipse.tutorial.app
    • Add a container for the part to the window
      • Application → Windows and Dialogs → Trimmed Window → Controls → Part Sash Container → Add Part Stack
    • Add a part to the container
      • Application → Windows and Dialogs → Trimmed Window → Controls → Part Sash Container → Part Stack → Add Part
      • Set the Label to Log View
    • Create the part implementation
      • Click the Class URI link in the part detail view
      • Set the following values in the opened dialog
        • Package: de.codecentric.eclipse.tutorial.app.part
        • Name: LogViewPart
      • Create a viewer that is used to show the log messages
      • Create a method that is executed/notified when an event for the topic “TOPIC_LOGGING” is send

The following is an example of a part using SWT:

public class LogViewPart {



Your email address will not be published. Required fields are marked *