Introduction to the use of declarative services in OSGi
Declarative services, the so-called declarative Service, I mentioned in the previous article to registered services and declarative services, but in the previous article did not mention how to use declarative services, but simply the concept and relative to the blueprint of the pros and cons, in a word, It's a piece of tape, and these days I think about it, or decide that you need to be careful about declarative services.
Brief introduction
Declarative services, which appears in the specification after OSGi 4, is quoted here as what others say, declarative services is a service-oriented component model that is designed to make it easier to publish, find on the OSGi service platform , binding services, and dynamic management of services, such as monitoring service status and resolving complex dependencies between services. Declarative services uses deferred loading of service components and component Lifecycle management to control the memory footprint and fast start-up, which solves the problems of traditional OSGi service model in the development and deployment of more complex applications, such as large memory consumption, slow startup, etc. And the description of the service component is implemented by XML, which is very easy for the user to understand and use.
The above few words can be introduced in almost any paragraph of OSGi Compendium will mention, in my this is also vulgar to mention a few words, but here I still want to introduce how to use and not simply explain what the latest features of OSGi Compendium, what new specifications need to understand, I think these only need to be seen in the official website more than a few times to understand, the following is the website, IBM article:
Introduction to the declarative Services specification in OSGi
The specifications for the original OSGi compendium are:
OSGi Compendium Original Document R4.cmpn.pdf
Above, the specifications are very clear, I will not repeat here, just to explain how to use declarative services after the release of a service, how in another bundle how to using this service, in this mention, do not write declarative There are some problems with using the service in the bundle of services, which will be explained later.
Writing declarative Services
When using declarative services, make sure that we have added support for compendium in the Pom file, related maven dependencies
As follows:
< Dependency ; <groupid ; Org.osgi</groupid ; <artifactid ; org.osgi.compendium</artifactid ; <version ; ${org.osgi.version}</version > <scope ; provided </scope ; </dependency ;
I have written here a simple program, an interface test, provides a Hello method, and then an implementation of the class testcomponent, the implementation of this method, the specific source code is:
Test:
package cn.com.ds;/** * Created by xiaxuan on 16/7/13. */publicinterface Test { String hello(String name);}
Testcomponet:
PackageCn.com.ds;ImportOrg.osgi.service.component.ComponentContext;ImportOrg.osgi.service.log.LogService;/** * Created by Xiaxuan on 16/7/13. * * Public class testcomponent implements Test { PublicStringHello(String name) {return "Hello"+ Name; } Public void Activate(Componentcontext context) {System.out.println ("Activate ("+ Context +")"); } Public void Deactivate(Componentcontext context) {System.out.println ("Deactivate ("+ Context +")"); } Public void Modified(Componentcontext context) {System.out.println ("Modified ("+ Context +")"); } Public void Bind(Logservice service) {Service.log (Logservice.log_info,"Bind"); } Public void Unbind(Logservice service) {Service.log (Logservice.log_info,"Unbind"); }}
This inside the active and deactive method is used to monitor the activation and deactivation of our services in the use of, here to log, indicating that the component is activated, the source code is relatively simple, now to observe the configuration file writing, This requires creating a Osgi-inf folder under the Resources directory, and then creating the file Components.xml, which is configured in XML, as follows:
<?xml Version = "1.0" encoding = "UTF-8"?><component name="Testcomponent"> <implementation class="cn.com.ds.TestComponent" /> <service> <provide interface="Cn.com.ds.Test"/> </Service> <reference bind="bind" cardinality="1..1"interface= "Org.osgi.service.log.LogService" name= "logservice"policy="dynamic" unbind="Unbind" /> </Component>
I write a service in component, to provide services outside, but also reference the services of Logservice, we in the implementation of the method, repeated use of logservice this service, what needs to mention here is that I'm using a declarative service in the form of a configuration file, but OSGi can also provide an annotated way to configure our components to achieve the same effect, and after this is done, we expose the service to the current bundle, We need to add a behavior to Felix's plugin as follows:
<Service-Component>OSGI-INF/components.xml</Service-Component>
In this case, the service is exposed in the MF file in the bundle after the install is completed.
Using declarative Services
Using this service is fairly straightforward, introducing the DS bundle in the feature configuration file, and then adding the DS dependency in the pom dependency, you can use the service provided by the DS bundle, which is fairly simple to use, as I wrote in Activator. And the previous Servicetracker written together, is simply to use the context to get the service, and then use, as follows:
PackageCn.com.example;ImportCn.com.ds.Test;ImportCn.com.ds.TestComponent;Importorg.osgi.framework.*;/** * Created by Administrator on 2016/6/18. * * Public class Activator implements bundleactivator {Helloservicetracker Servicetracker; Serviceregistration serviceregistration; Public void Start(Bundlecontext Bundlecontext)throwsException {System.out.println ("Service registered ...");//Turn on the service trackerServicetracker =NewHelloservicetracker (Bundlecontext); Servicetracker.open ();//// Registration Service//Serviceregistration = Bundlecontext.registerservice (Helloservice.class, New Helloserviceimpl (), null);//// get B Tracked Services//HelloService HelloService = (helloservice) servicetracker.getservice ();//if (HelloService! = null) {//System.out.println (Helloservice.hello ("Xiaxuan"));// }ServiceReference servicereference = bundlecontext.getservicereference (Test.class); Test testcomponent = (test) bundlecontext.getservice (ServiceReference); System.out.println (Testcomponent.hello ("Bingwen")); } Public void Stop(Bundlecontext Bundlecontext)throwsException {System.out.println ("Stopping service");//Close service tracker if(Servicetracker! =NULL) {servicetracker.close (); }//Logout serviceServiceregistration.unregister (); }}
There is no difference between this activator and the activator previously written in Servicetracker, and I wrote two knowledge points together, where the code that uses the DS service is the last two lines in start.
ServiceReference serviceReference = bundleContext.getServiceReference(Test.class);Test testComponent = (Test) bundleContext.getService(serviceReference);System.out.println(testComponent.hello("bingwen"));
In use, there is no difference.
Program run
Now start Karaf and observe the results:
Here we can see that we are using the test service normally, and test gets the corresponding instance. DS is normally used here.
Summarize
-
Compared to declarative services, individuals prefer to use blueprint some, after all, compared to ds,blueprint more convenient.
-
In the bundle in the DS declaration, do not use the service in Activator because the active time of component is after the Start method in Activator, and the component destroy time is also before the Stop method, It is not possible to get the corresponding instance, so the individual thinks it is best to declare the service in one bundle and to refer to it in another bundle.
-
The next article will explain the eventadmin communication process between bundles.
Application of declarative Services in OSGi