Java中可动态加载和卸载的应用程序模块-怎么样? [英] Dynamically loadable and unloadable application modules in Java - how?

查看:157
本文介绍了Java中可动态加载和卸载的应用程序模块-怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用外部模块的服务器应用程序.我想使它们可升级,而无需重新启动服务器.我怎么做?我找到了 OSGi ,但是对于我的任务而言,它看起来非常复杂而且庞大.

I'm writing a server application which makes use of external modules. I would like to make them to be upgradeable without requiring server restart. How do I do that? I've found OSGi but it looks very complicated and big for my task.

简单的* .jar文件是可以的,但是我想一旦加载它们,就无法从VM卸载它们并即时替换为另一个版本.

Simple *.jar files are ok, but once they are loaded, I suppose, I cannot unload them from VM and replace with another version on-the-fly.

您可以建议哪种方法?

推荐答案

似乎OSGi正是您要的东西.它可能很复杂,但是有多种方法可以解决.通过使用SpringDM或类似的东西可以在运行时中处理注册和使用服务的样板任务,可以减轻某些复杂性.注释驱动的服务注册和依赖注入确实减少了需要编写的代码量.

It seems like OSGi is exactly what you're asking for. It can be complex, but there are ways to deal with that. Some of the complexity can be mitigated by using SpringDM or something similar to handle the boilerplate tasks of registering and consuming services in the runtime. Annotation-driven service registration and dependency injection really reduces the amount of code that needs to be written.

降低复杂性的另一种方法是将应用程序的大部分部署在单个捆绑包中,而仅将需要模块化的部分部署到自己的捆绑包中.这减少了您在运行时注册和使用其他捆绑软件中的服务的风险,并降低了部署的复杂性.与标准Java应用程序一样,在捆绑包中运行的代码可以使用同一捆绑包中的其他代码-无需与OSGi运行时进行交互.这种方法的反义是将您的应用程序分解为许多离散的捆绑包,这些捆绑包将定义明确的服务导出到系统中的其他捆绑包.尽管这是一种非常模块化的方法,但确实带来了额外的管理所有捆绑软件的复杂性以及与OSGi运行时的更多交互.

Another way to reduce complexity is to deploy the bulk of your application in a single bundle and only deploy the parts that need to be modular into their own bundles. This reduces your exposure to registering and using services from other bundles in the runtime as well as reducing the complexity of deployment. Code running within a bundle can use other code in the same bundle just as in a standard Java app - no need to interact with the OSGi runtime. The opposite of this approach is to break up your application into lots of discrete bundles that export well-defined services to other bundles in the system. While this is a very modular approach, it does come with extra complexity of managing all those bundles and more interaction with the OSGi runtime.

我建议您阅读《 OSGi in Action》一书,以了解这些问题并查看一些不错的示例.

I would suggest taking a look at the book "OSGi in Action" to get a sense of the issues and to see some decent samples.

这篇关于Java中可动态加载和卸载的应用程序模块-怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆