OSGi and …? What do I use to controll loading/unlo

2019-08-03 08:04发布

I have a large ecosystem of applications and libraries which are currently deployed as a collection of .jars in various application servers (e.g. JBoss AS), and I'm trying to figure out a good set of tools to manage dependencies and life-cycles of the various packages.

I think of the packages as being in one of (at least) three possible states: "unloaded", "pending" and "loaded", loosely defined as follows:

  • Unloaded: The package is not available at the moment.
  • Pending: The package itself is available, but not all its dependencies. Therefore, it cannot be used at the moment.
  • Loaded: The package is available and has all its dependencies satisfied. If it's an application, it can run - if it's a library, it is ready to be used by another package.

(There might also be a few more states, such as "failed" for packages that tried to load but failed for some other reason than that dependencies were not satisfied, etc...)

In the life-cycle of a package, a number of things might cause a package to change state between these three:

  • A package with no dependencies is loaded, and goes from unloaded to loaded.
  • A package tries to load, but not all dependencies are satisfied; it goes from unloaded to pending.
  • A package in pending state suddenly has all its dependencies satisfied (because some other package went to state loaded), and automatically starts loading itself; transition from pending to loaded.
  • A package is unloaded. All loaded packages which depend on the now unloaded package go from loaded to pending.
  • A package is updated to a newer version. All dependent packages are automatically reloaded, to get access to the updated version.

I've started working on using OSGi for defining the dependencies - it rolls well with our build system and produces reliable dependency information. However, if I load the two OSGi bundles A and B into JBoss, where B depends on A, and then unload A, it seems like B happily keeps running. I've understood that there are some hooks that I could use to control this on a low level (framework events), but my spider sense is tingling, saying that there must be a better way to do this.

Is there a nice tool/framework/whatever-you-want-to-call-it that will compliment OSGi in these aspects?

标签: java osgi
2条回答
来,给爷笑一个
2楼-- · 2019-08-03 08:36

In OSGi, existing class loaders will remain active until you refresh the framework. So If you unload B (where A depends on B) then A will continue churning happy along untile you refresh. You can refresh the whole framework or only the bundles affected by a given bundle (e.g. refresh B).

The purpose of the refresh is to update/uninstall/install a set of bundles and then apply the changes in an atomic operation.

查看更多
smile是对你的礼貌
3楼-- · 2019-08-03 08:37

If you declare dependancy between modules, the lifecycle is properly managed and the descendant depandancies must be stopped before the top-level module is stopped.

However, stopping a module does nothing more than sending events to the bundle activator and removing references from classloader. Any activities (such as thread or distributed instances) must be manually clean. For example, you must call org.apache.commons.logging.LogFactory.release(ClassLoader) (if using commons logging) or remove any injected UI-component.

查看更多
登录 后发表回答