Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use separate class loaders and run in same JVM? (OSGI)

I've read that OSGI uses separate classloaders per module which allows modules to use different versions of their dependencies.. while at the same time running all modules in the same JVM.

How does this work? If module A uses version #1 of a dependency and module B uses version #2, won't you run into trouble if module A passes an instance of the dependency class to module B as a method parameter?

I would think module B would choke if it was expecting a different interface to the dependency class.

like image 605
Marcus Leon Avatar asked Mar 16 '10 16:03

Marcus Leon


1 Answers

You're right that inconsistent dependencies can cause problems. OSGi avoids this by calculating the transitive closure of these dependencies and ensuring that there are none at resolution time.

This allows you to expose a public dependency whilst having internal/hidden private dependencies and, as a result, hide your implementation dependencies to avoid this. The good thing is all thus is checked at Bundle resolution time rather than obscure runtime errors.

Specifically in your example, if A and B uses an incompatible interface, and A depends on B, then A will fail to resolve with a dependency error. So it won't even be able to start to pass it an incompatible type.

like image 78
AlBlue Avatar answered Nov 11 '22 18:11

AlBlue