views:

231

answers:

1

I am wondering if it possible to do the equivalent of

public void start(BundleContext context)
{  
    String filter = "filter for my specific service";
    context.addServiceListener(new MyServiceListener(), filter);
}

with Spring DM. I have found reference material for adding listeners for lifecycle management. But this relates to the lifecycle of the service being registered. What I need is to be made aware of the lifecycle of any service that is registered/unregistered of a specific service interface.

The bundle that needs to know this is not actually creating the service, but will be using it. Since there will multiple instances of this service that will change at runtime, I cannot simply create a service reference in my Spring configuration.

+1  A: 

Spring DM includes support for referencing a collection of services. Unfortunately, as a new user, I can't post links, but this functionality is described in section 7.2.2 of the Spring DM 1.2 documentation. Spring DM automatically updates the contents of this collection at runtime as services are added to and removed from the service registry.

When you declare a reference upon a service, be it as a single reference, or a collection of services, you can also declare a listener bean that will be notified as services come and go. This is documented in section 7.2.3.

I'd recommend making use a List or Set of services, and using a listener bean so that you're notified of changes to the collection's contents.

Andy Wilkinson
Yes, I found this documentation myself on Friday evening.
Robin