OSGI环境中单身人士的迁移解决方案 [英] Migration solution for singletons in an OSGI environment

查看:121
本文介绍了OSGI环境中单身人士的迁移解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java EE环境中工作,每个应用程序都在其自己的war文件中. 在每个应用程序war文件的WEB-INF/lib中,有一个公共jar可供所有应用程序共享. 这个公共的jar包含几个Singleton,可以从代码中的许多点进行访问. 由于战争文件的边界,每个应用程序都有其自己的Singleton实例. 由于我们要在每个应用程序中以不同的方式配置某些单例,因此我们今天的工作方式就是如此.

I'm working in a Java EE Environment in which each application is in a war file of its own. In the WEB-INF/lib of each application war file there is a common jar that is shared by all applications. This common jar contains several Singletons which are accessed from many points in the code. Because of the war-file boundaries each application has its own instances of the Singletons. Which is how we operate today, since we want to configure some of the singletons differently in each application.

现在我们正朝着OSGi环境迈进,在该环境下该解决方案将不再起作用,因为每个捆绑包都有自己的类加载器,因此,如果我尝试从捆绑包"appA.jar"访问驻留在捆绑包"common.jar"中的MySingleton "或从捆绑包"appB.jar"中获取相同的实例.

Now we are moving towards an OSGi environment, where this solution will no longer work since each bundle has its own class loader, so if I try to access MySingleton which resides in bundle "common.jar" from bundle "appA.jar" or from bundle "appB.jar" I will get the same instance.

请记住,我希望"每个捆绑包的一个单例的不同实例. (听起来很讽刺)

Remember I "want" a different instance of a singleton per bundle. (as ironic as it sounds)

现在,我意识到理想的解决方案是将代码固定为不依赖那些单例,但是由于时间紧迫,我想知道你们是否可以提出某种迁移解决方案,以允许我使用捆绑软件-宽的单身人士,所以他们每个人都可以 每个捆绑配置.

Now I realize the ideal solution would be to fix the code to not rely on those singletons, however due to a tight schedule I was wondering if you guys can suggest some sort of a migration solution that would allow me to use bundle-wide singletons so each of them could be configured per bundle.

推荐答案

您的单例将成为OSGi中的服务.

Your singleton will be a service in OSGi.

然后,您需要创建ManagedServiceFactory(例如,请参见此条款)负责注册此服务的不同实例;每个服务都将使用不同的属性进行注册(例如,application ="appA"和application ="appB")

Then you need to create a ManagedServiceFactory (see for instance this article) responsible of registering different instances of this service; each service will be registered with different properties (f.i. application="appA" and application="appB")

此后,您将可以从任何执行正常服务查找并指定正确属性的应用程序访问正确的服务.

After that, you will access the right service from any application doing a normal service lookup specifying the correct properties.

这篇关于OSGI环境中单身人士的迁移解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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