OSGI中声明式服务的动态目标 [英] Dynamic target for declarative service in OSGI

查看:153
本文介绍了OSGI中声明式服务的动态目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定使用服务的消费者,消费者如何使用陈述性服务来动态选择特定提供商?



示例



Service.java

  public interface Service {
public void do ();
}

Provider1.java



< pre class =lang-java prettyprint-override> public class Provider1 implements Service {
@Override
public void do(){
// a way
}
}

Provider2.java

  public class Provider2 implements Service {
@Override
public void do(){
//另一个方式
}
}

Consumer.java

  public class Consumer {
private Service myService;

protected void bindService(Service s){//实际上是Provider1
myService = s;
}

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

public void useThisKindOfService(String s){
//做某事疯狂
}
}

所以,我想要的不是做一些疯狂,要找到一种方法来重新配置消费者,以释放Provider1并要求Provider2。



可以吗?



与重复问题相关的更新



OSGI / Felix声明性服务:如何过滤要绑定的服务



在我的上下文中,我无法使用声明性目标,因为在构建时必须知道目标的值,在我的情况下,目标可以由用户在运行时定义。

解决方案

声明服务的组件可以通过ConfigurationAdmin进行配置。通过这样做,组件的配置可以在运行时更改。



您还可以通过ConfigurationAdmin更改配置 myService.target 运行。如果你这样做,另一个参考将绑定到你的组件。



如果您的组件的引用策略是动态的,新参考将被绑定,而不会重新激活您的组件。



有关更多信息,请参阅OSGi纲要规范的声明服务章节。


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.

解决方案

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.

这篇关于OSGI中声明式服务的动态目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆