OSGi how to run mutliple instances of one service

筅森魡賤 提交于 2019-12-06 13:28:09

问题


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);

回答1:


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.




回答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




回答3:


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.



来源:https://stackoverflow.com/questions/25115523/osgi-how-to-run-mutliple-instances-of-one-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!