16.2. Publishing an OSGi Service


Overview

This section explains how to generate, build, and deploy a simple OSGi service in the OSGi container. The service is a simple Hello World Java class and the OSGi configuration is defined using a blueprint configuration file.

Prerequisites

In order to generate a project using the Maven Quickstart archetype, you must have the following prerequisites:
  • Maven installation—Maven is a free, open source build tool from Apache. You can download the latest version from http://maven.apache.org/download.html (minimum is 2.0.9).
  • Internet connection—whilst performing a build, Maven dynamically searches external repositories and downloads the required artifacts on the fly. In order for this to work, your build machine must be connected to the Internet.

Generating a Maven project

The maven-archetype-quickstart archetype creates a generic Maven project, which you can then customize for whatever purpose you like. To generate a Maven project with the coordinates, org.fusesource.example:osgi-service, enter the following command:
mvn archetype:create
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=org.fusesource.example
-DartifactId=osgi-service
The result of this command is a directory, ProjectDir/osgi-service, containing the files for the generated project.
Note
Be careful not to choose a group ID for your artifact that clashes with the group ID of an existing product! This could lead to clashes between your project's packages and the packages from the existing product (because the group ID is typically used as the root of a project's Java package names).

Customizing the POM file

You must customize the POM file in order to generate an OSGi bundle, as follows:
  1. Follow the POM customization steps described in Section 6.1, “Generating a Bundle Project”.
  2. In the configuration of the Maven bundle plug-in, modify the bundle instructions to export the org.fusesource.example.service package, as follows:
    <project ... >
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
              <instructions>
                <Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
     <Export-Package>org.fusesource.example.service</Export-Package>
              </instructions>
            </configuration>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

Writing the service interface

Create the ProjectDir/osgi-service/src/main/java/org/fusesource/example/service sub-directory. In this directory, use your favorite text editor to create the file, HelloWorldSvc.java, and add the code from Example 16.3, “The HelloWorldSvc Interface” to it.

Example 16.3. The HelloWorldSvc Interface

package org.fusesource.example.service;

public interface HelloWorldSvc 
{
    public void sayHello();
}

Writing the service class

Create the ProjectDir/osgi-service/src/main/java/org/fusesource/example/service/impl sub-directory. In this directory, use your favorite text editor to create the file, HelloWorldSvcImpl.java, and add the code from Example 16.4, “The HelloWorldSvcImpl Class” to it.

Example 16.4. The HelloWorldSvcImpl Class

package org.fusesource.example.service.impl;

import org.fusesource.example.service.HelloWorldSvc;


public class HelloWorldSvcImpl implements HelloWorldSvc {
    
    public void sayHello()
    {
        System.out.println( "Hello World!" );
    }

}

Writing the blueprint file

The blueprint configuration file is an XML file stored under the OSGI-INF/blueprint directory on the class path. To add a blueprint file to your project, first create the following sub-directories:
ProjectDir/osgi-service/src/main/resources
ProjectDir/osgi-service/src/main/resources/OSGI-INF
ProjectDir/osgi-service/src/main/resources/OSGI-INF/blueprint
Where the src/main/resources is the standard Maven location for all JAR resources. Resource files under this directory will automatically be packaged in the root scope of the generated bundle JAR.
Example 16.5, “Blueprint File for Exporting a Service” shows a sample blueprint file that creates a HelloWorldSvc bean, using the bean element, and then exports the bean as an OSGi service, using the service element.
Under the ProjectDir/osgi-service/src/main/resources/OSGI-INF/blueprint directory, use your favorite text editor to create the file, config.xml, and add the XML code from Example 16.5, “Blueprint File for Exporting a Service”.

Example 16.5. Blueprint File for Exporting a Service

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

  <bean id="hello" class="org.fusesource.example.service.impl.HelloWorldSvcImpl"/>
  
  <service ref="hello" interface="org.fusesource.example.service.HelloWorldSvc"/>

</blueprint>

Running the service bundle

To install and run the osgi-service project, perform the following steps:
  1. Build the project—open a command prompt and change directory to ProjectDir/osgi-service. Use Maven to build the demonstration by entering the following command:
    mvn install
    If this command runs successfully, the ProjectDir/osgi-service/target directory should contain the bundle file, osgi-service-1.0-SNAPSHOT.jar.
  2. Install and start the osgi-service bundle—at the Red Hat JBoss Fuse console, enter the following command:
    JBossFuse:karaf@root> osgi:install -s file:ProjectDir/osgi-service/target/osgi-service-1.0-SNAPSHOT.jar
    Where ProjectDir is the directory containing your Maven projects and the -s flag directs the container to start the bundle right away. For example, if your project directory is C:\Projects on a Windows machine, you would enter the following command:
    JBossFuse:karaf@root> osgi:install -s file:C:/Projects/osgi-service/target/osgi-service-1.0-SNAPSHOT.jar
    Note
    On Windows machines, be careful how you format the file URL—for details of the syntax understood by the file URL handler, see Section A.1, “File URL Handler”.
  3. Check that the service has been created—to check that the bundle has started successfully, enter the following Red Hat JBoss Fuse console command:
    JBossFuse:karaf@root> osgi:list
    Somewhere in this listing, you should see a line for the osgi-service bundle, for example:
    [ 236] [Active     ] [Created     ] [       ] [   60] osgi-service (1.0.0.SNAPSHOT)
    To check that the service is registered in the OSGi service registry, enter a console command like the following:
    JBossFuse:karaf@root> osgi:ls 236
    Where the argument to the preceding command is the osgi-service bundle ID. You should see some output like the following at the console:
    osgi-service (236) provides:
    ----------------------------
    osgi.service.blueprint.compname = hello
    objectClass = org.fusesource.example.service.HelloWorldSvc
    service.id = 272
    ----
    osgi.blueprint.container.version = 1.0.0.SNAPSHOT
    osgi.blueprint.container.symbolicname = org.fusesource.example.osgi-service
    objectClass = org.osgi.service.blueprint.container.BlueprintContainer
    service.id = 273
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.