Can OSGi help reduce complexity?

后端 未结 6 1569
傲寒
傲寒 2021-02-01 04:20

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 pa

相关标签:
6条回答
  • 2021-02-01 04:29

    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.

    0 讨论(0)
  • 2021-02-01 04:32

    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

    0 讨论(0)
  • 2021-02-01 04:32

    No, OSGI will make you grey early.

    0 讨论(0)
  • 2021-02-01 04:39

    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")

    0 讨论(0)
  • 2021-02-01 04:45

    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.

    0 讨论(0)
  • 2021-02-01 04:45

    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.

    0 讨论(0)
提交回复
热议问题