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

后端 未结 2 797
耶瑟儿~
耶瑟儿~ 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 05:21

    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.

提交回复
热议问题