Java Reference
In-Depth Information
So far we've been talking about how damping works for service references. Refer-
ence lists are subtly different from references in how they handle dynamism, and
sometimes this difference comes in handy.
REFERENCES AND REFERENCE LISTS
A reference refers to one, and only one, instance of a service. Reference lists, on the
other hand, refer to several instances of a service. Although reference lists still use
proxies, they make it much more apparent when services come and go. As services
appear and disappear from the system, they'll pop in and out of a reference list (see
figure 6.11). A reference list will never contain entries for services that are unavail-
able. So dynamic is the reference list, services can even appear in an existing iterator.
(No ConcurrentModificationException s here!)
How does this help with optional services? If you use an optional reference list
rather than an optional reference to inject a service, then the service's absence still
won't prevent the bundle's Blueprint container from starting. The key difference is
that if a service arrives and then goes away again, you'll find a zero-length list rather
than a long wait and a ServiceUnavailableException .
Coping with multiplicity
On the other hand, if you use a reference list, you may get more than you bargained
for. What will you do if multiple instances of a service are present? You could decide
you want to call all the services in the list, but on the other hand you may never want
more than one service. If you do only want to call one service, then which service do
you call? Will the first service be the most appropriate one?
It turns out that this is an issue that affects you even if you're using references
instead of reference lists. With references, the container will choose the service for
you, but how does it decide which service to pick?
One strategy for ensuring you get supplied with the most suitable service is to spec-
ify a ranking for each service. (We showed you how to do this in section 6.3.6.) The
downside here is that the ranking must be specified at the time the service is regis-
tered by the service provider. If that's not you, you'll have some work to do persuading
New
service
instance
Proxy
Service
instance
Proxy
Service
instance
Proxy
Service
instance
Proxy
Reference
list
Service
instance
Proxy
Reference
list
Figure 6.11 As services appear and disappear in a system, they're automatically added to or removed
from reference lists.
Search WWH ::




Custom Search