2013/09/04

OSGi example in Eclipse 4.3 for JBoss 7

Do you have any experiences with OSGi? Do you know how to create OSGi bundle in Eclipse? Do you know how to start JBoss AS with OSGi support? You will know now ;)

Eclipse configuration

We need to configure Eclipse first. In my case it is Eclipse 4.3. You need to configure OSGi Framework under Preferences > Plug-in Development > Target Platform. Currently all plug-ins are included and it can cause many exceptions. So select (Active) platform, click Edit.


Click Content tab and select only these plug-ins:
  • org.eclipse.osgi
  • org.eclipse.equinox.console
  • org.apache.felix.gogo.runtime
  • org.apache.felix.gogo.shell

Creating bundle

Now we need to create our bundle (plug-in) under New > Plug-in Project like this:


Choose "Hello OSGi Bundle" as template. Your project will contain one simple activator which writes some message to System.out in case of start and stop of bundle.

Start OSGi framework in Eclipse

It is time to test our bundle. Right click on MANIFEST.MF file and choose Run As > OSGi Framework. If everything is OK, you should see start message from activator in console.

Try ss command to list all bundles:

We can try to export your new bundle to jar file. Righ click on bundle-test project, Export > Plug-in Development > Deployable plug-ins and fragments, choose your directory where jar will be created. Done.

Deploy bundle to JBoss AS

Start your JBoss 7 like: standalone.bat -c standalone-osgi.xml and copy your bundle jar into deployment directory. Quite simple, isn't it.

If you need Maven in your project, you can do it. Here is example of pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.test</groupId>
 <artifactId>osgi-test</artifactId>
 <version>0.0.1</version>
 <packaging>bundle</packaging>

 <dependencies>
  <dependency>
   <groupId>org.osgi</groupId>
   <artifactId>org.osgi.core</artifactId>
   <version>4.1.0</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.0.1</version>
    <extensions>true</extensions>
    <configuration>
     <instructions>
      <Import-Package>
       org.osgi*
      </Import-Package>
      <Bundle-Activator>com.bundle.Activator</Bundle-Activator>
      <Bundle-SymbolicName>${project.groupId}.${project.artifactId};singleton:=true</Bundle-SymbolicName>
      <Bundle-RequiredExecutionEnvironment>JavaSE-1.7</Bundle-RequiredExecutionEnvironment>
     </instructions>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>
One note to Maven, you need to remove MANIFEST.MF file and change directory structure ;).

Hopefully it will help you to begin with OSGi in Eclipse. In case of any question ask please.
▼ Click here to say thanks ▼

1 comment :

  1. Thank you for the explanation! I looked for this information hours and I tried to do a lot of things but nothing worked. On your post I can always find what I need. Thank you so much !
    digital signature

    ReplyDelete