Since I am a big proponent of modularity, low coupling, high cohesion, etc …
I believe that this technology is a breakthrough in how we create applications using the Java platform. With OSGi, it is very simple to create highly extensible applications, see for example the Eclipse IDE.
My goal here is not to show in depth how the technology works, but to demonstrate a small example of some of its advantages. The sample consists of a system for sending messages. The user types a message into a TextField and this message can be sent in several ways, such as Email or SMS. However, in this example, we have four modules. The graphical user interface, domain, sender of email messages and the sender via SMS.
Following the nomenclature of OSGi, each module is a Bundle. A Bundle is nothing more than a “jar” with some additional information from MANIFEST.MF. This information is used by the OSGi framework. Like almost everything in Java, OSGi technology is a specification and therefore have different implementations to choose from. Among them are the most famous Equinox (Eclipse Project), Felix (Apache) and Knopflerfish. In this article we will use the Equinox.

Download the Equinox. For this article we only need the jar. Run the jar to access the console of the Equinox.

C:\osgi>java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar –console

To view Bundles installed, simply type the command ss.

C:\osgi>java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar – console

osgi> ss

Framework is launched.

id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827

osgi> _

As we can see at this point we only have one bundle installed. The bundle of Equinox.
Now we’ll create our bundle and add it to Equinox. Create a bundle is very simple.
Create a simple project with the following class:

This class is the Activator of our bundle. The Activator is used by the OSGi framework to start or stop a bundle. In this first example, the Activator will only print messages when started and stopped. Now we need to modify the MANIFEST of the jar to make it an OSGi bundle.

See the MANIFEST passed to the OSGi bundle some of our information. Among them the name of the bundle (SymbolicName) and the Activator class. Now let’s install this bundle in Equinox. Generate a jar of the project and to install it in Equinox is simple:

install file:.jar

osgi> install file:bundle.jar

Bundle id is 1

osgi>

To verify that the bundle was properly installed, simply run the command ss:

Now that we know how to create a bundle, let’s start our example. In the example, we have four bundles.

* Domain: As the name says, it stores the domain classes in our example. We will have two classes: Message and IMessageSender.
* SenderSMS: implementation of IMessageSender that sends messages via SMS.
* SenderEmail: implementation of IMessageSender that sends messages by email.
* UI: GUI example

Bundle UI

We’ll start with the UI bundle. The activator will just build the frame for the user to enter the message.

Note that the bundle depends on a class called Message. This class is our domain, so it is not part of this bundle. Here comes another detail of OSGi. The communication is done through bundles of services. We can consider this model as an SOA within the VM. The services bundle UI will use the bundle core. Let’s look at the MANIFEST bundle UI.

See the Message class is comprised of a list of services. These services are the senders of messages to be used. Note that the send method only interact on the mailing list message. So far everything is very simple. Now we need to export the Message class as a core service bundle. The UI module will interact directly with this service to send messages.

First we need to tell it to export OSGi bundle to other bundles. See the MANIFEST:

See information Export-Package. For a class to be visible for another bundle, it must be exported within a package. In our case, the UI needs bundle of the Message class, so we need to export the package where the class is. Remember that the UI imported the bundle package.

Message To register the component as a service, we need to directly interact with the OSGi API. When the core bundle is started, we will register the service in the context of OSGi. The code is simple:

If we add the two bundles in the Equinox, we see that the two bundles are communicating. Now we need to create bundles that actually send the messages.

Bundle Sender Email and SMS

Shipping services via email and SMS will be new services from our system. Therefore, we create a bundle for each. This way we can control them separately. For example, we can stop the service by sending SMS and leave only the Email without affecting system operation. The two bundles have virtually the same structure, so I’ll save some lines here.

The sender will have only one bundle class that implements the interface and class IMessageSender Activator. This interface is at the core bundle, so we need to import the package in the same way we did in the bundle UI.

The bundle of mail is virtually the same code. The only difference is the message on System.out.

Note that registered the service with the name of the interface. So now we have two services with the same name. Whenever we ask for the service context with the interface name, he will perform a logical priority to return only one implementation.

Now we have two services for sending messages, we need to change our bundle core to use them. To achieve this objective, a ServiceTrackerCustomizer.

ServiceTrackerCustomizer e ServiceTracker

As we saw, we used to do Servicetrack lookup service. However, in the case of senders, we need to know when a new sender service is available or when a sender is removed. This information is important to feed the list of services within the Message object.

To access this information, we use a ServiceTrackerCustomizer. The code is simple:

See which messages are sent by email and SMS. In console Equinox, pause the service email:

stop

Try again send a message. Because the service is no longer available, the message was sent only by SMS.

Stopping power application modules without suffering side effects is sensational. Imagine that you discover a critical error in the SMS module. You need not take all the air is applied to correct this problem. Just pause the SMS module. The rest of the system will continue normal operation. Take the test with this small example. Pause and Start services. This will not affect the core, much less the UI.

I managed to explain a little of what is OSGi. It is noteworthy that has much more detail on control and classpath configuration bundles that do not pay attention here. It is the task for those interested take a look at other features.

It’s worth looking at the Spring-DM project. The spring makes it very easy to set up services in addition to providing an excellent IoC container.