Dynamic target for declarative service in OSGI

橙三吉。 提交于 2019-12-20 04:13:43

问题


Given a consumer which uses a service, how can this consumer select a specific provider dynamically using declarative service ?

Example

Service.java

public interface Service {
    public void do();
}

Provider1.java

public class Provider1 implements Service {    
    @Override
    public void do(){
        //a way 
    }   
}

Provider2.java

public class Provider2 implements Service {    
    @Override
    public void do(){
        //another way 
    }   
}

Consumer.java

public class Consumer {
    private Service myService;

    protected void bindService(Service s){ // Actually it's Provider1
        myService = s;
    }

    protected void unbindService(Service s){
        myService = null;
    }

    public void useThisKindOfService(String s){
        // Do something crazy
    }
}

So, what I would like it's instead of "Do something crazy", to find a way to reconfigure the consumer in order to release Provider1 and ask for Provider2.

Is it possible ?

Update related to "Duplicate Question"

OSGI/Felix Declarative services: How to filter the services to be bound

In my context I cannot use the declarative target because the value of the target has to be know at build time, in my case the target could be defined by a user at runtime.


回答1:


Components of Declarative Services can be configured via ConfigurationAdmin. By doing that, the configuration of the component can be changed at runtime.

You can also change the configuration of myService.target via ConfigurationAdmin at runtime. If you do that, another reference will be bound to your component.

If the policy of the reference of your component is dynamic, the new reference will be bound without reactivating your component.

For more information, see the Declarative Services chapter of the OSGi Compendium specification.



来源:https://stackoverflow.com/questions/32785821/dynamic-target-for-declarative-service-in-osgi

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