Tuesday, January 27, 2015

[OSGi] Hello World from OSGi | Create your first OSGi Bundle

Hi Friends,

This post will guide you to create a very very simple Hello World bundle using OSGi. We will use Eclipse for this purpose.

Few simple steps to follow.

Step 1. Create a new project in Eclipse. Open New > Project




Step 2. Select Plug in project



Step 3. Enter the name of your bundle and OSGi framework as Standard. Leave rest of the things as they are. After that, Press next.


Step 4. You will find details about your project in this screen, you can change bundle name, id, version, vendor. This screen also gives an option to create an Activator class by default which is responsible to run operations on start and stop of your module.


Step 5. From next screen select Hello OSGi Bundle Template and press next.


Step 6. You can configure what message to be printed at start and stop of your bundle. You can change these later as well. Once you customize, press finish.



Step 7. Now your project should appear in the project list. As screen below.


Note 1. Two important files are created by default. MANIFEST.MF and Activator.java. Below is the MANIFEST.MF file
Note 2. And Activator.java looks like this.
Activator.java implements BundleActivator. There are two methods which get BundleContext. These methods are called at start and stop of your bundle. At the start, you can write code for init purpose like database initialization, any object creation etc.And at the stop, you can write code for clean up. Of course, there are other several purpose those can be fulfilled with these methods. Depends on the requirements, isn't it?

Step 8. Now lets run the bundle we just created. Go to Run > Run. You should see the output.



So this is how we create an OSGi bundle with help of eclipse and run it. We will learn more about OSGi in next posts. We will also take a closer look at MANIFEST.MF soon enough. Stay Tuned.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.