Java Reference
In-Depth Information
Given this declaration, the Blueprint container knows it must inject an instance of A
from the service registry into the component by calling the setService() method. It's
also possible for Blueprint to inject the service proxy via a constructor. The following
class C has a dependency on service A , which is injected via a constructor argument:
public class C {
private A a;
public C(A a) { this.a = a }
public void someAction() { a.doit(); }
}
In this case, the Blueprint service-dependency description looks like this:
<reference id="a" interface="A"/>
<bean id="c" class="C">
<argument ref="a"/>
</bean>
What if your client code doesn't depend on a single service instance, but instead wants
to aggregate several service instances? The following example shows a class D that
aggregates many A services from the OSG i service registry:
public class D {
private List<A> list;
public void setServices(List<A> list) { this.list = list }
public void someAction() {
for (A a : list) {
a.doit();
}
}
}
In this case, class D is injected with a proxied list that aggregates the OSG i services.
Changes in the OSG i service registry are reflected in the list. New services are
appended to the end of the list, and old services are removed. The XML to describe
this sort of dependency in Blueprint is as follows:
<reference-list id="a" interface="A"/>
<bean id="d" class="D">
<property name="services" ref="a"/>
</bean>
This is similar to the previous dependency on a single service. The difference is that
you replace the XML <reference/> element with the <reference-list/> element.
Proxies and service dynamism
Blueprint uses proxies to mask OSGi service dynamism so you don't have to worry
about the concurrency issues in your code. This approach can't solve all issues re-
lated to dynamism. For example, in this static view of services, what happens if the
underlying service goes away and no replacements are available? In this case, the
service proxy blocks the calling thread, waiting for a replacement service, and will
eventually throw an org.osgi.framework.ServiceUnavailableException after a
certain timeout period if no replacement service arrives.
 
Search WWH ::




Custom Search