简体   繁体   English

嵌入式OSGi或应用程序包

[英]Embedded OSGi or Application Bundle

I've just spent the last two days reading up all the OSGi stuff I can get my hands on and I finally think I've got my head around it. 我刚刚花了两天的时间来阅读所有可以得到的OSGi东西,我终于认为我已经掌握了它。

I'm now trying to integrate it with an existing application for many reasons such as 3rd party plugins, automatic updates, not to mention that SOA just makes me happy. 我现在正在尝试将它与现有应用程序集成,原因有很多,例如第三方插件,自动更新,更不用说SOA让我感到高兴。

I now have a decision I'm struggling to make, which is weather 我现在有一个我正在努力做出的决定,那就是天气

  1. My entire application should become an OSGi bundle installed by default in the container; 我的整个应用程序应该成为默认安装在容器中的OSGi包; or 要么
  2. My application should launch an embedded OSGi container and interact with it for all the plugged services. 我的应用程序应该启动一个嵌入式OSGi容器,并与所有插件服务进行交互。

I'd prefer 1, as this lets me update the application easily and the architecture would be consistent. 我更喜欢1,因为这可以让我轻松更新应用程序,并且架构将是一致的。 Of course I expect to have to refactor the application into many smaller bundles. 当然,我希望必须将应用程序重构为许多较小的包。 However 2 makes things much easier in the short term, but will become awkward in the future. 然而,2在短期内使事情变得更容易,但将来会变得尴尬。

for option 1) you really don't want your whole application in one bundle - you would loose all the benefit from OSGi - but really that depend on the size of your application. 对于选项1)你真的不希望你的整个应用程序在一个包中 - 你将失去OSGi的所有好处 - 但实际上这取决于你的应用程序的大小。

It really depends where you want to run application and which task you want it to perform. 这实际上取决于您希望运行应用程序的位置以及您希望它执行的任务。 Also you probably want to have some kind of remoting to access the exposed services. 此外,您可能希望使用某种远程访问来访问公开的服务。

in option 1) you need to enable some kind of http/servlet bundle (there is a bridge that exists) in option 2) you application can run inside an application server so you don't have to worry about that. 在选项1)中,您需要启用某种http / servlet包(存在一个存在的桥),选项2)您的应用程序可以在应用程序服务器中运行,因此您不必担心这一点。

The first question you want to ask yourself is about the operational environment. 您想要问自己的第一个问题是操作环境。 Who is going to run the application? 谁将运行该应用程序? Do they need/want to be trained on OSGi? 他们是否需要/想要接受OSGi培训? Are they more comfortable with the J2EE stack? 他们对J2EE堆栈更熟悉吗?

I think the best option for you is to keep your options open, there is no real differences between 1) and 2) but what is staring the OSGi framework, either your code or the framework code. 我认为最好的选择是让你的选项保持开放,1)和2)之间没有真正的区别,但是什么是盯着OSGi框架,无论是你的代码还是框架代码。 Your application itself, ie the bundles constituting your application will be exactly the same. 您的应用程序本身,即构成您的应用程序的捆绑包将完全相同。

My advice would be not to worry too much about OSGi runtime to start with - but start on OSGi development - nothing stop you from developing "OSGi-style" and running in a standard JRE environment. 我的建议是不要过于担心OSGi运行时开始 - 但是从OSGi开发开始 - 没有什么能阻止你开发“OSGi风格”并在标准的JRE环境中运行。

I would rather go with option 2, Inherently your application is not a bundle, but an application. 我宁愿选择选项2,本质上你的应用程序不是一个包,而是一个应用程序。 If you want the OSGi value addition, spawn the OSGi container from within your application. 如果您想要添加OSGi值,请从您的应用程序中生成OSGi容器。 That way, at a future date if you decide to move away from OSGi, you can do in a simple way. 这样,如果您决定放弃OSGi,将来可以以简单的方式完成。

I think you want to go with option 1, and have your application consist of a set of bundles inside of an (mostly out-of-the-box) OSGi container. 我想你想要使用选项1,并让你的应用程序包含一组(大多数是开箱即用的)OSGi容器内的包。

  1. It will improve modularity of your own code. 它将改善您自己的代码的模块化。 You may even find that some parts of it can provide services of use outside of the original application. 您甚至可能会发现它的某些部分可以提供原始应用程序之外的使用服务。
  2. It is much easier to use other bundles from inside of OSGi, than from the host application. 从OSGi内部使用其他bundle比从宿主应用程序中使用要容易得多。 Because the host application cannot see the bundles' classes (and the bundles can only see what you explicitly expose from the host), you have to set up a pretty convoluted classpath or resort to reflection to call bundles from outside the container. 由于主机应用程序无法看到捆绑包的类(并且捆绑包只能看到您从主机明确公开的内容),因此您必须设置一个非常复杂的类路径或使用反射从容器外部调用捆绑包。

So I'd say that even in the short run, option 1 is probably easier. 所以我会说即使在短期内,选项1也可能更容易。

Also, I agree with Patrick's assertion that the bulk of your code does not need to care if it runs in OSGi or in a plain JVM. 此外,我同意Patrick的断言,即大部分代码不需要关心它是在OSGi中运行还是在普通JVM中运行。 Especially when using Declarative Services and such the need to use OSGi interfaces and mechanisms from your code is greatly reduced: You just add a few descriptor files to the jar's META-INF. 特别是在使用Declarative Services时,大大减少了代码中使用OSGi接口和机制的需求:你只需要在jar的META-INF中添加一些描述符文件。

I would definitely recommend 1 - the app should become an OSGi bundle(s), and not only because of easy updating. 我肯定会推荐1 - 该应用程序应该成为一个OSGi包,而不仅仅是因为易于更新。 If half of your code is in the OSGi framework and half is outside, you will have to construct a bridge for the communication between the two halves; 如果你的一半代码在OSGi框架中,而另一半在外面,你将不得不为两半之间的通信构建一个桥梁; you could also have issues with classes visibility. 您也可能遇到类可见性问题。

There are also many benefits from 1, and it is not so difficult to achieve. 1还有很多好处,实现起来并不困难。 What I would recommend is the following: 我建议的是以下内容:

  • Separate the application in as many modules as it seems logical to you. 将应用程序分成尽可能合理的模块。

You are not forced to have many modules - OSGi can as easily handle two bundles 10 MB each as well as 100 smaller bundles. 你没有被迫拥有很多模块 - OSGi可以轻松处理两个10 MB的捆绑包以及100个较小的捆绑包。 The separation should be a result of the functionality itself - a good starting point is the UML architecture diagram you probably did before you even started implementing the stuff. 分离应该是功能本身的结果 - 一个很好的起点是你甚至在开始实现这些东西之前可能做的UML架构图。 The places where the different functional parts communicate with each other are exactly the places where you should think about defining interfaces instead of classes - and these interfaces will then become your OSGi services and the implementations will become the bundles - and the next time you will have to update some part you will find out it is much easier to predict the effect on the other parts of the app because you separated it clearly and declared it in the manifest of the bundles. 不同功能部件相互通信的地方正是您应该考虑定义接口而不是类的地方 - 这些接口将成为您的OSGi服务,实现将成为捆绑 - 下次您将拥有更新某些部分你会发现预测应用程序其他部分的效果要容易得多,因为你清楚地将它分开并在捆绑包的清单中声明它。

  • Separate any external/open source libraries you use in separate bundles. 将您在单独的包中使用的任何外部/开源库分开。 They will most probably be the parts that will have to be updated more often and on a different timeline than your own code. 它们很可能是必须更频繁地更新的部分,并且在与您自己的代码不同的时间轴上。 It is also more important here to define clear package dependencies, package versions, and to avoid depending on the implementation parts instead of only on interfaces! 这里更重要的是定义明确的包依赖关系,包版本,并避免依赖于实现部分而不是仅依赖于接口!

  • Think about which parts of the app you want to expose to plugins. 考虑一下您希望向插件公开哪些应用程序部分。 Then make OSGi services out of these parts - ie publish the interfaces in the OSGi registry. 然后从这些部分中制作OSGi服务 - 即在OSGi注册表中发布接口。 You don't need to implement any specific thing - you can publish any Java object. 您不需要实现任何特定的东西 - 您可以发布任何Java对象。 The plugins will then use the regitry for the lookup. 然后,插件将使用regitry进行查找。

  • The same goes for the plugins - think about what you want to get from plugins and define the respective interfaces that the plugins can implement and publish and your app can lookup in the registry. 插件也是如此 - 想想你想从插件中得到什么,并定义插件可以实现和发布的各个接口,你的应用可以在注册表中查找。

  • And as a final tip - see what bundles are already available in the OSGi framework you have chosen. 作为最后的提示 - 看看你选择的OSGi框架中已经有哪些捆绑包。 There are many standard OSGi interfaces defined by the OSGi spec - for configuration, logging, persistent storage, remote connections, user admin, eventing, and many more. OSGi规范定义了许多标准的OSGi接口 - 用于配置,日志记录,持久存储,远程连接,用户管理,事件等等。 Since they are standard, you can use them without becoming dependent on any specific OSGi implementation. 由于它们是标准的,因此您可以使用它们而不依赖于任何特定的OSGi实现。 And uninstall what you don't need. 并卸载您不需要的内容。

Have you looked at the Spring Application server? 你看过Spring Application服务器了吗? Doesn't this allow you to manage this stuff? 这不是允许你管理这些东西吗?

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

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