This section explains how to generate, build, and deploy a simple OSGi client in
the OSGi container. The client finds the simple Hello World service in the OSGi
registry and invokes the sayHello()
method on it.
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.
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-client
,
enter the following command:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=osgi-client
The result of this command is a directory,
, containing
the files for the generated project.ProjectDir
/osgi-client
![]() | 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). |
You must customize the POM file in order to generate an OSGi bundle, as follows:
Follow the POM customization steps described in Modifying an Existing Maven Project.
Because the client uses the
HelloWorldSvc
Java interface, which is defined in theosgi-service
bundle, it is necessary to add a Maven dependency on theosgi-service
bundle. Assuming that the Maven coordinates of theosgi-service
bundle areorg.fusesource.example:osgi-service:1.0-SNAPSHOT
, you should add the following dependency to the client's POM file:<project ... > ... <dependencies> ... <dependency> <groupId>org.fusesource.example</groupId> <artifactId>osgi-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> ... </project>
To add a blueprint file to your client project, first create the following sub-directories:
ProjectDir
/osgi-client/src/main/resourcesProjectDir
/osgi-client/src/main/resources/OSGI-INFProjectDir
/osgi-client/src/main/resources/OSGI-INF/blueprint
Under the
directory, use your favorite text editor to create the file,
ProjectDir
/osgi-client/src/main/resources/OSGI-INF/blueprintconfig.xml
, and add the XML code from Example 10.6.
Example 10.6. Blueprint File for Importing a Service
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <reference id="helloWorld" interface="org.fusesource.example.service.HelloWorldSvc"/> <bean id="client" class="org.fusesource.example.client.Client" init-method="init"> <property name="helloWorldSvc" ref="helloWorld"/> </bean> </blueprint>
Where the reference
element creates a reference manager that finds a
service of HelloWorldSvc
type in the OSGi registry. The
bean
element creates an instance of the Client
class
and injects the service reference as the bean property, helloWorldSvc
.
In addition, the init-method
attribute specifies that the
Client.init()
method is called during the bean initialization phase
(that is, after the service reference has been injected into
the client bean).
Under the
directory, use your favorite text editor to create the file,
ProjectDir
/osgi-client/src/main/java/org/fusesource/example/clientClient.java
, and add the Java code from Example 10.7.
Example 10.7. The Client Class
// Java package org.fusesource.example.client; import org.fusesource.example.service.HelloWorldSvc; public class Client { HelloWorldSvc helloWorldSvc; // Bean properties public HelloWorldSvc getHelloWorldSvc() { return helloWorldSvc; } public void setHelloWorldSvc(HelloWorldSvc helloWorldSvc) { this.helloWorldSvc = helloWorldSvc; } public void init() { System.out.println("OSGi client started."); if (helloWorldSvc != null) { System.out.println("Calling sayHello()"); helloWorldSvc.sayHello(); // Invoke the OSGi service! } } }
The Client
class defines a getter and a setter method for the
helloWorldSvc
bean property, which enables it to receive the
reference to the Hello World service by injection. The init()
method is
called during the bean initialization phase, after property injection, which means
that it is normally possible to invoke the Hello World service within the scope of
this method.
To install and run the osgi-client
project, perform the following
steps:
Build the project—open a command prompt and change directory to
. Use Maven to build the demonstration by entering the following command:ProjectDir
/osgi-clientmvn install
If this command runs successfully, the
directory should contain the bundle file,ProjectDir
/osgi-client/targetosgi-client-1.0-SNAPSHOT.jar
.Install and start the osgi-service bundle—at the Fuse ESB console, enter the following command:
karaf@root> osgi:install -s file:
ProjectDir
/osgi-client/target/osgi-client-1.0-SNAPSHOT.jarWhere
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:karaf@root> osgi:install -s file:C:/Projects/osgi-client/target/osgi-client-1.0-SNAPSHOT.jar
Note On Windows machines, be careful how you format the
file
URL—for details of the syntax understood by thefile
URL handler, see File URL Handler.Client output—f the client bundle is started successfully, you should immediately see output like the following in the console:
Bundle ID: 239 OSGi client started. Calling sayHello() Hello World!