我在维基百科和其他网站上读过关于OSGi的文章,但我并没有真正看到大局。它说它是一个基于组件的平台,并且您可以在运行时重新加载模块。同样,到处都给出的“实际示例”是Eclipse插件框架。

我的问题是:

OSGi清晰而简单的定义是什么? 它能解决什么常见问题?

所谓“常见问题”,我指的是我们每天都要面对的问题,比如“OSGi能做些什么来让我们的工作更高效/有趣/简单?”


当前回答

它还被用于在移动端带来额外的中间件和应用程序的可移植性。移动端可用于WinMo, Symbian, Android等。一旦与设备功能集成,就会出现碎片化。

其他回答

在OSGi上让我抓狂的几件事:

1)实现和它们的上下文加载器有很多怪癖,并且可能有点异步(我们在confluence中使用felix)。与纯spring(没有DM)相比,[main]几乎运行了所有同步。

2)热加载后类不相等。例如,在hibernate上有一个tangosol缓存层。它被Fork.class填充,在OSGi作用域之外。你热加载了一个新的罐子,Fork没有改变。Class[Fork] != Class[Fork]。由于相同的潜在原因,它也会在序列化过程中出现。

3)集群。

您可以解决这些问题,但这是一个非常麻烦的问题,并且会使您的体系结构看起来有缺陷。

对于那些为热插拔做广告的人。OSGi的#1客户端?Eclipse。Eclipse在加载包之后做什么?

它重新启动。

You can, analogically speaking, change the motor of your car without turning it off. You can customize complex systems for the customers. See the power of Eclipse. You can reuse entire components. Better than just objects. You use a stable platform to develop component based Applications. The benefits of this are huge. You can build Components with the black box concept. Other components don't need to know about hidden interfaces, them see just the published interfaces. You can use in the same system several equal components, but in different releases, without compromise the application. OSGi solves the Jar Hell problem. With OSGi you develop thinking to architect systems with CBD

对于使用Java的每个人来说,有很多好处(我现在只提醒了这些)。

I don't care too much about the hotplugability of OSGi modules (at least currently). It's more the enforced modularity. Not having millions of "public" classes available on the classpath at any time protects well from circular dependencies: You have to really think about your public interfaces - not just in terms of the java language construct "public", but in terms of your library/module: What (exactly) are the components, that you want to make available for others? What (exactly) are the interfaces (of other modules) you really need to implement your functionality?

这很好,热插拔是附带的,但我宁愿重新启动我通常的应用程序,而不是测试所有的热插拔组合…

我发现OSGi有以下好处:

每个插件都是一个版本化的工件,它有自己的类加载器。 每个插件都依赖于它所包含的特定jar和其他特定版本的插件。 由于版本控制和隔离的类加载器,同一工件的不同版本可以同时加载。如果应用程序的一个组件依赖于插件的一个版本,而另一个组件依赖于另一个版本,那么它们可以同时加载。

这样,您就可以将应用程序构建为一组按需加载的版本化插件构件。每个插件都是一个独立的组件。就像Maven帮助您构建构建,使其可重复,并由创建它的工件的一组特定版本定义一样,OSGi帮助您在运行时完成这一点。

OSGi会让你的代码莫名其妙地抛出NoClassDefFoundError和ClassNotFoundException(很可能是因为你忘了在OSGi配置文件中导出一个包);因为它有类加载器,它可以使你的类com.example.Foo不能转换为com.example.Foo,因为它实际上是由两个不同的类加载器加载的两个不同的类。它可以在安装Eclipse插件后将Eclipse引导到OSGi控制台。

对我来说,OSGi只是增加了复杂性(因为它为我增加了一个心理模型),因为异常而增加了烦恼;我从未真正需要它“提供”的活力。它是侵入式的,因为它需要为所有模块配置OSGi包;这绝对不简单(在一个更大的项目中)。

因为我的糟糕经历,我倾向于远离那个怪物,非常感谢。我宁愿忍受jar依赖的地狱,因为这比OSGi引入的类加载器地狱更容易理解。