Java Reference
In-Depth Information
remote registry and injection into the OSG i service registry. To give context for the
example, the following listing shows the implementation of the addWatch() method
of RegistryWatcher .
Listing 15.17 RegistryWatcher helper addWatch() method
public void addWatch(String clazz, String filter) {
Watch watch = new Watch(clazz, filter);
synchronized (watches) {
Integer count = watches.get(watch);
if (count == null) {
log.info("Adding watch " + clazz + " -> " + filter);
Collection<RegistryServiceReference> services = registry
.findServices(clazz, filter);
for (RegistryServiceReference ref : services) {
if (!regs.containsKey(ref)) {
log.debug("Registering " + ref);
Future<ServiceRegistration> future = exec
.submit(new Registration(ref));
regs.put(ref, future);
}
else {
log.debug("Already registered " + ref);
}
}
} else {
watches.put( watch, count + 1 );
}
}
}
You begin by checking whether this a new Watch —a unique class and filter request. If
it is, you find the existing services that match your watch criteria from the Remote-
Registry . For each service, you check whether you've already imported it for a differ-
ent watch. If this is in fact a new service, you create a new Registration callable
object. Here, the Registration callable object is submitted to a background thread
executor to avoid deadlock scenarios that can occur if you execute external code
while holding the object lock on the m_watches object. Finally, you store the future
ServiceRegistration for tidying up later, should the Watch be removed.
The next listing shows the code for the Registration inner class.
Listing 15.18 Registration callable
public class RegistryWatcher {
...
class Registration implements Callable<ServiceRegistration> {
private final RemoteServiceReference ref;
public Registration(RemoteServiceReference ref) {
this.ref = ref;
}
public ServiceRegistration call() throws Exception {
 
Search WWH ::




Custom Search