如何在运行时智能地在多个OSGi服务中进行选择?

发布于 2024-11-03 18:22:47 字数 717 浏览 3 评论 0原文

我想到的是一个智能系统,它可以动态地在可用的 OSGi 服务中进行选择。也就是说,根据某些运行时参数选择一个或另一个实现。例如,通知正在运行的算法,该算法在多次迭代后更改运算符,或者根据系统中的负载平衡或其他情况进行更改。

while(stopCriterion){
    operator.doSomething(); //There exist many operator implementations
}

我的第一个方法是使用 DS 公开服务并使用 0..n 和动态策略绑定服务。然后,从外部智能组件,通知算法在每次迭代中使用哪个服务(也许使用EventAdmin?)。

operator[selected].doSomething();

当必须执行许多不同服务实现的实验时,这可以帮助我降低复杂性。另外,我计划使用远程服务规范和 Eclipse 通信框架 来研究分布式算法和那些东西,所以在执行时动态出现新的实现也是可能的,

但是,我不知道这是否是一个好主意,或者是否存在另一种更好的机制来动态选择使用哪个实现。我认为使用 ServiceTracker 代替 DS 不是一个好的选择,但我愿意接受建议:)

提前致谢。

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. For example, notify to a running algorithm that change an operator after several iterations, or depending of load balancing in a system or whatever.

while(stopCriterion){
    operator.doSomething(); //There exist many operator implementations
}

My first approach is to use DS to expose the services and bind services with 0..n and dynamic policy. Then, from an external intelligent component, notify the algorithm which service use in every iteration (using EventAdmin, maybe?).

operator[selected].doSomething();

This could help me to reduce complexity when many experiments with a lot of different service implementations must be executed. Also, I am planning to use Remote Services specification with Eclipse Communication Framework to make research in distributed algorithms and that stuff, so dynamically appearing of new implementations in execution time also could be possible

However, I don't know if is this a good idea or there exist another better mechanism to dynamically select which implementation use. I think that using ServiceTracker instead DS is not a good option, but I'm open to suggestions :)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-11-10 18:22:47

在我看来,这就像一种策略模式,可以很好地使用服务来实现。假设您有一种名为 Operator 的服务(并且有一个同名的接口),其工作原理大致如下:

  • 创建一个 OperatorProvider 服务,其中包含必要的功能和一些附加信息(例如,此实现何时合适),并创建多个实例,每个实例对应一个策略。
  • 创建一个选择器服务,该服务实现 Operator 接口,并将对该服务的所有调用引导至最合适的 OperatorProvider。该服务选择最合适的提供商的方式可能是情报的一部分。
  • 服务的实际用户现在仅依赖于 Operator 服务,而不必担心提供者的选择。

我假设您可以将选择策略放在选择器服务中,但如果它确实是外部组件,您可以使用任何您喜欢的机制来处理智能组件和选择器之间的通信:服务接口、事件等。

This looks to me like a strategy pattern, which can very well be implemented using services. Assuming you have a type of service called Operator (and have an interface with the same name), this would work roughly like this:

  • Create a OperatorProvider service, which contains the necessary functionality, and some additional information (such as, when is this implementation suitable), and create a number of instances of that, one for each of your strategies.
  • Create a selector service, which implements the Operator interface, and channels all calls to the service to the most suitable OperatorProvider. The way in which this service selects the most suitable provider, is probably part of the intelligence.
  • The actual user of the service now only has a dependency on an Operator service, and does not have to worry about the provider selection.

I assume you can put the selection strategy in the selector service, but if it really is an external component, you can use any mechanism you like to handle the communication between the intelligent component and the selector: a service interface, events, etc.

酷遇一生 2024-11-10 18:22:47

我想,某种动态策略模式甚至依赖注入可以满足您的需求。某些类使用可以在运行时更改的策略(您将其称为运算符)。我认为,您有另一个服务可以告诉使用哪种策略(基于运行时参数)。

粗略的实现可能如下所示:

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;
     }
  }
}

另一个服务,即可以将依赖项注入到工作实例的服务,将维护所有工作实例的列表,实现一些逻辑来选择新服务并注入到所有(或某些)工作人员中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文