如何在运行时智能地在多个OSGi服务中进行选择?
我想到的是一个智能系统,它可以动态地在可用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这就像一种策略模式,可以很好地使用服务来实现。假设您有一种名为
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: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.Operator
interface, and channels all calls to the service to the most suitableOperatorProvider
. The way in which this service selects the most suitable provider, is probably part of the intelligence.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.
我想,某种动态策略模式甚至依赖注入可以满足您的需求。某些类使用可以在运行时更改的策略(您将其称为运算符)。我认为,您有另一个服务可以告诉使用哪种策略(基于运行时参数)。
粗略的实现可能如下所示:
另一个服务,即可以将依赖项注入到工作实例的服务,将维护所有工作实例的列表,实现一些逻辑来选择新服务并注入到所有(或某些)工作人员中。
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:
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.