此内容没有您所选择的语言版本。
16.3. Accessing an OSGi Service
Overview 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
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.
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-client, enter the following command:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=osgi-client
mvn archetype:create
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=org.fusesource.example
-DartifactId=osgi-client
The result of this command is a directory,
ProjectDir/osgi-client, 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:
- Follow the POM customization steps described in Section 11.1, “Generating a Bundle Project”.
- Because the client uses the
HelloWorldSvcJava interface, which is defined in theosgi-servicebundle, it is necessary to add a Maven dependency on theosgi-servicebundle. Assuming that the Maven coordinates of theosgi-servicebundle areorg.fusesource.example:osgi-service:1.0-SNAPSHOT, you should add the following dependency to the client's POM file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Writing the Blueprint file 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To add a blueprint file to your client project, first create the following sub-directories:
ProjectDir/osgi-client/src/main/resources ProjectDir/osgi-client/src/main/resources/OSGI-INF ProjectDir/osgi-client/src/main/resources/OSGI-INF/blueprint
ProjectDir/osgi-client/src/main/resources
ProjectDir/osgi-client/src/main/resources/OSGI-INF
ProjectDir/osgi-client/src/main/resources/OSGI-INF/blueprint
Under the
ProjectDir/osgi-client/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.6, “Blueprint File for Importing a Service”.
Example 16.6. Blueprint File for Importing a Service
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).
Writing the client class 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Under the
ProjectDir/osgi-client/src/main/java/org/fusesource/example/client directory, use your favorite text editor to create the file, Client.java, and add the Java code from Example 16.7, “The Client Class”.
Example 16.7. The Client Class
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.
Running the client bundle 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To install and run the
osgi-client project, perform the following steps:
- Build the project—open a command prompt and change directory to
ProjectDir/osgi-client. Use Maven to build the demonstration by entering the following command:mvn install
mvn installCopy to Clipboard Copied! Toggle word wrap Toggle overflow If this command runs successfully, theProjectDir/osgi-client/targetdirectory should contain the bundle file,osgi-client-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-client/target/osgi-client-1.0-SNAPSHOT.jar
JBossFuse:karaf@root> osgi:install -s file:ProjectDir/osgi-client/target/osgi-client-1.0-SNAPSHOT.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow Where ProjectDir is the directory containing your Maven projects and the-sflag directs the container to start the bundle right away. For example, if your project directory isC:\Projectson a Windows machine, you would enter the following command:JBossFuse:karaf@root> osgi:install -s file:C:/Projects/osgi-client/target/osgi-client-1.0-SNAPSHOT.jar
JBossFuse:karaf@root> osgi:install -s file:C:/Projects/osgi-client/target/osgi-client-1.0-SNAPSHOT.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteOn Windows machines, be careful how you format thefileURL—for details of the syntax understood by thefileURL handler, see Section A.1, “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!
Bundle ID: 239 OSGi client started. Calling sayHello() Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow