The application of OSGi Servicelistener

Source: Internet
Author: User

Introduction to the application of OSGi Servicelistener

I wanted to skip the basics of OSGi directly, starting with some intermediate or high-level articles in OSGi, and then thinking about Servicelistener, Servicetracker, ds in OSGi, So decided to osgicommand the contents of the follow-up, starting from Servicelistener, but here will not tell the Felix official website Servicelistener example, Instead, they write example and explain them separately.

Servicelistener
/** * A {@code ServiceEvent} listener. {@code ServiceListener} is a * listener interface that may be implemented by a bundle developer. When a * {@code ServiceEvent} is fired, it is synchronously delivered to a * {@code ServiceListener}. The Framework may deliver * {@code ServiceEvent} objects to a {@code ServiceListener} out * of order and may concurrently call and/or reenter a * {@code ServiceListener}. *  */

Servicelistener explanation as shown above, the original meaning is that this is just an interface, after the corresponding implementation, can be used to monitor some services, in the service registration, modification, logoff, the corresponding other calls, now I will tell the specific use of this servicelistener, In the process of use, and then to explain the middle there will be those problems.

Servicelistener Usage 1

First, provide an interface and an implementation class, respectively, HelloService and Helloserviceimpl, the code is as follows:

HelloService:

package cn.com.example;/** * Created by xiaxuan on 16/7/12. */publicinterface HelloService {    String hello(String name);}

Helloserviceimpl:

package cn.com.example;/** * Created by xiaxuan on 16/7/12. */publicclass HelloServiceImpl implements HelloService {    publichello(String name) {        return"hello " + name;    }}

is very simple, is to provide a simple service, there is a method for Hello, the real focus is still in the activator, now first put out the code in the activator, and then to explain the listening service and the problem is easy to appear:

 PackageCn.com.example;Importorg.osgi.framework.*;/** * Created by Xiaxuan on 16/7/12. * * Public  class Activator2 implements bundleactivator, servicelistener { Serviceregistration serviceregistration; Bundlecontext context; Public void Start(Bundlecontext context)throwsException { This. Context = Context; Context.addservicelistener ( This); System.out.println ("Service registered ...."); Serviceregistration = Context.registerservice (Helloservice.class,NewHelloserviceimpl (),NULL); } Public void Stop(Bundlecontext context)throwsException {Context.removeservicelistener ( This); System.out.println ("Service unregistered ...");    Serviceregistration.unregister (); } Public void servicechanged(Serviceevent event) {Switch(Event.gettype ()) { CaseServiceevent.registered://Get Service ReferenceServiceReference ref = Context.getservicereference (Helloservice.class);//Get Service instanceHelloService HelloService = (helloservice) context.getservice (ref);if(HelloService! =NULL) {//Call service methodSystem.out.println (Helloservice.hello ("Xiaxuan"));//Release serviceContext.ungetservice (ref); } Break; CaseServiceEvent.UNREGISTERING:System.out.println ("servicechanged Find service unregistered."); Break; }    }}

After we implement Servicelistener in activator, we also need to add ourselves to the listener, in the Servicechanged method, when the service changes can be monitored in this method, which either register, logoff, Or changes can be made to listen to, now we hit breakpoints, monitoring related services.

Service startup

Service startup process is no longer, this time we in the stop bundle, and then start the bundle again to debug and view the corresponding information, now show that Karaf has been successfully started, as follows:

Can see example this bundle has started normally, and now use the Stop command, essentially in Karaf This command is the Bundle:list command, in the Karaf a large number of use of the command, after the time will talk about the Karaf source, talk about these commands , this time example's ID is 8, we use Stop 8 to stop the bundle, after executing the command as follows:

When the state of the bundle is resolved, the bundle has stopped, then we start the bundle, the command is start 8, this time we observe our breakpoint, this breakpoint is set in the Servicechanged method, As shown below:

We release the breakpoint, this time the program enters the breakpoint again, as follows:

Found this time to enter the breakpoint, the event type and the last difference, indicating here the Servicelistener Servicechange method to the current bundle start hand, all the service registration has done the monitoring, this time, we remove breakpoints, observe the console, Will find the ' Hello Xiaxuan ', which has hit many times, as follows:

Five times the Hello Xiaxuan, stating that there are five times the service registration, each service registration came in to get a helloservice, and then called a service, in the console printing once, Servicelistener did not listen to a specific service.

Servicelistener Usage 2

In the above Servicelistener usage, and not only in the activator implementation of Servicelistener to do the listening service, can be directly implemented in the Start method, if the Activator class to implement the Servicelistener interface , you also need to add itself to the listener, the code is as follows:

context.addServiceListener(this);

If implemented in the Start method, the code looks like this:

 PackageCn.com.example;Importorg.osgi.framework.*;/** * Created by Xiaxuan on 16/7/13. * * Public  class Activator3 implements bundleactivator {Serviceregistration serviceregistration; Bundlecontext context; Public void Start(Bundlecontext Bundlecontext)throwsException { This. context = Bundlecontext; System.out.println ("Service registered ...."); Serviceregistration = Context.registerservice (Helloservice.class,NewHelloserviceimpl (),NULL); Context.addservicelistener (NewServicelistener () { Public void servicechanged(Serviceevent event) {Switch(Event.gettype ()) { CaseServiceevent.registered://Get Service ReferenceServiceReference ref = Context.getservicereference (Helloservice.class);//Get Service instanceHelloService HelloService = (helloservice) context.getservice (ref);if(HelloService! =NULL) {//Call service methodSystem.out.println (Helloservice.hello ("Xiaxuan"));//Release serviceContext.ungetservice (ref); } Break; CaseServiceEvent.UNREGISTERING:System.out.println ("servicechanged Find service unregistered."); Break;    }            }        }); } Public void Stop(Bundlecontext context)throwsException {System.out.println ("Service unregistered ...");    Serviceregistration.unregister (); }}

New Servicelistener directly in Addservicelistener, and then implement the relevant method, compared to a little bit simpler, but the effect is the same.

Summarize
    • When using Servicelistener, the cost is only obtained when the service changes, and the registration and logoff of the service can be dynamically sensed.

    • However, the problem is that the service that existed before the Servicelistener registration could not be monitored, it was necessary to maintain the access and release of the service, and it was inconvenient to listen to multiple service.

    • As long as the service changes when the relevant monitoring, in fact, in many cases do not need to listen to other services, sometimes used to be more troublesome.

    • The next section will explain the Servicetracker, which is a further encapsulation of the servicelistener and can effectively monitor the services we need.

The application of OSGi Servicelistener

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.