OSGi notes (ii) True OSGi application _osgi

Source: Internet
Author: User
Tags object object

Publishing and using Services

Since the OSGi framework can easily hide implementation classes, it is natural to provide interfaces externally, and the OSGi Framework provides registration and query capabilities for services. OK, so let's actually do it on the basis of Hello World project. (If not please refer to previous article: OSGi notes (i) HelloWorld)

We need to perform the following steps: Define a service interface and export it for use by other bundle, define a default service implementation and hide its implementation, bundle the service to the Equinox framework when it is started, query the service from the framework, and test availability.

OK, in order to achieve the above requirements, we actually operate as follows: Define a new package Osgi.test.helloworld.service to store the interface. The advantage of a single package is that you can just export this package to other bundle and hide all of the implementation classes in the above package to create a new interface Ihello, provide a simple string service, the code is as follows:

Listing 2. Ihello

							
Package osgi.test.helloworld.service; 

The public interface Ihello { 
    /** 
     * Gets the interface to the Hello information. 
     * @return The Hello string. 
     * 
    /String Gethello (); 
}

Then create a new package Osgi.test.helloworld.impl to store the implementation class. Create a new Defaulthelloserviceimpl class in the package above to implement the above interface:

Listing 3. Ihello Interface Implementation
							
public class Defaulthelloserviceimpl implements Ihello { 

    @Override public 
    String Gethello () {return 
        "Hello Osgi,service "; 
    } 

 }

Registration service, the OSGI framework provides two forms of registration, all implemented through the Bundlecontext class: Registerservice (string,object,dictionary) Registers the service object object to the interface name String, Can carry a property dictionary Dictionary registerservice (string[],object,dictionary) Registration service object object to interface array group string[], can carry a dictionary of properties Dictionary, that is, a service object can be registered according to multiple interface names, because the class can implement multiple interfaces;

We use the first registration method, modify the Activator class Start method, add the registration code:

Listing 4. Add registration Code

							
public void Start (Bundlecontext context) throws Exception { 
	    
    System.out.println ("Hello World"); 
    Context.registerservice ( 
        IHello.class.getName (), 
        new Defaulthelloserviceimpl (), 
        null); 
	    

In order for our service to be used by other bundle, it must be MANIFEST. MF in the Export declaration, double-click MANIFEST. MF, find runtime > Exported packages > Click Add, as shown, select the Service pack to:

Figure 14. Select the exported Service pack


Another new bundle called Hello World, osgi.test.helloworld2, is used to test the availability of services provided by Osgi.test.helloworld Bundle; Add Import Package: in the second Bundle's MANIFEST. MF file, find dependencies > Imported Packages > Add ..., select the Osgi.test.helloworld.service package we just export out:

Figure 15. Choose the Osgi.test.helloworld.service bag that you just export out


Query service: Similarly, the OSGi framework provides two methods for referencing ServiceReference of query services: getservicereference (String): A reference to a service based on the name of the interface; Getservicereferences ( string,string): According to the interface name and another filter name corresponding to the filter to get a reference to the service; Here we use the first query method, in the Osgi.test.helloworld2 bundle Activator's start method to add a query and test statement:

Listing 5. Add Query and test statements
							
public void Start (Bundlecontext context) throws Exception { 
    System.out.println ("Hello world2"); 
	    
    /** 
        * Test Hello service from Bundle1. 
    * * 
    Ihello hello1 = 
        (Ihello) context.getservice ( 
        context.getservicereference (IHello.class.getName ())); 
        System.out.println (Hello1.gethello ()); 
}

Modify the running environment because we added a bundle, so we also need to add the configuration information for the new bundle in the run configuration, as shown in the following figure:

Figure 16. Add the configuration information for the new bundle


Implementation, the following results are obtained:

Figure 17. Execution results


Congratulations on your success.

Above reproduced from: http://www.ibm.com/developerworks/cn/education/opensource/os-eclipse-osgi/section5.html

Some of my own knowledge:

1, the implementation of the code is hidden: the OSGi framework to achieve the hidden code, the specific implementation of the service is the framework of isolation, the following code is to the OSGI framework registered [Java] View plain copy context.registerservice (Ihel Lo.class.getName (),//interface class new Defaulthelloserviceimpl (),//implementation class NULL);


2, program implementation process: The program start level defines the bundle loading sequence, the current program analysis first run loaded Osgi.test.helloworld_ 1.0.0, which is the start function of the activator running HelloWorld. ---print out the HelloWorld then load the osgi.test.helloworld2_1.0.0,---print out the Hello World2 and invoke the interface, the OSGi framework looks up the implementation class in the directory based on the name of the interface---print hello Osgi,service

3, a few menu: exported packages is everywhere service, publishing interface import package Specify the package used by this plug-in

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.