Java 9 Modularity in Action

5,669 views

Published on

With Java 9, modularity comes to your doorstep (whether you ordered it or not). This isn't your average language feature: making the most out of it may involve rewiring your brain. In this session we explore the benefits of a modular codebase using Java 9 modules. Because who doesn’t like more reliable and secure applications, meanwhile killing the dreaded classpath?

After reviewing the basic concepts of modules, we'll explore modularity patterns that enhance your design and development skills. We all have existing software, so migration of codebases to a modular world is an important topic as well. Automatic modules and other new concepts are added to your toolbox to this end.

Want examples? We've got plenty in this practical and code-driven talk. Even if you intend to use Java 9 without modules, this talk helps you prevent common issues. You'll be ready for Java 9 before you know it.

Both speakers are currently writing “Java 9 Modularity” for O’Reilly, expected early 2017. They have extensive experience under the belt already with the Java 9 Jigsaw early access builds.

Published in: Software

Java 9 Modularity in Action

  1. 1. By Sander Mak Java 9 Modularity in Action @Sander_Mak
  2. 2. Today's journey Modularity matters Java 9 modules Services Migration Linking
  3. 3. Modularity matters Modularity is the ultimate agile tool
  4. 4. Good fences make good neighbours Hide your internals! Service contract Module
  5. 5. Work in progress, almost there now. Java 9: Jigsaw, JSR-376
  6. 6. Goals ‣ Modularise the JDK ‣ Reliable application composition ‣ Strong encapsulation - hide platform internals ‣ Improved security Current status ‣ JDK 9 modularised ‣ JSR 376 prototype (Jigsaw) ‣ Spec in final draft
  7. 7. History of Jigsaw JSR-277 Java Module System 2005 2006 2011 2014 JSR-294 Improved Modularity Support Java 7: No Jigsaw Java 8: No Jigsaw JSR-376 Java Platform Module System JSR-291 'OSGi 4.1'
  8. 8. Something happened in May... Java 9: Jigsaw, JSR-376
  9. 9. Drama, intrigue, politics
  10. 10. Drama, intrigue, politics JCP Executive Committee
  11. 11. Drama, intrigue, politics JCP Executive Committee
  12. 12. JEP 200: The Modular JDK
  13. 13. Goodbye classpath Reliable configuration
  14. 14. Goodbye classpath Modules with explicit dependencies my.module requires other.module
  15. 15. Strong encapsulation Hide your internals java.base java.lang java.time java.util com.sun.* sun.* jdk.internal.* my.module requires Access checks at VM level (even reflection)
  16. 16. Why not just use OSGi? ‣ OSGi is built on top of the JVM ‣ Can’t be used to modularise the JDK itself
  17. 17. Why not just use OSGi? ‣ OSGi is built on top of the JVM ‣ Can’t be used to modularise the JDK itself Why not only modularise the JDK? ‣ Fair point :-) ‣ OSGi has never seen mass adoption, Java 9 will bring modularity to all of us
  18. 18. Demo: EasyText Analyze text complexity easytext.cli easytext.analyis requires
  19. 19. Contracts & Services MyInterface i = new MyImpl(); How to use code that you can’t access?
  20. 20. Contracts & Services Provider module Service Registry Consumer module Register service Lookup service Return service instance Use an interface as contract
  21. 21. JSR-376 services module myApi { exports com.api; }
  22. 22. JSR-376 services module myApi { exports com.api; } module myConsumer { requires myApi; uses com.api.MyService; }
  23. 23. JSR-376 services module myApi { exports com.api; } ‣ Services resolved and wired by modular runtime ‣ Use with 'enhanced' ServiceLoader API module myConsumer { requires myApi; uses com.api.MyService; } module myProvider { requires myApi; provides com.api.MyService with myProvider.MyServiceImpl; }
  24. 24. JSR-376 services module myApi { exports com.api; } ‣ Services resolved and wired by modular runtime ‣ Use with 'enhanced' ServiceLoader API module myConsumer { requires myApi; uses com.api.MyService; } module myProvider { requires myApi; provides com.api.MyService with myProvider.MyServiceImpl; } Iterable<MyService> services = ServiceLoader.load(MyService.class); for(MyService svc: services) { svc.doSomething(); }
  25. 25. Extensibility Demo: EasyText
  26. 26. Extensibility Multiple algorithms: ‣ Flesch-Kincaid ‣ Coleman-Liau Demo: EasyText
  27. 27. Extensibility CLI & GUI? Multiple algorithms: ‣ Flesch-Kincaid ‣ Coleman-Liau Demo: EasyText
  28. 28. easytext.algorithm.api easytext.algorithm.kincaid easytext.algorithm.coleman easytext.cli easytext.gui Demo: EasyText javafx.controls JDKApplication javafx.graphics
  29. 29. easytext.algorithm.api easytext.algorithm.kincaid easytext.algorithm.coleman easytext.cli easytext.gui Demo: EasyText javafx.controls JDKApplication ServiceLoader javafx.graphics
  30. 30. JSR-376 linking ‣ Use a linking tool (jlink) to create a custom 'runtime image' with only the modules you need ‣ Uses explicit dependencies from module-info.class ‣ Allows for whole-program optimization Runtime image java.base java.desktop my.mod1 my.mod2 JVM
  31. 31. Demo: EasyText linking
  32. 32. ‣ Use jdeps to audit your code ‣ Escape hatch: 
 --add-exports java.base/javax.security.auth.x500=mymod Preparing for Java 9
  33. 33. ‣ Use jdeps to audit your code ‣ Escape hatch: 
 --add-exports java.base/javax.security.auth.x500=mymod Preparing for Java 9 ‣ Gradual migration possible ‣ Mix classpath & modulepath ‣ Automatic modules
  34. 34. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path
  35. 35. package com.javamodularity.demonstrator; import org.apache.commons.lang3.StringUtils; public class Demo { public static void main(String args[]) { String output = StringUtils.leftPad("Leftpad FTW!", 20); System.out.println(output); } } Classic classpath
  36. 36. package com.javamodularity.demonstrator; import org.apache.commons.lang3.StringUtils; public class Demo { public static void main(String args[]) { String output = StringUtils.leftPad("Leftpad FTW!", 20); System.out.println(output); } } Classic classpath javac -cp lib/commons-lang3-3.4.jar -d out $(find src -name '*.java') java -cp out:lib/commons-lang3-3.4.jar com.javamodularity.demonstrator.Demo Compile Run
  37. 37. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path
  38. 38. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path Can’t reference the classpath from named modules!
  39. 39. Top down migration demonstrator.jar classpath java.base module path commons.lang But this isn’t our code!
  40. 40. Automatic Modules ‣ A plain JAR on the module path becomes an Automatic Module ‣ Module name derived from JAR name ‣ Exports everything ‣ Reads all other modules
  41. 41. module demonstrator { requires commons.lang; } Using Automatic Modules
  42. 42. module demonstrator { requires commons.lang; } Using Automatic Modules javac --module-path lib --module-source-path src -d mods $(find src -name '*.java') java --module-path mods:lib -m demonstrator/com.javamodularity.demonstrator.Demo Compile Run
  43. 43. Java 9 release July 27th 2017
  44. 44. Java 9 release July 27th 2017
  45. 45. Java 9 release July 27th 2017 September 21st 2017 No promises :-)
  46. 46. ‣ JSR-376 forces module-aware tooling ‣ Modularity throughout all development phases Java 9's promise
  47. 47. ‣ JSR-376 forces module-aware tooling ‣ Modularity throughout all development phases Java 9's promise Spotlight on modularity == good
  48. 48. Thank you. bit.ly/java9book @javamodularity
  49. 49. Thank you. bit.ly/java9book @javamodularity 40% discount: AUTHD
  50. 50. bit.ly/java9book @javamodularity bit.ly/java9course Java 9 Modularity: First Look Thank you.40% discount: AUTHD

×