OSGi Learning Path (3)-Helloworld_osgi of OSGi

Source: Internet
Author: User
Now it's all in the HelloWorld. To cover all three levels of the OSGi framework, this HelloWorld may be a little more complicated than the other OSGi HelloWorld programs you see. If you're unfamiliar with some of the APIs in your code, remember to go back to the previous entry to find the corresponding content, which will help you understand the code. The key code inside has been highlighted in yellow. (For space consideration, import statements in code are omitted) definition and implementation of 3.1 HelloWorld

First we create an engineering org.serc.helloworld, in which we create an interface that contains the SayHello method, ready to serve as a service interface: [Java] View plain copy Package      Org.serc.helloworld;   public interface Hello {void SayHello (); }

This interface is then implemented:

[Java] View plain copy package Org.serc.helloworld.impl;      public class Helloimpl implements hello{final String hellostring;   Public Helloimpl (String hellostring) {this.hellostring = hellostring;   public void SayHello () {System.out.println (this.hellostring); }   }

The work done by the SayHello of this class is to output a hellostring string that is obtained when the object is constructed. To expose this interface, we need to add the following entry in the manifest file: [Java] View plain copy export-package:org.serc.helloworld;version= "1.0"

Next, in order to register this service in the framework, we define a activator: [Java] View plain copy package org.serc.helloworld.activator;      public class activator implements bundleactivator {   Private  list<serviceregistration> registrations = new arraylist<serviceregistration > ();      Public void start (bundlecontext ctx)  {                                 registrations.add (Ctx.registerservice ( Hello.class.getName (), New helloimpl ("Hello, osgi"),  null);  }       Public void stop (BUNDLECONTEXT&NBSP;CTX)  {       for  ( serviceregistration registration : registrations)  {            system. OUT.PRINTLN ("unregistering: " + registration);            registration.unregister ();       }  

We passed the "Hello, OSGi" string for this helloimpl in order for this Activator to work, you need to define the following in the manifest file: [Java] View plain copy bundle-activator: Org.serc.helloworld.activator.Activator

The final manifest content of this Bundle is as follows: [Java] View plain copy bundle-manifestversion:2 Bundle-symbolicname:org.serc.helloworld Bu ndle-version:1.0 Bundle-activator:org.serc.helloworld.activator.activator import-package:org.osgi.framework Expor T-package:org.serc.helloworld;version= "1.0"

Your eclipse project should now be like this: 3.2 Get and perform SayHello services

Create a project org.serc.helloworld.client, create a bundleactivator called Hellouser, where the Start method obtains the service object with the interface Hello and invokes the SayHello method through this object: [Java] View plain copy package org.serc.helloworld.client;      public class  hellouser implements bundleactivator {      Public void start ( BUNDLECONTEXT&NBSP;CTX)  {       ServiceReference ref =  Ctx.getservicereference (Hello.class.getName ());       if  (ref !=  NULL)  {           Hello hello = null;            try {                hello =  (Hello)  ctx.getservice (ref);                if  (hello != null )     &Nbsp;              hello.sayhello ();                else                    system.out.println (" Service:hello---object null ");           } catch   (runtimeexception e)  {                e.printstacktrace ();           } finally  {               ctx.ungetservice ( REF);               hello =  null;           }       }  else {           system.out.println ("Service:hello---not exists");        }  }      public void stop (bundlecontext &NBSP;CTX)  throws Exception {     }  }  

To get the definition of Hello interface, we also need to import the package of hello in the manifest file:

[HTML] View plain copy bundle-manifestversion:2 bundle-symbolicname:org.serc.helloworld.client bundle-version:1.0 Bundle-activator:org.serc.helloworld.client.hellouser import-package:org.serc.helloworld;version= "[1.0,2.0)", Process of Org.osgi.framework 3.3 HelloWorld Program

It may be easier to look at the code than it is to see the execution flow of the program, the following diagram shows the interdependencies of the various functions of these classes, the entire relationship begins with the definition of the Hello interface, and then the Hello interface is implemented to get Helloimpl And then to activator to register Helloimpl as a service in the framework, and then to Hellouser to get the service that was registered by interacting with the framework, and use this service to output the string; The last option is when we stop Org.serc.helloworld this bundle, the program will cancel the previously registered service.

3.4 Implementation of the program

With the above work, we got two bundle:org.serc.helloworldorg.serc.helloworld.client of our own definition now open the Run configurations interface, We'll see the bundles tag. These two bundle:

That is, when the OSGi framework starts, it automatically install and start the 2 bundle, and we click the Run button to see what happens: 3.5 view changes in the frame state during program execution using the command line

3.4 Actually gives only one result, and if you're not quite sure how the results come out, the content of this section should help you better understand the process of outputting the results. Here's a step-by-step installation and execution of bundle through some of the equinox command lines, and look at the state of the frame in the process to let you figure out how the result came about.
First, cancel the automatic start of two helloworldbundle in Run configuration:

Then click Run, which will not immediately output the Hello, OSGi string, and now we'll use the "ss" command to view the status of the bundle:

There are two bundle that are not active and the description does not start, and now we perform "Start 8" to start Org.serc.helloworld this bundle:

In the Services command to view the currently registered service, we will see a lot of system services in a number of more than one of the following services:

It's obvious that we registered after start, but none of the bundle are using the service yet. Next we start number 9th bundle, which is the bundle we use to invoke the service:

The string of "Hello,osgi" is then printed.

So what happens if we start number 9th first and not start number 8th bundle bundle? You can try it, because we've already done something unusual with no service in the code, and there's a corresponding output. Let's stop number 8th bundle (here's a picture of Bundleid added, everyone is the same):

You can see that the service you just registered has been logged off, and now we are going to execute Refresh 11 (that is, number 9th bundle) to rerun the Bundleactivator start method:

It can be seen that the Hello service has ceased to exist. From here we can see that, in fact, the bundle of the boot sequence is also a need to pay attention to the link, sometimes you define the bundle is a sequential sensitivity, you must have some bundle boot after the start of the bundle in the back to start correctly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.