OSGi、 Java 模块化与 Jigsaw

所以到昨天早上为止,我都不知道 OSGi 到底是什么。OSGi只是一个我反复看到的流行词,所以我终于腾出一些时间来温习它。

实际上它看起来很酷,所以我想首先声明(郑重声明)我在任何方面都不反对 OSGi,这也不是什么“抨击 OSGi”的问题。

归根结底,OSGi 似乎已经基本上解决了 Java 模块化的 JSR 277问题,它认识到 JAR文件规范存在缺陷,在某些角落情况下可能导致名称空间解析和类加载问题。OSGi 还做了很多其他很酷的事情,但是据我所知,这是它最大的吸引力(或者说是其中之一)。

对我来说——作为一个相当新的(几年前) Java EE 开发者,这绝对是令人难以置信的,我们在2011年,目前生活在 Java 7的时代,这些类加载问题仍然存在; 特别是在企业环境中,一个应用服务器可能有数百个 JAR,其中许多取决于不同版本的另一个和所有运行(或多或少)并发。

我的问题是:

尽管我对 OSGi 很感兴趣,尽管我很想开始学习它,看看它在哪里/是否对我的项目有用,但我没有时间坐下来学习那么大的东西,至少现在是这样。

那么,当这些问题出现时,非 OSGi 开发人员应该怎么做呢?目前有哪些 爪哇咖啡(Oracle/Sun/JCP)解决方案(如果有的话) ?为什么电锯杀人狂会从 J7身上割下来?社区对 Jigsaw 明年在 J8中实现有多大把握?尽管 Jigsaw 还不是 Java 平台的一部分,但是否有可能为您的项目获得 Jigsaw?

我想我现在要问的是恐慌,阴谋,还有手掌的组合。现在我终于明白了什么是 OSGi,我只是不“明白”像 Jigsaw 这样的东西是如何花了20多年才实现的,以及它是如何从一个发布版本中被封装起来的。这是最基本的。

而且,作为一个开发人员,我也很好奇我的解决方案是什么,没有 OSGi。

另外,注意: 我知道这不是一个“ 纯粹的编程”类型的问题,但是在你们中的一些人把你们的鼻子弄歪之前,我想声明(再次声明) ,我故意把这个问题放在 SO 上。那是因为我对我的 SOers 同伴们只有最大的尊重,而且我正在从我每天看到的潜伏在这里的一些“ IT 之神”那里寻找一个架构级别的答案。

但是,对于那些绝对 坚持的人来说,这个问题需要一些代码段来支持:

int x = 9;

(感谢任何可以在这个 OSGi/Jigsaw/classloader/name space/JAR 鬼东西上权衡利弊的人!)

10208 次浏览

It's simple, if you want to do real component-based development in Java today then OSGi is the only game in town.

In my opinion, Jigsaw is a combination of a compromise of what's doable in the JDK and previous bad relationship between SUN and the OSGi guys. Maybe it will ship with Java 8, but we have to wait and see.

OSGi is no panacea if you are working in a typical enterprise setting and you will need to become familiar with how class loading works as a number of well-known libraries (looking at you, Hibernate) made assumptions about class visibility that are no longer valid inside OSGi.

I like OSGi, but I wouldn't try and retrofit it to an existing system. I'd also weigh the pros and cons in terms of greenfield development - I would recommend looking at the Apache or Eclipse products that simplify life for OSGi and not doing it all yourself.

If you're not doing OSGi, then your out of luck if you've come up with a system that has dependencies on different versions of the same library - all you can do is try and avoid the problem, though needing multiple versions of a library seems like a architecture 'smell' to me.

First understand that Jigsaw's primary use case is to modularise the JRE itself. As a secondary goal it will offer a module system that may be used by other Java libraries and applications.

My position is that something like Jigsaw is probably necessary for the JRE only, but that it will create far more problems than it claims to solve if used by other Java libraries or apps.

The JRE is a very difficult and special case. It is over 12 years old and is a frightful mess, riddled with dependency cycles and nonsensical dependencies. At the same time is is used by approximately 9 million developers and probably billions of running systems. Therefore you absolutely cannot refactor the JRE if that refactoring creates breaking changes.

OSGi is a module system that helps you (or even forces you to) create software that is modular. You cannot simply sprinkle modularity on top of an existing non-modular codebase. Making a non-modular codebase into a modular one inevitably requires some refactoring: moving classes into the correct package, replacing direct instantiation with the use of decoupled services, and so on.

This makes it hard to apply OSGi directly to the JRE codebase, yet we still have a requirement to split the JRE into separate pieces or "modules" so that cut-down versions of the JRE can be delivered.

I therefore regard Jigsaw as a kind of "extreme measure" to keep the JRE code alive while splitting it up. It does not help code to become more modular, and I'm convinced that it will actually increase the maintenance required to evolve any library or application that uses it.

Finally: OSGi exists whereas Jigsaw does not exist yet and may never exist. The OSGi community has 12 years of experience in developing modular applications. If you are seriously interested in developing modular applications, OSGi is the only game in town.

I love your use of the phrase "corner cases" to describe the current situation.

there are shortcomings with the JAR file specification that can lead to namespace resolution and classloading issues in certain corner cases

Anyhow, since many years I've been interested in tools and techniques that supports the creation of and, even better, enforce, code that is cleaner, more decoupled, more coherent and more maintainable than what probably would have been the result without them. Test Driven Design and Junit was such a combination.

After having spent a couple of months moving a substantial part of our code base to OSGi, I would say that OSGi is an even better tool in this regard. And that is really a sufficient reason to move to OSGi. In the long run it will save you a lot of money.

And, as a bonus, it'll give you a chance to do a lot of cool stuff. Imagine, during a demo, seamlessly upgrading the authentication module, without traffic loss, to support OAuth ... it's suddenly a joy again to create stuff!