1

我还有另一个与 iPOJO 中的动态更新有关的问题:

1.问题:

  • 假设我有 2 个组件 A 和 B。这两个组件分别由 A_impl.java 和 B_impl.java 实现。它们实现了两个服务 A_service 和 B_service。
  • 组件“C”使用 A_service 声明如下:

//

@Component(name="C", immediate=true)
@Instantiate
public class C_impl {
        @Requires
        A_Service service;
}

//

  • 所有三个组件都已部署并且工作正常。

2.要求:

  • 现在,我将通过将 C_impl.java 中的依赖类型(@Requires A_service 服务)更改为(@Requires B_service 服务)来动态更新“C”的实现,即它在运行时将 C_impl 中的 A_service 更改为 B_service。

问题

如何开发独立组件来重新配置(控制)组件实现?我读过(http://felix.apache.org/site/dive-into-the-ipojo-manipulation-depths.html)但我不太明白。提前感谢您的回复

4

1 回答 1

1

Well, you want to change the component class of a component ? This will not really work.

The only way would be to have the two implementation available without instance declared (no @instantiate) and create a component that required both Factories (org.apache.felix.ipojo.Factory service) and create the instances when needed. Obviously, if you need replacement, you would also need to dispose the first created instance when creating the second one.

So, it would need a component like this (this is pseudo code):

@Component(immediate=true)
@Instantiate
public class Controller {

    @Requires(filter="(factory.name=A)")
    Factory factoryOfA;

    @Requires(filter="(factory.name=B)")
    Factory factoryOfB;    

    ComponentInstance instance;

    @Validate
    public void createA() throws Exception {
        instance = factoryOfA.createInstance(null);
    }

    public void switchToB() throws Exception {
        instance.dispose();
        instance = factoryOfB.createInstance(null);
    }
}
于 2014-11-11T10:40:49.293 回答