Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.16.2. Publishing an OSGi Service
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You must customize the POM file in order to generate an OSGi bundle, as follows:
- Follow the POM customization steps described in Section 6.1, “Generating a Bundle Project”.
- In the configuration of the Maven bundle plug-in, modify the bundle instructions to export the
org.fusesource.example.service
package, as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Writing the service interface Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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
Writing the service class Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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
Writing the blueprint file Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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
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
Running the service bundle Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To install and run the
osgi-service
project, perform the following steps:
- 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
mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If this command runs successfully, theProjectDir/osgi-service/target
directory should contain the bundle file,osgi-service-1.0-SNAPSHOT.jar
. - 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
JBossFuse:karaf@root> osgi:install -s file:ProjectDir/osgi-service/target/osgi-service-1.0-SNAPSHOT.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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 isC:\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
JBossFuse:karaf@root> osgi:install -s file:C:/Projects/osgi-service/target/osgi-service-1.0-SNAPSHOT.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteOn Windows machines, be careful how you format thefile
URL—for details of the syntax understood by thefile
URL handler, see Section A.1, “File URL Handler”. - 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
JBossFuse:karaf@root> osgi:list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Somewhere in this listing, you should see a line for theosgi-service
bundle, for example:[ 236] [Active ] [Created ] [ ] [ 60] osgi-service (1.0.0.SNAPSHOT)
[ 236] [Active ] [Created ] [ ] [ 60] osgi-service (1.0.0.SNAPSHOT)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
JBossFuse:karaf@root> osgi:ls 236
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where the argument to the preceding command is theosgi-service
bundle ID. You should see some output like the following at the console:Copy to Clipboard Copied! Toggle word wrap Toggle overflow