Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld...
Transcript of Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld...
![Page 1: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/1.jpg)
Copyright © 2016 Alex Blewitt
Modularity in JavaWith OSGi
Alex Blewitt @alblue
Docklands.LJC January 2016
![Page 2: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/2.jpg)
Copyright © 2016 Alex Blewitt
Modularity in Java
![Page 3: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/3.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Easy?
![Page 4: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/4.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 5: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/5.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 6: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/6.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 7: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/7.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 8: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/8.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 9: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/9.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 10: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/10.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 11: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/11.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 12: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/12.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 13: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/13.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
![Page 14: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/14.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Easy?
![Page 15: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/15.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
solutions are complex
solutions are hard
sufficiently advanced technology is indistinguishable from magic
![Page 16: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/16.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
OSGi is complex
OSGi is hard
sufficiently advanced technology is indistinguishable from magic
![Page 17: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/17.jpg)
Copyright © 2016 Alex Blewitt
Modularity is Hard!
sufficiently advanced technology is indistinguishable from magic
Jigsaw is hard
Jigsaw is complex
![Page 18: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/18.jpg)
Copyright © 2016 Alex Blewitt
Why do people think modularity is easy?
• Java's modular already, right?
• Fields and Methods
• Classes
• Packages
• JARs Maven/Gradle
"Lies to Children"
![Page 19: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/19.jpg)
Copyright © 2016 Alex Blewitt
• Terry Pratchett – Science of the Discworld
Any explanation of an observed phenomenon which, while not 100% scientifically accurate, is simple enough, and just accurate enough, to convey the beginnings of understanding to anyone who is new to the subject.
*also known as Lies to Bosses
*"Lies to Children"
http://wiki.lspace.org/mediawiki/index.php/Lies-To-Children
![Page 20: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/20.jpg)
Copyright © 2016 Alex Blewitt
• Fields are private
• Apart from reflection
• Fields are final
• Apart from setAccessible
• Methods are standalone
• Apart from lambdas and inner classes
"Lies to Developers"
![Page 21: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/21.jpg)
Copyright © 2016 Alex Blewitt
• Classes are encapsulated
• Apart from dependent types for internal dependencies
• Packages are boundaries for classes
• Except cyclic references between packages can easily occur
• JARs are unique elements of deployment
• Except JARs can contain duplicate classes ("first one wins")
"Lies to Developers"
![Page 22: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/22.jpg)
Copyright © 2016 Alex Blewitt
• Only one class/static per VM
• Classes are unique per ClassLoader, not VM
• JARs are versioned
• Except no-one agrees on version numbers
• Semantic versioning is important
• Except when it isn't
"Lies to Developers"
https://github.com/vert-x3/wiki/wiki/3.1-Release-Notes
![Page 23: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/23.jpg)
Copyright © 2016 Alex Blewitt
Semantic Versioning
1.2.3.db4fa6Major
MinorPatch Qualifier
Breaking Change
New Features
Bug Fix
Build Identifier Timestamp
http://semver.org
Numeric Textual
![Page 24: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/24.jpg)
Copyright © 2016 Alex Blewitt
What Developers believe• Dependencies are easy to manage
JitWatch 1.0.0 slf4j-api 1.7.7
logback-classic 1.1.2 logback-core 1.1.2
![Page 25: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/25.jpg)
Copyright © 2016 Alex Blewitt
What Developers believe• Transitive dependencies are easy to manage
JitWatch 1.0.0 slf4j-api 1.7.7
logback-classic 1.1.2 logback-core 1.1.2
logback-core 1.1.2 slf4j-api 1.7.6
jansi 1.9
![Page 26: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/26.jpg)
Copyright © 2016 Alex Blewitt
What Developers believe• Optional transitive dependencies are easy to
manageJitWatch 1.0.0 slf4j-api 1.7.7
logback-classic 1.1.2 logback-core 1.1.2
logback-core 1.1.2 slf4j-api 1.7.6
jansi 1.9
groovy-all 2.0.7 jansi 1.6
![Page 27: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/27.jpg)
Copyright © 2016 Alex Blewitt
What Developers believe• Test optional transitive dependencies are easy to
manageJitWatch 1.0.0 slf4j-api 1.7.7
logback-classic 1.1.2 logback-core 1.1.2
logback-core 1.1.2 slf4j-api 1.7.6
jansi 1.9
groovy-all 2.0.7 jansi 1.6subethasmtp 2.1.0 slf4j-api 1.6
* many dependencies not shown for brevity
![Page 28: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/28.jpg)
Copyright © 2016 Alex Blewitt
What Developers hope• It all just works
slf4j-apijitwatch logback-classic logback-core jansi janio
![Page 29: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/29.jpg)
Copyright © 2016 Alex Blewitt
What the JVM sees• Series of JARs loaded in a ClassLoader
slf4j-apijitwatch logback-classic logback-core jansi janio
![Page 30: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/30.jpg)
Copyright © 2016 Alex Blewitt
What the JVM sees• JVM sees a one-dimensional list of classes
slf4j-apijitwatch logback-classic logback-core jansi janio
![Page 31: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/31.jpg)
Copyright © 2016 Alex Blewitt
Looking up classes• Resolving a class is stepping along to find it
• Packages are ignored
• No concept of modularity
![Page 32: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/32.jpg)
Copyright © 2016 Alex Blewitt
Module busting• Compile- & run- time dependencies may differ
• Class.forName() can bust through module barriers
• Dynamic instantiation may look up implementation
• SLF4J – which logger to use
• Hibernate – looking up database drivers
• Annotation scanners walk the entire list of classes
![Page 33: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/33.jpg)
Copyright © 2016 Alex Blewitt
Modularisation• Only adds benefit once reaching a certain size
• No-one needs a Hello World module
• Difficult to retro fit
• (Just ask the Jigsaw team)
• Prevents accidental leakage between packages
• jetty-client 6.1.23 -> jetty (server) 6.1.23
util package
org.mortbay.io package
![Page 34: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/34.jpg)
Copyright © 2016 Alex Blewitt
Packages are leaky• Classes are oblivious to package boundaries
slf4j-apijitwatch logback-classic logback-core jansi janio
Friendly classes/interfaces are only available in the same package
However the same friendly package can be present in two or more JAR files
![Page 35: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/35.jpg)
Copyright © 2016 Alex Blewitt
Packages are leaky• Classes can follow transitive chain accidentally
slf4j-apijitwatch logback-classic logback-core jansi janio
It is easy to accidentally depend on a class that comes from a transitive dependency without
realising that it has happened
JIT watch does not do this; it's used as an example
![Page 36: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/36.jpg)
Copyright © 2016 Alex Blewitt
Packages are leaky• JARs can have cycles
slf4j-apijitwatch logback-classic logback-core jansi janio
More common in unstructured builds or single-project IDEs
![Page 37: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/37.jpg)
Copyright © 2016 Alex Blewitt
Unstructured buildssrc/com/example/client/Client.java
javac -d client com/example/client/*.java
public class Client { void method() { }}
import com.example.server.AnException;
src/com/example/server/Server.javasrc/com/example/server/AnException.java
javac -d server com/example/server/*.java
throws AnException {Client directory now contains client/com/example/server/
AnException.class
![Page 38: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/38.jpg)
Copyright © 2016 Alex Blewitt
Accidental dependenciesjava.beans java.util
java.iojava.awt
java.applet
Everything depends on util …
Because a BeanDescriptor can have an java.awt.Image
Twenty years later and Applets are still the bane
of Java's existence!
Beans.instantiate takes a parameter
AppletInitializer
![Page 39: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/39.jpg)
Copyright © 2016 Alex Blewitt
Easy come, hard go
http://openjdk.java.net/projects/jigsaw/doc/jdk-modularization.htmlhttp://cr.openjdk.java.net/~mchung/jigsaw/graphs/jdk8-b48.png
Diagram is based on older Java 8 version, may change
Bidirectional module dependencies!
Beans and Desktop were merged in Java 9
![Page 40: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/40.jpg)
Copyright © 2016 Alex Blewitt
It's amazing anything works at all …
![Page 41: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/41.jpg)
Copyright © 2016 Alex Blewitt
How do we solve these problems?
![Page 42: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/42.jpg)
Copyright © 2016 Alex Blewitt
Module barriers• Good fences make good neighbours
slf4j-apijitwatch logback-classic logback-core jansi janio
![Page 43: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/43.jpg)
Copyright © 2016 Alex Blewitt
Module barriers• Good fences make good neighbours
slf4j-apijitwatch logback-classic logback-core jansi janio
module
Exports Depends
![Page 44: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/44.jpg)
Copyright © 2016 Alex Blewitt
OSGi and JigsawThis is where they start to differ
OSGi • Dynamic • MANIFEST.MF • Services • Export
• Package • Import
• Module* • Package
• Versioned • Module • Package
Jigsaw • Static • module-info • ServiceLoader • Export
• Package • Import
• Module*
• No versioning
* Module dependencies may be declared as transitive
![Page 45: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/45.jpg)
Copyright © 2016 Alex Blewitt
OSGi
Require-Bundle Import-Package
Export-PackageModules in OSGi are called Bundles Each bundle exports
its own public API via packages
Dependencies can be on whole
bundle
Or can be on a package by
package basis
![Page 46: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/46.jpg)
Copyright © 2016 Alex Blewitt
MANIFEST.MFExport-Package: com.example.ui.widgetsImport-Package: com.example.utilRequire-Bundle: com.example.monolithBundle-SymbolicName: com.example.uiBundle-Version: 1.2.3Bundle-ManifestVersion: 2
MANIFEST.MF
Manifest.MF chosen because it is first file in JAR and therefore easily accessible
![Page 47: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/47.jpg)
Copyright © 2016 Alex Blewitt
How is the manifest used?• Can be used by compiler to construct paths
• Can be used by runtime to ensure dependencies
• Can be used by IDEs to wire projects together
• Can be used to resolve dependencies from repo
• Can be used by GUIs to show content
• Can be used by humans for documentation
![Page 48: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/48.jpg)
Copyright © 2016 Alex Blewitt
OSGi Frameworks• Bundles are managed by a framework
• Felix
• Equinox
• Knopflerfish
• Prosyst
OSGi frameworks are like WebApp engines like Tomcat or Jetty
1. Start Tomcat 2. Drop in WAR file 3. Profit!
![Page 49: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/49.jpg)
Copyright © 2016 Alex Blewitt
OSGi Frameworks• Bundles are managed by a framework
• Felix
• Equinox
• Knopflerfish
• Prosyst
OSGi frameworks are like WebApp engines like Tomcat or Jetty
1. Start Tomcat 2. Drop in WAR file 3. Profit!1. Start OSGi 2. Drop in JAR file 3. Profit!
![Page 50: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/50.jpg)
Copyright © 2016 Alex Blewitt
OSGi Frameworks
INSTALLED RESOLVED ACTIVE
?
How do we refer across bundles?
![Page 51: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/51.jpg)
Copyright © 2016 Alex Blewitt
OSGi Services• Services provide a way of bundles to communicate
• Have a shared interface (e.g. java.sql.Driver)
• Bundles can provide service instances
• Bundles can require service instances
• Service registry stores service instances
![Page 52: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/52.jpg)
Copyright © 2016 Alex Blewitt
OSGi Servicesjava.sql
org.hibernate com.mysql.jdbc
Service RegistryImplementation com.mysql.jdbc.Driver
Interface java.sql.Driver
class.forName()
Inversion of control
![Page 53: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/53.jpg)
Copyright © 2016 Alex Blewitt
OSGi Servicesjava.sql
org.hibernate com.mysql.jdbc
context.registerService(interface,instance)
The framework gives you the BundleContext, like Spring
gives an ApplicationContext
![Page 54: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/54.jpg)
Copyright © 2016 Alex Blewitt
OSGi Servicesjava.sql
org.hibernate com.mysql.jdbc
context.registerService(interface,instance)context.getService(interface)*
* actually it gets a Service from a ServiceReference
![Page 55: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/55.jpg)
Copyright © 2016 Alex Blewitt
OSGi Servicesjava.sql
org.hibernate com.mysql.jdbc
context.registerService(interface,instance)context.getService(interface)
Service users have to cope with the service not being present (null) and acting accordingly
![Page 56: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/56.jpg)
Copyright © 2016 Alex Blewitt
OSGi Servicesjava.sql
org.hibernate com.mysql.jdbc
Declarative Services
I can provide a DriverI can need a Driver<xml/><xml/>
Can be generated from annotations in code
![Page 57: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/57.jpg)
Copyright © 2016 Alex Blewitt
Dynamic OSGi
![Page 58: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/58.jpg)
Copyright © 2016 Alex Blewitt
How does this work?• Things can't come and go in a Java program!
• Because classes are cached by the ClassLoader
• WebApps can come and go in a Tomcat server …
• Each WebApp gets its own ClassLoader
• When WebApp is removed, ClassLoader goes
• Classes are recycled
![Page 59: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/59.jpg)
Copyright © 2016 Alex Blewitt
Bundle barriers• Each bundle has its own ClassLoader
slf4j-apijitwatch logback-classic logback-core jansi janio
Each module ClassLoader implements visibility rules
When module is stopped, ClassLoader thrown away
Works in the same way as Tomcat and WebApps
👮 👮 👮 👮 👮 👮
ClassLoader is the API police
![Page 60: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/60.jpg)
Copyright © 2016 Alex Blewitt
ClassLoaders
• ClassLoaders are critical to the success of Java
• Allowed evolution of files -> JARs -> Jmods
• Propelled Java into enterprise with Servlets
• Popularised Java through AppletClassLoader
ClassLoaders are the guardians of the Java spirit
![Page 61: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/61.jpg)
Copyright © 2016 Alex Blewitt
Getting Started with OSGi1. Update existing build to generate OSGi metadata
• Maven: maven-bundle-plugin
• Gradle: apply plugin: 'osgi'
• or: apply plugin: 'biz.aQute.bnd'
2. Install bundles into OSGi framework
3. Use OSGi console to inspect dependencies
![Page 62: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/62.jpg)
Copyright © 2016 Alex Blewitt
Getting Started with OSGi4. Use annotations to define service components
5. Decompose larger bundles into smaller ones
6. Review dependencies regularly
7. Use a tool to verify semantic versioning
![Page 63: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/63.jpg)
Copyright © 2016 Alex Blewitt
OSGi and Jigsaw• OSGi and Jigsaw have different target markets
• OSGi uses a dynamic application runtime
• Jigsaw is about the modular JDK
• Both will encourage Java developers to modularise
• They share far more in common than differences
![Page 64: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/64.jpg)
Copyright © 2016 Alex Blewitt
OSGi and Jigsaw• Changes for Jigsaw will benefit OSGi & vice-versa
• Fixes for Class.forName()
• Proper segregation into modular boundaries
• Using services to acquire implementations (IoC)
• OGSi and Jigsaw interoperability getting closer
• "OSGi and Java 9 Modules Working Together" (Neil Bartlett)
http://njbartlett.name/2015/11/13/osgi-jigsaw.html
![Page 65: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/65.jpg)
Copyright © 2016 Alex Blewitt
OSGi and Jigsaw
OSGi
Jigsaw
![Page 66: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/66.jpg)
Copyright © 2016 Alex Blewitt
OSGi and Jigsaw
OSGi
Jigsaw
Similarities • Module paths • Strict separation • Future of Java • Services to separate
Differences • Static vs Dynamic • Package imports • Service creation • Versioning • JARs vs Jmods • Java Any vs Java 9
Can OSGi use Jigsaw modules or load Jmods? Can Jigsaw modules use OSGi bundles? Can Jigsaw services be created manually?
![Page 67: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/67.jpg)
Copyright © 2016 Alex Blewitt
Future of Java• The future of Java is modular
• Will cause some pain
• Will highlight less-than-perfect dependencies
• Will cause problems for Class.forName() code
• No "one true classpath"
• Module paths are the path to success
Use .class instead
![Page 68: Modularity in JavaCopyright © 2016 Alex Blewitt • Terry Pratchett – Science of the Discworld Any explanation of an observed phenomenon which, while not 100% scientifically ...](https://reader033.fdocuments.us/reader033/viewer/2022050103/5f42683e731c9e64846ca9cd/html5/thumbnails/68.jpg)
Copyright © 2016 Alex Blewitt
Questions?Modularity in Java with OSGi
Alex Blewitt @alblue
Docklands.LJC January 2016http://docklandsljc.uk