OSGI中声明式服务的动态目标 [英] Dynamic target for declarative service in 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。
可以吗?
与重复问题相关的更新
在我的上下文中,我无法使用声明性目标,因为在构建时必须知道目标的值,在我的情况下,目标可以由用户在运行时定义。
声明服务的组件可以通过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屋!