如何扩展/修改OSGi生命周期管理?

发布于 2024-08-07 15:31:30 字数 576 浏览 2 评论 0原文

我有一个模块化应用程序,它使用 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 技术交流群。

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

发布评论

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

评论(1

演出会有结束 2024-08-14 15:31:30

尝试使用服务和服务跟踪器在捆绑包之间进行通信。这也将有助于解耦您的捆绑包,因为您可能会使用接口。

使用与数据库对话的捆绑包示例:

  1. A​​ctivator.start(),启动后台线程来执行初始化业务。激活器应该快速执行。
  2. 注册一个服务,为其他包提供所需的数据库相关抽象。
  3. 其他捆绑包创建一个服务跟踪器来查找它们需要的服务,
  4. 仅当第一个捆绑包注册服务时,其他捆绑包才会在服务跟踪器中获得回调。第一个捆绑包仅在服务准备就绪时才注册该服务,这意味着其他捆绑包可以立即开始使用它。

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:

  1. Activator.start(), launch a background thread to do your initization business. Activators should execute quickly.
  2. Register a service that provides the needed DB related abstraction for other bundles.
  3. Other bundles create a service tracker to look for services they need,
  4. Other bundles get a callback in the service tracker only when the first bundle registers the service. The first bundles only registers the service when it's ready implying that other bundles can begin using it immediately.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文