6.4. Leveraging the Service Registry

We have seen that the framework maintains a service registry to which a bundle can publish its service by calling the registerService method and from which another can get the service by calling the getServiceReference(s) then getService methods on the BundleContext interface. However, it may not be apparent that you can in fact register any type of object with the registry.

For example, the following snippet is entirely valid, although probably not useful:

for (int i = 0; i < 100; i++)
   bundleContext.registerService(
         "java.lang.Integer", new Integer(i));

And the following call would then return 100 service references to Integer objects in the array

 ServiceReference[] ref = bundleContext.getServiceReferences( ...

Get Programming Open Service Gateways with Java Embedded Server™ Technology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.