Java 9 and Project Jigsaw

960 views

Published on

From the Developer day of the Hungarian Oracle User Group. 2015.10.08

Author: Marton Elek, DPC Consulting
Event: HOUG Szakmai nap 2015, 2015.10.07

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
960
On SlideShare
0
From Embeds
0
Number of Embeds
70
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java 9 and Project Jigsaw

  1. 1. Java SE 9, Project Jigsaw Elek Márton @anzix 2015 March DPC Consulting
  2. 2. Java 9 roadmap 2015/09/08 Mark Reinhold: The state of the module system (Java 9 b83) 2015/12/10 Feature Complete 2016/02/04 All Tests Run 2016/02/25 Rampdown Start 2016/04/21 Zero Bug Bounce 2016/06/16 Rampdown Phase 2 2016/07/21 Final Release Candidate 2016/09/22 General Availability
  3. 3. Planned features
  4. 4. Improvements ● Java REPL ● HTTP2 Client ● UTF-8 Property files ● Microbenchmark Suite ● Compiler improvements ● Money and Currency API, JSR-354 (?) ● JSON API (? - proposed to be dropped) ● ProcessAPI update (pid, process list) ● HTML5 Javadoc ● Make G1 the default GC algorithm ● Remove deprecated GC combinations
  5. 5. Modularization
  6. 6. Related JEPs/JSR JEP 200: The Modular JDK JEP 201: Modular Source Code JEP 220: Modular Run-Time Images JEP 162: Prepare for Modularization (Java8) JSR 376: Java Platform Module System Good source: Mark Reinhold: The state of the module system
  7. 7. Getting started Create your first module: ● traditional JAR file with ● module-info.java module hu.dpc.java9.logger { }
  8. 8. Compile the module ● Could be compiled with standard tools (maven) if there are no dependencies ● Command line tools are also changed javac -d /tmp/modules/hu.dpc.java9.logger -modulepath /tmp/modules -sourcepath src/main/java …..java
  9. 9. Using the logger Create your first module: ● traditional JAR file with ● module-info.java module hu.dpc.java9.logger { exports hu.dpc.java9.logger; }
  10. 10. Using logger public class App { public static void main(String[] args) LoggerFactory.getLogger().log("Hello world"); } } module hu.dpc.java9.app { requires hu.dpc.java9.logger; }
  11. 11. Package level dependencies Source: M Reinhold: The state of the module system
  12. 12. Why we need classloaders? ● Classloader is for creating new classes ● Reference to a class could be used even without explicit export LoggerImpl App LoggerFactory Logger exports requires
  13. 13. Compile and run javac -d /tmp/modules/hu.dpc.java9.logger -modulepath /tmp/modules -sourcepath src/main/java ....java java -mp /tmp/modules -m hu.dpc.java9.app/hu.dpc.java9.App
  14. 14. Backward compatibility java -mp /tmp/modules -m hu.dpc.java9.app/hu.dpc.java9.App java -cp /tmp/modules/hu.dpc.java9.app:/tmp/modules/hu.dpc.java9.logger hu.dpc.java9.App
  15. 15. Packaging: jmod ● Accommodate native code, configuration files, and other kinds of data ● "Whether this new format, provisionally named “JMOD,” should be standardized is an open question."
  16. 16. JDK modules ● Most of the APIs are modularized ● can’t be used without proper requires in module-info
  17. 17. Service layer
  18. 18. Service Layer logger-framework.jar logger-mysql.jar interface Logger{...} class MysqlLogger{...} class LoggerFactory{...
  19. 19. Service Layer logger-framework.jar logger-syslog.jar interface Logger{...} class SyslogLogger{...} class LoggerFactory{...
  20. 20. Service Layer - Java 6-8 Service Provider Interface ● Which are the implementation of a specific interface? ServiceLoader<Logger> loggers = ServiceLoader.load(Logger.class); for (Logger logger: loggers) { logger.log("hello world"); } }
  21. 21. Service Layer - Java 6-8 ServiceLoader<Logger> loggers = ServiceLoader.load(Logger.class); for (Logger logger: loggers) { logger.log("hello world"); } } Definition (in the jar file): META-INF/services/hu.dpc.java9.logger.Logger hu.dpc.java9.logger.internal.LoggerImpl
  22. 22. Service Layer - Java 6-8 logger-framework.jar logger-mysql.jar interface Logger{...} class MysqlLogger{...} class LoggerFactory{...
  23. 23. Service Layer - Java 9 Usage: ServiceLoader<Logger> loggers = ServiceLoader.load(Logger.class); for (Logger logger: loggers) { logger.log("hello world"); } }
  24. 24. Definition In the hu.dpc.java9.logger module: module hu.dpc.java9.logger { exports hu.dpc.java9.logger; provides hu.dpc.java9.logger.Logger with hu.dpc.java9.logger.internal.LoggerImpl; } In the hu.dpc.java9.app module module hu.dpc.java9.app { requires hu.dpc.java9.logger; use hu.dpc.java9.logger.Logger; }
  25. 25. Non requirements ● Modularize the Java Language Specification ● Modularize the Java Virtual Machine Specification ● Multiple versions ● Version selection ● Strict classloader requirements See: http://openjdk.java.net/projects/jigsaw/spec/reqs/
  26. 26. Java 9 vs OSGi Java 9 / Jigsaw OSGi metadata module-info.java (nincs meta adat) META-INF classpath separation exports/requires Import-Package/Export-Package classpath++ transitive imports, fragment bundle/dynamic import classloader hierarchy ClassLoader: unspecified strict, per bundle service layer static (get implementations) dynamic (get implemetations + start/stop listeners) services interface + implementation for the standard services (logging, config...) versioning no yes
  27. 27. Summary/Future ● Summary – classpath separation: private/public – service locator (based on existing SPI) – modularized JVM APIs ● Not scope – versioning! – strict class loader rules ● Shoud be upgraded: – all the IDEs – all the build tools (maven, gradle) – all the JVM based languages (Scala, Clojure) ● SPI – could be more widely adopted

×