27

I saw lots of presentations on OSGi and i think it sounds promising for enforcing better modularization. Apparently "hotdeployment" and "running different versions of x in parallel" are mayor selling points too.

I wonder whether what OSGi promises to solve is even an issue...? It reminded me of the early days of OO when similar claims were maid:

When OO was new, the big argument was reusability. It was widely claimed that when using OO, one would only have to "write once" and could then "use everywhere".

In practice I only saw this working for some pretty low level examples. I think the reason for this is that writing reusable code is hard. Not technically but from a interface design point of view. You have to anticipate how future clients will want to use your classes and take the right choices up front. This is difficult by definition and thus the potential reusability benefit often failed to deliver.

With OSGi, I have the suspicion that here again we could fall for promises, potential solutions for problems that we don't really have. Or if we have them, we don't have them in a big enough quantity and severity that would justify to buy into OSGi for help. "Hotdeployment" for example of a subset of modules is definitely a great idea, but how often does it really work? How often not because it turned out you got the modularization wrong for the particular issue? How about model entities that are shared between multiple modules? Do these modules all have to be changed at the same time? Or do you flatten your objects to primitives and use only those in inter-module communication, in order to be able to keep interface contracts?

The hardest problem when applying OSGi is, I would presume, to get the modularization "right". Similar to getting the interfaces of your classes right in OO, with OSGi, the problem stays the same, on a bigger scale this time, the package or even service level.

As you might have guessed, I'm currently trying to evaluate OSGi for use in a project. The major problem we have, is increasing complexity as the codebase grows and I would like to break the system up in smaller modules that have less and more defined interactions.

  • Given no framework can ever help deciding what to modularize, has OSGi ever payed off for you?
  • Has it made your life easier when working in teams?
  • Has it helped to reduce bug count?
  • Do you ever successfully "hotdeploy" major components?
  • Does OSGi help to reduce complexity over time?
  • Did OSGi keep its promises?
  • Did it fulfill your expectations?

Thanks!

raoulsson
  • 14,978
  • 11
  • 44
  • 68

6 Answers6

21

OSGi pays off because it enforces modularization at runtime, something you previously did not have, often causing the design on paper and implementation to drift apart. This can be a big win during development.

It definitely helps make it easier to work in teams, if you let teams focus on a single module (possibly a set of bundles), and if you get your modularization right. One could argue that one can do the same thing with a build tool like Ant+Ivy or Maven and dependencies, the granularity of dependencies that OSGi uses is in my opinion far superior, not causing the typical "dragging in everything plus the kitchen sink" that JAR level dependencies cause.

Modular code with less dependencies tends to lead to cleaner and less code, in turn leading to less bugs that are easier to test for and solve. It also promotes designing components as simple and straightforward as possible, whilst at the same time having the option to plug in more complicated implementations, or adding aspects such as caching as separate components.

Hot deployment, even if you do not use it at runtime, is a very good test to validate if you modularized your application correctly. If you cannot start your bundles in a random order at all, you should investigate why. Also, it can make your development cycle a lot quicker if you can update an arbitrary bundle.

As long as you can manage your modules and dependencies, big projects stay manageable and can be easily evolved (saving you from the arguably bad "complete rewrite").

The downside of OSGi? It's a very low-level framework, and whilst it solves the problems it is intended for quite well, there are things that you still need to resolve yourself. Especially if you come from a Java EE environment, where you get free thread-safety and some other concepts that can be quite useful if you need them, you need to come up with solutions for these in OSGi yourself.

A common pitfall is to not use abstractions on top of OSGi to make this easier for the average developer. Never ever let them mess with ServiceListeners or ServiceTrackers manually. Carefully consider what bundles are and are not allowed to do: Are you willing to give developers access to the BundleContext or do you hide all of this from them by using some form of declarative model.

Marcel Offermans
  • 3,313
  • 1
  • 15
  • 23
  • 2
    Why is there such a deficit in OSGi knowledge on SO? You seem to be one of the few people on here that sufficiently answers OSGi related questions. – javamonkey79 Dec 09 '10 at 20:44
  • Thanks for your kind words. There are quite a few experienced OSGi developers here, but I agree there should probably be even more. Keep the questions coming though, SO is a great site to get and find answers! – Marcel Offermans Dec 09 '10 at 21:46
9

I've worked with OSGi for some years now (although in the context of an eclipse project, not in a web project). It is clear that the framework does not free you from thinking how to modularize. But it enables you to define the rules.

If you use packages and defines (In a design document? Verbal?) that certain packages may not access classes in other packages, without an enforcement of this constraint, it will be broken. If you hire new developers they don't know the rules. They WILL break the rules. With OSGi you can define the rules in code. For us this was a big win, as it has helped us to maintain the architecture of our system.

OSGi does not reduce complexity. But it definitely helps to handle it.

hoipolloi
  • 7,984
  • 2
  • 27
  • 28
Arne Deutsch
  • 14,629
  • 5
  • 53
  • 72
5

I am using OSGI for over 8 years now, and every time I dive in a non-OSGI project I get the feeling over overspeeding without a seatbelt on. OSGI makes project setup and deployment harder, and forces you to think about modularization upfront, but gives you the easy of mind of enforcing the rules at runtime. Take maven apache camel as an example. When you create a new maven project and add apache camel as a dependency, the applications seems to have all its dependencies, and you will only notice the ClassNotFoundExceptions at runtime, which is bad. When you run in an OSGI container and load the apache camel modules, the modules with unmet dependencies are not started, and you know upfront what the problem is.

I also use the hot-deployment all the time, and update parts of my application on the fly without the need for a restart.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Leen Toelen
  • 389
  • 6
  • 12
4

OSGi does NOT pay off. The fact is OSGi is not easy to use and at the end of the day or year depending on how long it takes you to get things working, it does not add value:

  • Your application will not be more modular overall, on the contrary, It ends being more exposed and not isolated from other applications since it is a share everything instead of share nothing arch.
  • Versioning is pushed further down the stack, you wrestle with maven transitive dependencies only to do that again at runtime in OSGI.
  • Most libraries are designed to work as libraries in the application classloader not as bundles with their own classloader.
  • Maybe appropriate for plugin architectures where third party developers need to be sandboxed or maybe it is just EJB2.0 all over again.

I added the following slides and I will follow up with example code to demonstrate how to work successfully with OSGi if it is forced on you. http://www.slideshare.net/ielian/tdd-on-osgi

eliani
  • 59
  • 3
  • Welcome to [so]. You might want to expand on your answer, such as why OSGi is hard to use and whether OSGi has any benefits whatsoever. You might want to take a look at [answer], which provides useful hints on how to write great answers. – Qantas 94 Heavy Jan 28 '14 at 00:31
  • Thanks for your comment. I hope the slides I included help with that. – eliani Feb 27 '14 at 21:56
  • "share everything instead of share nothing arch." -> OSGi subsystems. "Versioning is pushed further down the stack, you wrestle with maven transitive dependencies only to do that again at runtime in OSGI."-> it actually solves so many issues related to transitive dependencies, when there is a version collision. "Most libraries are designed to work as libraries in the application classloader not as bundles "-> only very few libraries have actual issues being used in OSGi environments (hibernate, last time I checked, was the best example) – santiagozky Dec 12 '15 at 21:17
  • I cannot stress more how I support your answer. I've been working on a small project that does a bunch of tricks for over a year now. Initially it was a small monolith and then it was decided to go modular and the dependency/version hell continues. After a year now the project consists of 100+ maven modules most of which contain 2-3 classes just to allow for flexibility. We had to create an peer interface for every model object that is used across other teams just to fence off the transitive dependencies on other libraries. It's a package hell + a dependency hell. – anydoby Apr 25 '18 at 03:09
4

I used OSGI in one project (I admit - not very much). It provides good promises, but as @Arne said, you still need to think on your own about how you modularize.

OSGI did help our project because it made the architecture more stable. Breaking the modularization is more "difficult", so decisions that we made regarding how to modularize stayed valid for a longer time.
To put it differently - without OSGI, and under time pressure to deliver, sometimes you or your team members make compromises, shortcuts and other hacks, and the the original intent of the architecture is lost.

So OSGI didn't reduce the complexity per se, but it protected it from growing unnecessarily over time. I guess that is a good thing :)

I haven't used the hot deploy feature, so I can't comment about that.

To answer your last point, it did meet my expectations, but it required a learning curve and some adaption, and the payoff is only for long-term.

(as a side note, your question reminds me a bit of the adage that "maven is the awt of build systems")

Yoni
  • 10,171
  • 9
  • 55
  • 72
  • I have not heard of the "maven is the awt of build systems" saying before, and could not locate it with Google. Do you have a link? – Thorbjørn Ravn Andersen Feb 17 '10 at 06:22
  • From this link: http://prezi.com/ngwwf1wcfn7n/your-next-sucessful-build/ ... the text right below the presentation: "Using Maven2 to build tools was like AWT to UI frameworks: revolutionary, but not without downsides." – Yoni Feb 17 '10 at 14:57
-1

No, OSGI will make you grey early.

Jeb
  • 15,939
  • 6
  • 34
  • 37
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Ian Kenney Apr 29 '15 at 21:42
  • How about this then? Given no framework can ever help deciding what to modularize, has OSGi ever payed off for you? no Has it made your life easier when working in teams? no Has it helped to reduce bug count? no Do you ever successfully "hotdeploy" major components? no Does OSGi help to reduce complexity over time? no Did OSGi keep its promises? no Did it fulfill your expectations? no – Jeb Apr 30 '15 at 22:52
  • Admitting the fact that @Jeb's last comment is full of sometimes superfluous criticism I can list multiple cases from my experience that prove he is right. In addition, many major frameworks abandoned support of OSGI, using third party libs becomes a real problem even with runtime wrapping to bundles, provided for example by Apache Karaf. One quote from one smart man very precisely describes my many years experience with OSGI - "Sounds good, doesn't work". – Yuriy Feb 01 '18 at 19:02
  • Combining osgi and non-osgi projects in one company is at least challenging. If you want to reuse even pojos from other teams using, let's say, annotations from a library you are not using or cannot expose to your service users is really difficult. Adding a new field to a pojo becomes a tough task since you have to remember to increment versions of your services using this new thing. I personally did not find osgi productive to justify introducing yet another complexity vector. It's much easier to use spring and docker if you want isolation. For the rest it's pure waste of time (my opinion). – anydoby Apr 25 '18 at 03:30