简体   繁体   English

OSGI环境中单身人士的迁移解决方案

[英]Migration solution for singletons in an OSGI environment

I'm working in a Java EE Environment in which each application is in a war file of its own. 我正在Java EE环境中工作,每个应用程序都在其自己的war文件中。 In the WEB-INF/lib of each application war file there is a common jar that is shared by all applications. 在每个应用程序war文件的WEB-INF / lib中,有一个公共jar可供所有应用程序共享。 This common jar contains several Singletons which are accessed from many points in the code. 这个公共的jar包含几个Singleton,可以从代码中的许多点进行访问。 Because of the war-file boundaries each application has its own instances of the Singletons. 由于战争文件的边界,每个应用程序都有其自己的Singleton实例。 Which is how we operate today, since we want to configure some of the singletons differently in each application. 由于我们要在每个应用程序中以不同的方式配置某些单例,因此我们今天的工作方式就是如此。

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. 现在,我们正朝着OSGi环境迁移,该解决方案将不再起作用,因为每个捆绑软件都有自己的类加载器,因此,如果我尝试从捆绑软件“ appA.jar”或从中访问驻留在捆绑软件“ common.jar”中的MySingleton捆绑“ appB.jar”,我将获得相同的实例。

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. 现在,我意识到理想的解决方案是修复代码,使其不依赖那些单例,但是由于时间紧迫,我想知道你们是否可以建议某种迁移解决方案,从而允许我使用捆绑软件范围的单例。每个捆绑包都可以配置它们。

Your singleton will be a service in OSGi. 您的单例将成为OSGi中的服务。

Then you need to create a ManagedServiceFactory (see for instance this article ) responsible of registering different instances of this service; 然后,您需要创建一个ManagedServiceFactory(请参阅本文 ),负责注册该服务的不同实例; each service will be registered with different properties (fi application="appA" and application="appB") 每个服务将使用不同的属性进行注册(例如,application =“ appA”和application =“ appB”)

After that, you will access the right service from any application doing a normal service lookup specifying the correct properties. 之后,您将可以从任何执行正常服务查找并指定正确属性的应用程序访问正确的服务。

I can think of a few options: 我可以想到一些选择:

  1. Include a copy of all the classes in common.jar directly in the WAR bundle. 直接在WAR包中包含common.jar中所有类的副本。
  2. Nest common.jar in each WAR bundle, then modify the bundle class path in MANIFEST.MF to include the nested jar: Bundle-ClassPath: ., common.jar 在每个WAR包中嵌套common.jar,然后在MANIFEST.MF中修改包类路径以包括嵌套的jar:Bundle-ClassPath:。,common.jar
  3. Modify the singleton to use OSGi services, and use a ServiceFactory to ensure that each requesting bundle receives its own instance of that service. 修改单例以使用OSGi服务,并使用ServiceFactory来确保每个发出请求的捆绑包都收到其自己的服务实例。 You'll need to cache the service instance (don't get/use/unget) to avoid getting a new instance on each access. 您需要缓存服务实例(不要获取/使用/ unget),以避免在每次访问时获取新实例。

Singletons indeed map to services. 单身人士确实映射到服务。 If the applications (appA, appB) are actually bundles, then implement your service as a ServiceFactory. 如果应用程序(appA,appB)实际上是捆绑包,则将您的服务实现为ServiceFactory。 This will allow you to return a separate instance for every invoking bundle automatically. 这将允许您为每个调用包自动返回单独的实例。 That will be easier than a ManagedServiceFactory (which needs explicit configuration for each instance) or hacking getters. 这将比ManagedServiceFactory(每个实例都需要显式配置)或黑客获取程序容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM