Java Reference
In-Depth Information
4.2
OSGi services in action
What do we mean by dynamic ? After a bundle has discovered and started using a ser-
vice in OSG i, it can disappear at any time. Perhaps the bundle providing it has been
stopped or even uninstalled, or perhaps a piece of hardware has failed; whatever the
reason, you should be prepared to cope with services coming and going over time.
This is different from many other service frameworks, where after you bind to a ser-
vice it's fixed and never changes—although it may throw a runtime exception to indi-
cate a problem.
OSG i doesn't try to hide this dynamism: if a bundle wants to stop providing a ser-
vice, there's little point in trying to hold it back or pretend the service still exists. This
is similar to many of the failure models used in distributed computing. Hardware
problems in particular should be acknowledged and dealt with promptly rather than
ignored. Fortunately, OSG i provides a number of techniques and utility classes to
build robust yet responsive applications on top of such fluidity; we'll look more closely
at these in chapters 11 and 12. But before we can discuss the best way to handle
dynamic services, you first need to understand how OSG i services work at the basic
level, and to do that we need to introduce the registry.
The OSG i framework has a centralized service registry that follows a publish-find-
bind model (see figure 4.8). To put this in the perspective of service providers and
consumers,
A providing bundle can publish Plain Old Java Objects ( POJO s) as services.
A consuming bundle can find and then bind to services.
Framework
Bundle
OSGi
service
registry
Publish
Find
Find
Figure 4.8 OSGi
service registry
You access the OSG i service registry through the BundleContext interface, which you
saw in section 3.2.4. Back then, we looked at its lifecycle-related methods; now we'll
look into its service-related methods, as shown in the following listing.
Listing 4.1 BundleContext methods related to services
public interface BundleContext {
...
void addServiceListener(ServiceListener listener, String filter)
throws InvalidSyntaxException;
void addServiceListener(ServiceListener listener);
void removeServiceListener(ServiceListener listener);
 
Search WWH ::




Custom Search