如何扩展/修改OSGi生命周期管理?
我有一个模块化应用程序,它使用 OSGi 进行生命周期和依赖项管理。但是,某些捆绑包在启动后需要一些时间才能准备就绪,例如,因为它们必须从某个地方获取数据。此外,它们可能无法在配置更新期间处理某些调用,例如,保持与数据库连接的捆绑包在更新连接参数时无法发送查询。
因此,在我看来,捆绑包可以具有比 OSGi 容器管理的状态更微妙的状态,并且由于它们影响捆绑包交互,因此需要进行一些处理。我可以看到执行此操作的三种基本策略:
- 巧妙的调整,例如将所有初始化代码放入
BundleActivator.start()
中。如果需要很长时间才能获取该数据,那么捆绑包就不会永远启动。我不能 100% 确定这会涵盖所有情况,而且似乎有点错误。 - 为我的捆绑包配备一个额外的事件系统,它们用来通知彼此更微妙的状态,例如“暂时不可用”或“现在真的准备好了”。这可能只是不必要的开销。
- 让捆绑包保留其更微妙的状态更改,无论如何都只接受调用,如有必要,在内部推迟它们。当调用者实际上可以更好地处理不可用性时,这可能不合适。
您对此有什么一般性建议吗? OSGi 中是否有我可以使用的东西?
I have a modular application that uses OSGi for lifecycle and dependency management. However, some of the bundles require some time after startup to be ready, for example, because they have to acquire data from somewhere. Also, they might be unable to process certain calls during a configuration update, for example a bundle that keeps a connection to a db can't send queries while it is updating the connection parameters.
So, it seems to me that a bundle can have more subtle states than those managed by the OSGi container, and since they affect bundle interaction, there needs to be some handling. I can see three basic strategies for doing this:
- screw subtlety, and for example put all initialization code into
BundleActivator.start()
. If it takes forever to acquire that data, well then the bundle just won't be started forever. I'm not 100% sure that this would cover all cases, and it seems slightly wrong. - fit my bundles with an additional event system that they use to notify each other of more subtle states like "momentarily unavailable" or "really ready now". This might simply be unnecessary overhead.
- have the bundle keep its more subtle state changes to itself and just take calls anyway, deferring them internally if necessary. This might not be appropriate when the caller could actually handle unavailability better.
Do you have any general advice on that? Is there even something in OSGi that I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用服务和服务跟踪器在捆绑包之间进行通信。这也将有助于解耦您的捆绑包,因为您可能会使用接口。
使用与数据库对话的捆绑包示例:
Try using services and service tracker to communicate between bundles. This will also help decouple your bundles as you'll likely be using interfaces.
Using you example of bundle which talks to a database: