2

Is it possible to run multiple instance of the same service in the osgi framework? More specific, I need to start multiple instances of a service, but each instance should recieve different parameters. This is because the services have similar functionality. But instead of writing a service for every variation, I want to reuse one implementing class.

I've already found the registerService method in the framework api.

    ServiceRegistration<?> registration = bundlecontext.registerService(
            className, class, null);

however, i seem to create only one instance of each class. Is there a workaround for this?

preferably something like

    ServiceRegistration<?> registration = bundlecontext.registerService(
            className + "#" + (++counter), new classInstance(), null);
Joris W
  • 517
  • 3
  • 16

3 Answers3

3

Note that using Declarative Services with the corresponding annotations makes this quite easy, here's an excerpt from the Apache Sling codebase (ConfiguredFeature.java):

@Component(
        name = "org.apache.sling.featureflags.Feature",
        metatype = true,
        configurationFactory = true,
        policy = ConfigurationPolicy.REQUIRE)
@Service
public class ConfiguredFeature implements Feature {

    @Property(label = "Name", description = "Short name of this feature")
    private static final String NAME = "name";

    private String name;

    @Activate
    private void activate(final Map<String, Object> configuration) {
        this.name = PropertiesUtil.toString(configuration.get(NAME), "");
    }

    ...
}

Using configurationFactory = true and policy = ConfigurationPolicy.REQUIRE causes one instance of this service to be created for each corresponding OSGi configuration, which is a natural way of creating multiple instances.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
2

You could create a ManagedServiceFactory. The factory can register a new service for each configuration set in Configuration Admin.

I have a simple example here which uses Felix DependencyManager to register the component: https://github.com/paulbakker/osgicourse/tree/master/greeterfactory/src/greeterfactory

Paul Bakker
  • 1,024
  • 7
  • 9
  • I'm rather new to osgi, so i don't know that much yet. But the 'org.apache.felix.dm' and 'org.osgi.service.cm' are not included in the target platform. Where can i find them and how do i add them? – Joris W Aug 05 '14 at 12:09
1

You have the parameters slightly wrong, or at least misleading:

ServiceRegistration<?> registration = bundlecontext.registerService(
        className, class, null);

The second parameter to registerService is an object, not a class. This is an object you instantiate yourself. You can create as many as you like, in whatever way you like, before passing them to OSGi.

However if you are doing this with externally-supplied configuration data, should look into Declarative Services and their ability to receive config from the OSGi Configuration Admin service.

UPDATE Taking another look at your question, I see the counter that you tried to add to the class name. This is not required and in fact not permitted either. Just call registerService multiple times.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77