How to choose in runtime among several OSGi services in an intelligent way?

后端 未结 2 798
耶瑟儿~
耶瑟儿~ 2021-02-09 04:32

I\'ve in mind an intelligent system which can choose among available OSGi services dynamically. That is, choose an implementation or another depending of some runtime parameter.

2条回答
  •  伪装坚强ぢ
    2021-02-09 04:58

    I guess, some sort of dynamic strategy pattern or even dependency injection could fit your needs. Some class uses a strategy (you called it operator) which can change at runtime. I think, you have another service that can tell the which strategy to use (based on runtime parameters).

    A rough implementation could look like that:

    public Worker {
      private Operator operator;    // your actual operator strategy
      public void setOperator(Operator actualOperator) {
        this.operator = operator;
      }
    
      public doSomething() {
    
         while(stopCriterion) {
           Operator operatorForThisIteration = operator;  // pick the injected service
           operatorForThisIteration.doSomething;
         }
      }
    }
    

    And another service, the one that can inject dependencies to worker instances, would maintain a list of all worker instances, implement some logic to choose a new service and inject in into all (or some) workers.

提交回复
热议问题