Slide 1

Slide 1 text

Copyright © 2016 Alex Blewitt Modularity in Java With OSGi Alex Blewitt @alblue Docklands.LJC January 2016

Slide 2

Slide 2 text

Copyright © 2016 Alex Blewitt Modularity in Java

Slide 3

Slide 3 text

Copyright © 2016 Alex Blewitt Modularity is Easy?

Slide 4

Slide 4 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 5

Slide 5 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 6

Slide 6 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 7

Slide 7 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 8

Slide 8 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 9

Slide 9 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 10

Slide 10 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 11

Slide 11 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 12

Slide 12 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 13

Slide 13 text

Copyright © 2016 Alex Blewitt Modularity is Hard!

Slide 14

Slide 14 text

Copyright © 2016 Alex Blewitt Modularity is Easy?

Slide 15

Slide 15 text

Copyright © 2016 Alex Blewitt Modularity is Hard! solutions are complex solutions are hard sufficiently advanced technology is indistinguishable from magic

Slide 16

Slide 16 text

Copyright © 2016 Alex Blewitt Modularity is Hard! OSGi is complex OSGi is hard sufficiently advanced technology is indistinguishable from magic

Slide 17

Slide 17 text

Copyright © 2016 Alex Blewitt Modularity is Hard! sufficiently advanced technology is indistinguishable from magic Jigsaw is hard Jigsaw is complex

Slide 18

Slide 18 text

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"

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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"

Slide 21

Slide 21 text

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"

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Copyright © 2016 Alex Blewitt Semantic Versioning 1.2.3.db4fa6 Major Minor Patch Qualifier Breaking Change New Features Bug Fix Build Identifier Timestamp http://semver.org Numeric Textual

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Copyright © 2016 Alex Blewitt What Developers believe • Optional 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 groovy-all 2.0.7 jansi 1.6

Slide 27

Slide 27 text

Copyright © 2016 Alex Blewitt What Developers believe • Test optional 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 groovy-all 2.0.7 jansi 1.6 subethasmtp 2.1.0 slf4j-api 1.6 * many dependencies not shown for brevity

Slide 28

Slide 28 text

Copyright © 2016 Alex Blewitt What Developers hope • It all just works slf4j-api jitwatch logback-classic logback-core jansi janio

Slide 29

Slide 29 text

Copyright © 2016 Alex Blewitt What the JVM sees • Series of JARs loaded in a ClassLoader slf4j-api jitwatch logback-classic logback-core jansi janio

Slide 30

Slide 30 text

Copyright © 2016 Alex Blewitt What the JVM sees • JVM sees a one-dimensional list of classes slf4j-api jitwatch logback-classic logback-core jansi janio

Slide 31

Slide 31 text

Copyright © 2016 Alex Blewitt Looking up classes • Resolving a class is stepping along to find it • Packages are ignored • No concept of modularity

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Copyright © 2016 Alex Blewitt Packages are leaky • Classes are oblivious to package boundaries slf4j-api jitwatch 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

Slide 35

Slide 35 text

Copyright © 2016 Alex Blewitt Packages are leaky • Classes can follow transitive chain accidentally slf4j-api jitwatch 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

Slide 36

Slide 36 text

Copyright © 2016 Alex Blewitt Packages are leaky • JARs can have cycles slf4j-api jitwatch logback-classic logback-core jansi janio More common in unstructured builds or single-project IDEs

Slide 37

Slide 37 text

Copyright © 2016 Alex Blewitt Unstructured builds src/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.java src/com/example/server/AnException.java javac -d server com/example/server/*.java throws AnException { Client directory now contains client/com/example/server/ AnException.class

Slide 38

Slide 38 text

Copyright © 2016 Alex Blewitt Accidental dependencies java.beans java.util java.io java.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

Slide 39

Slide 39 text

Copyright © 2016 Alex Blewitt Easy come, hard go http://openjdk.java.net/projects/jigsaw/doc/jdk-modularization.html http://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

Slide 40

Slide 40 text

Copyright © 2016 Alex Blewitt It's amazing anything works at all …

Slide 41

Slide 41 text

Copyright © 2016 Alex Blewitt How do we solve these problems?

Slide 42

Slide 42 text

Copyright © 2016 Alex Blewitt Module barriers • Good fences make good neighbours slf4j-api jitwatch logback-classic logback-core jansi janio

Slide 43

Slide 43 text

Copyright © 2016 Alex Blewitt Module barriers • Good fences make good neighbours slf4j-api jitwatch logback-classic logback-core jansi janio module Exports Depends

Slide 44

Slide 44 text

Copyright © 2016 Alex Blewitt OSGi and Jigsaw This 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

Slide 45

Slide 45 text

Copyright © 2016 Alex Blewitt OSGi Require-Bundle Import-Package Export-Package Modules 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

Slide 46

Slide 46 text

Copyright © 2016 Alex Blewitt MANIFEST.MF Export-Package: com.example.ui.widgets Import-Package: com.example.util Require-Bundle: com.example.monolith Bundle-SymbolicName: com.example.ui Bundle-Version: 1.2.3 Bundle-ManifestVersion: 2 MANIFEST.MF Manifest.MF chosen because it is first file in JAR and therefore easily accessible

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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!

Slide 49

Slide 49 text

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!

Slide 50

Slide 50 text

Copyright © 2016 Alex Blewitt OSGi Frameworks INSTALLED RESOLVED ACTIVE ? How do we refer across bundles?

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Copyright © 2016 Alex Blewitt OSGi Services java.sql org.hibernate com.mysql.jdbc Service Registry Implementation com.mysql.jdbc.Driver Interface java.sql.Driver class.forName() Inversion of control

Slide 53

Slide 53 text

Copyright © 2016 Alex Blewitt OSGi Services java.sql org.hibernate com.mysql.jdbc context.registerService(interface,instance) The framework gives you the BundleContext, like Spring gives an ApplicationContext

Slide 54

Slide 54 text

Copyright © 2016 Alex Blewitt OSGi Services java.sql org.hibernate com.mysql.jdbc context.registerService(interface,instance) context.getService(interface)* * actually it gets a Service from a ServiceReference

Slide 55

Slide 55 text

Copyright © 2016 Alex Blewitt OSGi Services java.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

Slide 56

Slide 56 text

Copyright © 2016 Alex Blewitt OSGi Services java.sql org.hibernate com.mysql.jdbc Declarative Services I can provide a Driver I can need a Driver Can be generated from annotations in code

Slide 57

Slide 57 text

Copyright © 2016 Alex Blewitt Dynamic OSGi

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Copyright © 2016 Alex Blewitt Bundle barriers • Each bundle has its own ClassLoader slf4j-api jitwatch 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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Copyright © 2016 Alex Blewitt Getting Started with OSGi 1. 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

Slide 62

Slide 62 text

Copyright © 2016 Alex Blewitt Getting Started with OSGi 4. 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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Copyright © 2016 Alex Blewitt OSGi and Jigsaw OSGi Jigsaw

Slide 66

Slide 66 text

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?

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Copyright © 2016 Alex Blewitt Questions? Modularity in Java with OSGi Alex Blewitt @alblue Docklands.LJC January 2016 http://docklandsljc.uk