Over a million developers have joined DZone.

From OSGi to GlassFish in 5 Steps

DZone's Guide to

From OSGi to GlassFish in 5 Steps

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Sometime ago the new connection between GlassFish and OSGi was announced: you will be able to extend the upcoming GlassFish V3 via OSGi bundles. Though V3 isn't final yet, one can already get acquainted with it and, already, with its OSGi extensibility. The information on the practical steps of extending GlassFish via OSGi is currently not centralized, so here is a quick start, describing the absolute basics, from which one can extrapolate more meaningful scenarios.

At the end of this intro, you will (unlike everyone else in the world who does not have the OSGi bundle that you will create here) see your user name, user directory, and user home, whenever GlassFish starts up:

We will basically follow the instructions provided by Arun Gupta, which mostly worked for me, using the Maven Bundle Plugin.

  1. Create the POM.xml and other artifacts from the command line:
    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes 
    -DgroupId=org.glassfish.samples.osgi.helloworld -DartifactId=helloworld

  2. Open the pom.xml in the IDE of your choice, tweak it according to step 3 of Arun's instructions, and code the basic OSGi bundle's content:

    In the end, this was my class:

    package org.glassfish.samples.osgi.helloworld;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class App implements BundleActivator {

    public void start(BundleContext context) throws Exception {
    String userName = context.getProperty("user.name");
    String userDir = context.getProperty("user.dir");
    String userHome = context.getProperty("user.home");
    System.out.println("User Name: " + userName);
    System.out.println("User Dir: " + userDir);
    System.out.println("User Home: " + userHome);

    public void stop(BundleContext context) throws Exception {


  4. Run the "Install" goal (in NetBeans IDE, that means right-clicking the project and then choosing Custom | Goals and then running the Install goal) and put the JAR created by this process into this folder:

    So, here you see my JAR, which is called "Greeter-0.1-SNAPSHOT.jar":


  6. Open GLASSFISH_HOME/glassfish/felix/conf/config.properties and register the JAR there under "felix.auto.start", i.e., look at the final line below:
    felix.auto.start.1= \
    ${com.sun.aas.installRootURI}/modules/tiger-types-osgi-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/auto-depends-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/config-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/hk2-core-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/osgi-adapter-0.2.1.jar \

  8. Soon (very soon!) GlassFish will start in Felix mode by default, as shown in Arun's blog entry. However, for now, run the following from the GLASSFISH_HOME/glassfish folder, i.e., for now, be aware of the "DGlassFish_Platform" switch:
    java -DGlassFish_Platform=Felix -jar modules/glassfish-10.0-tp-2-SNAPSHOT.jar


Then GlassFish starts up in Felix mode and the OSGi bundle that you registered earlier will simply be picked up as part of the start up process. And then you will see the messages you sent to yourself in the bundle.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}