gpt4 book ai didi

osgi - 依赖于包的 OSGI 包(在 Karaf 下)的自然启动顺序是什么?

转载 作者:行者123 更新时间:2023-12-04 02:15:57 24 4
gpt4 key购买 nike

我在 2.2.8 版本的 Karaf 上遇到问题(很可能在早期版本上也是如此)。

我将使用 Karaf 来托管具有动态部署包的系统。 bundle 由用户部署,我无法事先知道它们是哪些。

我希望 BundleActivator.start() 的顺序与包之间的包依赖性(导入/导出包的依赖性)完全对应,并计划期望可以安全地假设 bundle0 将在 bundle1 运行之前完全初始化开始。但事实并非如此 - 似乎 BundleActivator.start() 是以“随机”顺序调用的,并且忽略了包之间的包依赖性。

示例用例,我有 3 个库

test-lib0 - defines testlib0.ITestRoot, exports testlib0 package 
test-lib1 - defines testlib1.TestRoot implements ITestRoot, exports testlib1 package
test-lib2 - uses both libs, ITestRoot and TestRoot

启动 Karaf 后,我在控制台中看到以下示例输出

karaf@root> TestLib1Activator.start() 
TestLib2Activator.start()
ITestRoot: interface com.testorg.testlib0.ITestRoot - 16634462
TestRoot: class com.testorg.testlib1.TestRoot - 21576551
TestLib0Activator.start()

但我希望它应该始终按此顺序

TestLib0Activator.start() 
TestLib1Activator.start()
TestLib2Activator.start()
ITestRoot: interface com.testorg.testlib0.ITestRoot - 16634462
TestRoot: class com.testorg.testlib1.TestRoot - 21576551

我附上示例项目以供测试。测试用例:在“mvn install”之后只需将 jar 从 ./deploy 文件夹移动到 Karaf 的同一文件夹,跟踪消息应该出现在控制台中。(注意:它可能从第一次尝试就可以正常工作,然后再试一次:))

示例测试项目 http://karaf.922171.n3.nabble.com/file/n4025256/KarafTest.zip

注意:这是来自 http://karaf.922171.n3.nabble.com/What-is-the-natural-start-order-for-dependent-bundle-td4025256.html 的交叉帖子

最佳答案

在 OSGi 中,bundle 的生命周期是installedresolvedstartingstarted

Import-Package 和 Export-Package 只影响包从installedresolved。因此,该框架确保您从中导入包的所有 bundle 都在您的 bundle 之前得到解析,但您的 bundle 只会进入已解析状态。然后在第二步中调用激活器。所以你不能假设激活器是按相同的顺序调用的。如果您在 teSTLib2 可以工作之前需要一些初始化,那么您应该使用 OSGi 服务。

因此,如果我正确理解了您的情况,那么您的 teSTLib0 定义了一个接口(interface),teSTLib1 实现了它,而 teSTLib2 想要使用该实现。所以实现这一点的最好方法是在 teSTLib1 中将 impl 发布为 OSGi 服务,并在 teSTLib3 中引用该服务。然后,您可以将服务与 ServiceTracker 或例如蓝图。我有一个小例子来说明这一点:http://www.liquid-reality.de/x/DIBZ .因此,如果您像我的示例蓝图中那样处理您的案例,请确保 teSTLib2 的上下文仅在服务存在时启动。当服务消失时,它甚至会停止 teSTLib2。

关于osgi - 依赖于包的 OSGI 包(在 Karaf 下)的自然启动顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11574166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com