What does OSGi solve?
Asked Answered
S

16

296

I've read on Wikipedia and other sites about OSGi, but I don't really see the big picture. It says that it's a component-based platform, and that you can reload modules at runtime. Also the "practical example" given everywhere is the Eclipse Plugin Framework.

My questions are:

  1. What is the clear and simple definition of OSGi?

  2. What common problems does it solve?

By "common problems" I mean problems we face everyday, like "What can OSGi do for making our jobs more efficient/fun/simple?"

Siloum answered 19/9, 2008 at 22:46 Comment(0)
P
103

what benefits does OSGi's component system provide you?
Well, Here is quite a list:

Reduced Complexity - Developing with OSGi technology means developing bundles: the OSGi components. Bundles are modules. They hide their internals from other bundles and communicate through well defined services. Hiding internals means more freedom to change later. This not only reduces the number of bugs, it also makes bundles simpler to develop because correctly sized bundles implement a piece of functionality through well defined interfaces. There is an interesting blog that describes what OSGi technology did for their development process.

Reuse - The OSGi component model makes it very easy to use many third party components in an application. An increasing number of open source projects provide their JARs ready made for OSGi. However, commercial libraries are also becoming available as ready made bundles.

Real World - The OSGi framework is dynamic. It can update bundles on the fly and services can come and go. Developers used to more traditional Java see this as a very problematic feature and fail to see the advantage. However, it turns out that the real world is highly dynamic and having dynamic services that can come and go makes the services a perfect match for many real world scenarios. For example, a service could model a device in the network. If the device is detected, the service is registered. If the device goes away, the service is unregistered. There are a surprising number of real world scenarios that match this dynamic service model. Applications can therefore reuse the powerful primitives of the service registry (register, get, list with an expressive filter language, and waiting for services to appear and disappear) in their own domain. This not only saves writing code, it also provides global visibility, debugging tools, and more functionality than would have implemented for a dedicated solution. Writing code in such a dynamic environment sounds like a nightmare, but fortunately, there are support classes and frameworks that take most, if not all, of the pain out of it.

Easy Deployment - The OSGi technology is not just a standard for components. It also specifies how components are installed and managed. This API has been used by many bundles to provide a management agent. This management agent can be as simple as a command shell, a TR-69 management protocol driver, OMA DM protocol driver, a cloud computing interface for Amazon's EC2, or an IBM Tivoli management system. The standardized management API makes it very easy to integrate OSGi technology in existing and future systems.

Dynamic Updates - The OSGi component model is a dynamic model. Bundles can be installed, started, stopped, updated, and uninstalled without bringing down the whole system. Many Java developers do not believe this can be done reliably and therefore initially do not use this in production. However, after using this in development for some time, most start to realize that it actually works and significantly reduces deployment times.

Adaptive - The OSGi component model is designed from the ground up to allow the mixing and matching of components. This requires that the dependencies of components need to be specified and it requires components to live in an environment where their optional dependencies are not always available. The OSGi service registry is a dynamic registry where bundles can register, get, and listen to services. This dynamic service model allows bundles to find out what capabilities are available on the system and adapt the functionality they can provide. This makes code more flexible and resilient to changes.

Transparency - Bundles and services are first class citizens in the OSGi environment. The management API provides access to the internal state of a bundle as well as how it is connected to other bundles. For example, most frameworks provide a command shell that shows this internal state. Parts of the applications can be stopped to debug a certain problem, or diagnostic bundles can be brought in. Instead of staring at millions of lines of logging output and long reboot times, OSGi applications can often be debugged with a live command shell.

Versioning - OSGi technology solves JAR hell. JAR hell is the problem that library A works with library B;version=2, but library C can only work with B;version=3. In standard Java, you're out of luck. In the OSGi environment, all bundles are carefully versioned and only bundles that can collaborate are wired together in the same class space. This allows both bundle A and C to function with their own library. Though it is not advised to design systems with this versioning issue, it can be a life saver in some cases.

Simple - The OSGi API is surprisingly simple. The core API is only one package and less than 30 classes/interfaces. This core API is sufficient to write bundles, install them, start, stop, update, and uninstall them and includes all listener and security classes. There are very few APIs that provide so much functionality for so little API.

Small - The OSGi Release 4 Framework can be implemented in about a 300KB JAR file. This is a small overhead for the amount of functionality that is added to an application by including OSGi. OSGi therefore runs on a large range of devices: from very small, to small, to mainframes. It only asks for a minimal Java VM to run and adds very little on top of it.

Fast - One of the primary responsibilities of the OSGi framework is loading the classes from bundles. In traditional Java, the JARs are completely visible and placed on a linear list. Searching a class requires searching through this (often very long, 150 is not uncommon) list. In contrast, OSGi pre-wires bundles and knows for each bundle exactly which bundle provides the class. This lack of searching is a significant speed up factor at startup.

Lazy - Lazy in software is good and the OSGi technology has many mechanisms in place to do things only when they are really needed. For example, bundles can be started eagerly, but they can also be configured to only start when other bundles are using them. Services can be registered, but only created when they are used. The specifications have been optimized several times to allow for these kind of lazy scenarios that can save tremendous runtime costs.

Secure - Java has a very powerful fine grained security model at the bottom but it has turned out very hard to configure in practice. The result is that most secure Java applications are running with a binary choice: no security or very limited capabilities. The OSGi security model leverages the fine grained security model but improves the usability (as well as hardening the original model) by having the bundle developer specify the requested security details in an easily audited form while the operator of the environment remains fully in charge. Overall, OSGi likely provides one of the most secure application environments that is still usable short of hardware protected computing platforms.

Non Intrusive - Applications (bundles) in an OSGi environment are left to their own. They can use virtually any facility of the VM without the OSGi restricting them. Best practice in OSGi is to write Plain Old Java Objects and for this reason, there is no special interface required for OSGi services, even a Java String object can act as an OSGi service. This strategy makes application code easier to port to another environment.

Runs Everywhere - Well, that depends. The original goal of Java was to run anywhere. Obviously, it is not possible to run all code everywhere because the capabilities of the Java VMs differ. A VM in a mobile phone will likely not support the same libraries as an IBM mainframe running a banking application. There are two issue to take care of. First, the OSGi APIs should not use classes that are not available on all environments. Second, a bundle should not start if it contains code that is not available in the execution environment. Both of these issues have been taken care of in the OSGi specifications.

Source : www.osgi.org/Technology/WhyOSGi

Pilotage answered 19/9, 2008 at 22:46 Comment(5)
Seems to me like one might achieve at least some these benefits by simply utilizing a SOA (stateless/stateful). When I deploy a patched/updated version of a component to a different (versioned) end-point then I can simply have the dependent component, switch and utilize the patched service at the new end-point. Since I can have both the old version and the new version deployed and running at the same time, I can also have the dependent component use different parts of both the old version and the new version as needed.Calamus
Looks like people are going through a hell of a lot of trouble to do microservices and SOA to (hopefully) get 20% more functionality than OSGi. Companies should think twice how much OSGi gives for so little extra work. All services on the same JVM in a single process, where multiple services can be taken offline or upgraded as required.Berghoff
OSGi bundles might just be the closest abstraction to the elusive 'component' that people have been looking for for ages!Berghoff
SOA and microservices can provide some of the benefits but at a much larger cost. In both cases all communication happens over the network which is very expensive compared to local calls. Managing versions in SOA and microservices is also quite a nightmare. Calling an OSGi service in comparison is as cheap as any java method call.Tresatrescha
this answer looks like total marketing talk: it's very generic, high level, long and bloated and mentions "benefits" that are much easier to achieve with other means, instead of shortly focusing on the real advantages of OSGI like the other highest rated answers do.Debase
U
93

I've found the following benefits from OSGi:

  • Each plugin is a versioned artifact that has its own classloader.
  • Each plugin depends on both specific jars that it contains and also other specific versioned plug-ins.
  • Because of the versioning and isolated classloaders, different versions of the same artifact can be loaded at the same time. If one component of your application relies on one version of a plug-in and another depends on another version, they both can be loaded at the same time.

With this, you can structure your application as a set of versioned plugin artifacts that are loaded on demand. Each plugin is a standalone component. Just as Maven helps you structure your build so it is repeatable and defined by a set of specific versions of artifacts it is created by, OSGi helps you do this at runtime.

Unloosen answered 19/9, 2008 at 23:16 Comment(1)
One of the biggest benefits of this strong versioning mechanism is that dependencies are verified during deployment, meaning that you get unresolved dependency errors at deployment time instead of NoClassDefFoundErrors at run time. Aside from the module system, the OSGi service layer does deserve mention. It's not as easy to start with because it impacts your architecture (and it's not always appropriate to use it), but it's a very powerful system once you have adopted it.Kaufman
P
61

I don't care too much about the hotplugability of OSGi modules (at least currently). It's more the enforced modularity. Not having millions of "public" classes available on the classpath at any time protects well from circular dependencies: You have to really think about your public interfaces - not just in terms of the java language construct "public", but in terms of your library/module: What (exactly) are the components, that you want to make available for others? What (exactly) are the interfaces (of other modules) you really need to implement your functionality?

It's nice, that hotplug comes with it, but I'd rather restart my usual applications than testing all combinations of hotplugability...

Peddle answered 20/9, 2008 at 16:43 Comment(1)
You can achieve the same using Guice and packages, exporting only interfaces and Modules and marking everything else package-privateSiloum
T
22
  • You can, analogically speaking, change the motor of your car without turning it off.
  • You can customize complex systems for the customers. See the power of Eclipse.
  • You can reuse entire components. Better than just objects.
  • You use a stable platform to develop component based Applications. The benefits of this are huge.
  • You can build Components with the black box concept. Other components don't need to know about hidden interfaces, them see just the published interfaces.
  • You can use in the same system several equal components, but in different releases, without compromise the application. OSGi solves the Jar Hell problem.
  • With OSGi you develop thinking to architect systems with CBD

There are a lot of benefits (I reminded just these now), available for everyone who uses Java.

Taunt answered 19/9, 2008 at 22:46 Comment(0)
C
15

edited for clarity. OSGi page gave a better simple answer than mine

A simple answer: An OSGi Service Platform provides a standardized, component-oriented computing environment for cooperating networked services. This architecture significantly reduces the overall complexity of building, maintaining and deploying applications. The OSGi Service Platform provides the functions to change the composition dynamically on the device of a variety of networks, without requiring a restarts.

In a single application structure, say the Eclipse IDE, it's not a big deal to restart when you install a new plugin. Using the OSGi implementation completely, you should be able to add plugins at runtime, get the new functionality, but not have to restart eclipse at all.

Again, not a big deal for every day, small application use.

But, when you start to look at multi-computer, distributed application frameworks, that's where it starts to get interesting. When you have to have 100% uptime for critical systems, the capability to hotswap components or add new functionality at runtime is useful. Granted, there are capabilities for doing this now for the most part, but OSGi is trying to bundle everything into a nice little framework with common interfaces.

Does OSGi solve common problems, I'm not sure about that. I mean, it can, but the overhead may not be worth it for simpler problems. But it's something to consider when you are starting to deal with larger, networked, applications.

Cawthon answered 19/9, 2008 at 22:55 Comment(3)
Are you saying that OSGi provides an inter-JVM mechanism for service discovery in a distributed environment? My own question (#376225) has been answered as if OSGi is single-VMGlim
What do you mean by "networks" in "change the composition dynamically on the device of a variety of networks"?Argueta
Aren't microservices solving the similar problems?Stainless
S
13

A Few Things that drive me nuts on OSGi:

1) The implentations and their context loaders have a lot of quirks to them, and can be somewhat async (We use felix inside of confluence). Compared to a pure spring (no DM) where [main] is pretty much running through everything sync.

2)Classes are not equal after a hot load. Say, for instance you have a tangosol cache layer on hibernate. It is filled with Fork.class, outside of the OSGi scope. You hotload a new jar, and Fork has not changed. Class[Fork] != Class[Fork]. It also appears during serialization, for the same underlying causes.

3)Clustering.

You can work around these things, but it is a major major pain, and makes your architecture look flawed.

And to those of you advertising the hotplugging.. OSGi's #1 Client? Eclipse. What does Eclipse do after loading the bundle?

It restarts.

Simony answered 19/9, 2008 at 22:46 Comment(1)
Don't forget to mention that Eclipse may not even boot since the OSGi dependency graph got broken by that new bundle.Zwick
Z
9

OSGi makes your code throw NoClassDefFoundError and ClassNotFoundException for no apparent reason (most probably because you forgot to export a package in OSGi configuration file); since it has ClassLoaders it can make your class com.example.Foo fail to be cast to com.example.Foo since it's actually two different classes loaded by two different classloaders. It can make your Eclipse boot into an OSGi console after installing an Eclipse plugin.

For me, OSGi only added complexity (because it added one more mental model for me to grok), added annoyances because of exceptions; I never really needed the dynamicity it "offers". It was intrusive since it required OSGi bundle configuration for all modules; it was definitely not simple (in a larger project).

Because of my bad experience, I tend to stay away from that monster, thank you very much. I'd rather suffer from jar dependency hell, since that's way way more easily understandable than the classloader hell OSGi introduces.

Zwick answered 19/9, 2008 at 22:46 Comment(0)
E
6

If a Java based application requires adding or removing modules (extending the base functionality of application), without shutting down the JVM, OSGI can be employed. Usually if the cost of shutting down JVM is more, just to update or to enhance functionality.

Examples:

  1. Eclipse: Provides platform for plugins to install, uninstall, update and inter-depend.
  2. AEM: WCM application, where functionality change will be business driven, which can not afford down times for maintenance.

Note: Spring framework stopped supporting OSGI spring bundles, considering it as unnecessary complexity for transaction based applications or for some point in these lines. I personally do not consider OSGI unless it is absolutely necessary, in something big like building a platform.

Ezequieleziechiele answered 19/9, 2008 at 22:46 Comment(0)
D
5

I've been doing work with OSGi almost 8 or so years and I have to say that you should consider OSGi only if you have a business need to update, remove, install or replace a component on runtime. This also means that you should have a modular mindset and understanding what modularity means. There's some arguments that OSGi is lightweight - yes, that is true but there are also some other frameworks that are lightweight and easier to maintain and develop. Same goes to secure java blah blah.

OSGi requires a solid architecture to be used correctly and it's quite easy to make OSGi-system that could just as easily be a standalone-runnable-jar without any OSGi being involved.

Diddle answered 19/9, 2008 at 22:46 Comment(0)
T
5

I am yet to be a "fan" of OSGi...

I have been working with an enterprise application at Fortune 100 companies. Recently, the product we use has "upgraded" to an OSGi implementation.

starting local cba deployment... [2/18/14 8:47:23:727 EST] 00000347 CheckForOasis

finally deployed and "the following bundles will be quiesced and then restarted" [2/18/14 9:38:33:108 EST] 00000143 AriesApplicat I CWSAI0054I: As part of an update operation for application

51 minutes... each time code changes... The previous version (non-OSGi) would deploy in less than 5 minutes on older development machines.

on a machine with 16 gig ram and 40 free gig disk and Intel i5-3437U 1.9 GHz CPU

The "benefit" of this upgrade was sold as improving (production) deployments - an activity that we do about 4 times a year with maybe 2-4 small fix deployments a year. Adding 45 minutes per day to 15 people (QA and developers) I can't imagine ever being justified. In big enterprise applications, if your application is a core application, then changing it is, rightly so (small changes have potential for far reaching impacts - must be communicated and planned with consumers all over the enterprise), a monumental activity - wrong architecture for OSGi. If your application is not an enterprise application - i.e. each consumer can have their own tailored module likely hitting their own silo of data in their own silo'd database and running on a server that hosts many applications, then maybe look at OSGi. At least, that is my experience thus far.

Thickhead answered 19/9, 2008 at 22:46 Comment(2)
OSGi cannot cause a deployment to go from 5 to 51 minutes since it has virtually no overhead. This increase in startup time must be caused by something else, or, by serializing initialization by making activators initialize synchronously. It is not the fault of OSGi because in general people get a lower startup time.Jolly
Interesting reply. I'm just reading about OSGi now... yeah late comer. But my concern is about data in an enterprise context. Similar issue I have with microservices.Fridlund
A
2

Others have already outlined the benefits in detail, I hereby explain the practical usecases I have either seen or used OSGi.

  1. In one of our application, we have event based flow and flow is defined in plugins based on OSGi platform so tomorrow if some client wants different/additional flow then he just have to deploy one more plugin, configure it from our console and he is done.
  2. It is used for deploying different Store connectors, for example, suppose we already have Oracle DB connector and tomorrow mongodb is required to be connected then write a new connector and deploy it and configure the details through console and again you are done. deployment of connnectors is handled by OSGi plugin framework.
Atomic answered 19/9, 2008 at 22:46 Comment(0)
D
2

The OSGi provides following benefit:

■ A portable and secure execution environment based on Java

■ A service management system, which can be used to register and share services across bundles and decouple service providers from service consumers

■ A dynamic module system, which can be used to dynamically install and uninstall Java modules, which OSGi calls bundles

■ A lightweight and scalable solution

Dispensary answered 19/9, 2008 at 22:46 Comment(0)
F
1

There is already a quite convincing statement in its official site, I may quote as

The key reason OSGi technology is so successful is that it provides a very mature component system that actually works in a surprising number of environments. The OSGi component system is actually used to build highly complex applications like IDEs (Eclipse), application servers (GlassFish, IBM Websphere, Oracle/BEA Weblogic, Jonas, JBoss), application frameworks (Spring, Guice), industrial automation, residential gateways, phones, and so much more.

As for the benefits to developer?

DEVELOPERS: OSGi reduces complexity by providing a modular architecture for today’s large-scale distributed systems as well as small, embedded applications. Building systems from in-house and off-the-shelf modules significantly reduces complexity and thus development and maintenance expenses. The OSGi programming model realizes the promise of component-based systems.

Please check the details in Benefits of Using OSGi.

Flier answered 19/9, 2008 at 22:46 Comment(0)
C
1

At the very least, OSGi makes you THINK about modularity, code reuse, versioning and in general the plumbing of a project.

Conspectus answered 19/9, 2008 at 22:46 Comment(1)
It doesn't make you think about that, it can just confuse you, it's a lie that it solves all that problems (only you can solve them), it just brings dependency hell when your project is bigger than ~50 plugins, and usually you need to do a lot of classloader tricks. So your code is a lot more messy, because you need to do some osgi hacking and all devs in your team need to understand those hacks to understand the code. This influence is so big, that it affects your architecture in a way that you do very bad things to your code, It is a hell.Ultrasonic
D
1

It is also being used to bring additional portability of middleware and applications on the mobile side. Mobile side is available for WinMo, Symbian, Android for example. As soon as integration with device features occurs, can get fragmented.

Donniedonnish answered 30/4, 2009 at 20:38 Comment(0)
C
0

While I think the post by Abhishek Goel is right on the money, may I present a simpler response? OSGi lets you add units of functionality (ie plugins) to your awesome software with out a lot of the headaches that are normally involved. Some examples:

  • I want use version X of a really cool library in my awesome program and the plugin writer writer wants to use incompatible version Y (just because).
  • I want to just drop a jar file in some directory and 'it gets discovered, installed and it works'
  • I don't want a complex installation process for my plugin.
  • I want the plugin mechanism for my awesome program to be not real hard to implement in my code.
  • I don't want a plugin mechanism to double the size of my awesome program.
  • I have some library code all plugins need and I would realy like the plugin code to be limited to using my exposed API and I dont want the plugins to be able to screw around with other code that might be otherwise exposed.

OSGi address these issues and more. It can help you create extensions that are not all that hard to write, keeps you [mostly] out of 'classloader hell' and once you get the hang of it its a great help. just to let you know, there IS a learning curve involved - dont expect to master OSGi in a week, its been my experience that breaking up code on to modular units seems to help a lot, I dont have to recompile the whole app, just small parts and drop it in to the bundles directory and we're ready to rock and roll. I've been using OSGi a LOT for just about 2 months now and I think I have it mostly figured out. I think its time well spent.

Credo answered 19/9, 2008 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.