Expert Designer at IBTECH - Software Infrastructure em IBTECH ULUSLARARASI BİLİŞİM VE İLETİŞİM TEKNOLOJİLERİ
13 de Nov de 2017•0 gostou•500 visualizações
1 de 42
Java 9 Module System
13 de Nov de 2017•0 gostou•500 visualizações
Baixar para ler offline
Denunciar
Software
An introduction to Java 9 Module System.
Presented at Software Craftsmansip Turkey Community (https://www.meetup.com/Software-Craftsmanship-Turkey/) (08.Nov.2017)
1. Java 9
That's one small step for a man, one giant leap for mankind.
Neil Amstrong
Open, Modular, Small
(Açık, Modüler, Küçük)
2. Who am I?
Married and father of twins.
Graduated from Marmara University CSE in 2002.
Worked 12,5 years at Oksijen in the roles of "Software
Developer", "Software Architect", "Team Leader", "Technical
Coordinator" and "Mentor".
Currently working in IBTech as a "Software Architect".
Developed various applications in different sizes from small
libraries/tools to distributed/telco-grade/backend java
applications.Expert in understanding and beautifying
someone else's code (Refactoring & Clean Coding).
/in/hasanunal
/hasanunalcom
3. Oracle Java History
JDK 1.0 (Jan 1996)
JDK 1.1 (Feb 1997)
J2SE 1.2 (Dec 1998)
J2SE 1.3 (May 2000)
J2SE 1.4 (Feb 2002)
J2SE 5.0 (Sep 2004)
Java SE 6 (Dec 2006)
-------- Sun open sources JDK (Mar 2007)
-------- Oracle acquires Sun (Apr 2009)
Java SE 7 (Jul 2011)
Java SE 8 (Mar 2014)
Java 9 (Sep 2017)
4. Notable Dates
Release - Early 2016 Delayed
Release - September 2016 Delayed
Release - 23 March 2017 Delayed
8 May 2017 EC rejected the JPMS spec.
Release - 21 July 2017 Delayed
Release - 21 September 2017 Released
6. IBM Java 9!
IBM Java Distros
– AIX (OS)
– Linux on Power System (server)
– Linux on z Systems (mainframe)
– z/OS on z Systems (mainframe)
– Windows (for development)
IBM Java 9 beta
Join IBM Open Beta Program
https://developer.ibm.com/javasdk/2017/02/08/our-java-9-beta-is-open-for-business/
7. Java News
OracleJDK is merged with OpenJDK and Oracle distributes
OpenJDK as from Java9.
Commercial features will be in OpenJDK.
Release Cadance: 6 months.
Java 10: http://openjdk.java.net/projects/jdk10/ (Mar 2018)
Oracle leaves
– JEE to Eclipse community
– Netbeans to Apache community
IBM open sources it's own jvm (J9).
9. Main Features of Java 9
91 JEPs are implemented. (link)
JDK
Module System (JPMS)
Versioning Schema
Tools
+Multi-Release JAR Files
+Compile for Older Versions
+jshell (repl) +jlink +jmod
-hprof -jhat
~javac ~java ~jcmd
Security
-SHA-1
+SHA-3
10. Main Features of Java 9
Deployment
Deprecate Java Plugin (Applet)
jlink
Language
@SafeVargs
Try-with-resources
Diamond w/ anonymous classes
Identifier name Underscore (_) removal
Private interface methods.
Javadoc
HTML5 output
Javadoc search
Module System support
11. Main Features of Java 9
Core Libraries
Process API Updates
Variable Handles
Compact Strings
Platform Logging API and Service
More Concurrency Updates
Factory Methods for Collections
Enhanced Method Handles
Enhanced Deprecation
Spin-Wait Hints
Filter Incoming Serialization Data
Stack-Walking API
12. Main Features of Java 9
JVM
Compiler control
Segmented Code Cache (for native code)
JVM Tuning
Unified JVM/GC Logging
Remove GC Combinations Deprecated in JDK 8
Make G1 the Default Garbage Collector
Deprecate the CMS GC
Internationalization
Unicode 8.0
UTF-8 properties
Installer
13. Main Features of Java 9
Client
TIFF Image I/O
Multi-Resolution Images
JavaFX module system support
BeanInfo annotations
HiDPI Graphics on Windows and Linux
Platform-Specific Desktop Features
Nashorn (JS Engine)
Parser API for Nashorn
Implement Selected ECMAScript 6 Features in Nashorn
− Template strings
− let, const, and block scope
− Iterators and for..of loops
− Map, Set, WeakMap, and WeakSet
− Symbols
− Binary and octal literals
14. JPMS – Module System
WHY
Make the Java SE Platform/JDK, more easily
scalable down to small computing devices
Improve the security and maintainability of
Java SE Platform Implementations in general,
and the JDK in particular
Enable improved application performance
Make it easier for developers to construct and
maintain libraries and large applications,
for both the Java SE and EE Platforms.
18. JPMS – Platform Modules
Java Platform is divided into modules.
https://blog.codecentric.de/files/2015/11/jdk-tr1.png
19. JPMS – Platform Modules
Base Module (“java.base”)
Always present
Only module known by module system
Exports all of the platform's core packages
Every other module depends implicitly upon base module
Base module depends upon no other module
module java.base {
exports java.io;
exports java.lang;
exports java.lang.invoke;
exports java.lang.module;
exports java.lang.ref;
exports java.lang.reflect;
exports java.net;
...
}
20. JPMS – Using Modules
Module Path
Means to locate whole modules (similar to classpath)
Resolves module dependences
System path contains module artifacts (jars) or
module directories
e.g. %JAVA_HOME%/jmods;libs
If the module system is unable to find a dependent module
or if it encounters two modules w/ same name
then the compiler or virtual machine report an error and exits
22. JPMS – Implied Readibility
// Application Code
Driver d = DriverManager.getDriver(url); java.sql→
Connection c = d.connect(url, props); java.sql→
d.getParentLogger().info("Connection acquired"); java.logging→
How can the application access “java.logging” module?
Revise “java.sql” module descriptor
module java.sql {
requires public java.logging;
requires public java.xml;
exports java.sql;
exports javax.sql;
exports javax.transaction.xa;
}
If one module exports a package containing a type whose signature refers to a package
in a second module then the declaration of the first module should include a requires
public dependence upon the second.
23. JPMS – Unnamed Module
All jars loaded from the classpath are considered
as a member of Unnamed Module.
Ensures that every type is associated with a module.
The unnamed module reads every other module
(i.e. all named & built-in platform modules)
Thus existing Java 8 application compile and run on Java 9.
(unless api's are deprecated or removed)
The unnamed module exports all of its packages.
But named module can not access types in the unnamed module.
If a package is defined in both a named module and the unnamed
module then the package in the unnamed module is ignored.
24. JPMS – Unnamed Module
If our application is written before Java 8, module dependences
in Java 9 is as below:
Grey covered jars are in classpath, therefore they are defined
as “unnamed module”.
25. JPMS – Bottom-Up Migration
What to migrate to Java 9?
Find the intermodule dependencies (using jdeps)
Use buttom-up approach to select&migrate to modules.
com-foo-app.jar
com-foo-bar.jar
org-baz-qux.jar
It is easy to migrate “org.baz.qux” to module system.
Because it has no dependences.
26. JPMS – Bottom-Up Migration
Continue to bottom-up migration
What if “org.baz.qux.jar” is maintained by another organization and
cannot be converted to a named module?
Is it possible to migrate other jars into modules?
27. JPMS – Automatic Module
A jar w/o module descriptor in modulepath is defined as
automatic module
Accesses all other modules (named and unnamed)
Implicitly exports all of its packages to Unnamed Module
Can be read explicitly by named modules.
Mudule name is automatically determined using jar name
(subject to change)
Removes the file extension
Removes trailing version number
replaces all the non-alphanumeric characters with dots
mysql-connector-java-6.1.6.jar → mysql.connector.java
28. JPMS – Top-Down Migration
− Application jars are not module.
− JRE is Java9. Therefore, platform (java.*) modules exists
in environment.
− Want to convert application jars into modules.
Only com-foo-app.jar and com-foo-bar.jar can be converted.
org-baz-qux.jar is maintained by another organization.
com-foo-app.jar
com-foo-bar.jar
org-baz-qux.jar
Current Situation
29. JPMS – Top-Down Migration
module com.foo.bar {
requires org.baz.qux;
}
module com.foo.app {
requires com.foo.bar;
requires java.sql;
}
Move org-baz-qux.jar to module-path. It will have
automodule name: org-baz-qux.jar → org.baz.qux
Now com-foo-app.jar and com-foo-bar.jar can
depend upon org.baz.qux module.
org.baz.qux
→ Automatic Module
30. JPMS – Top-Down Migration
Notes
Automatic Modules allow an existing application to be migrated
to modules from the top down (first app.jar then other jars)
To migrate the jars in classpath
Find and analyze interdependency (jdeps)
Convert the source code of your organization into modules
Move 3rd
party jars into module-path to make them
automodule
thus, your module code may depend/read the automodules.
(until module versions of 3rd
party jars are prepared)
31. JPMS – Module Types Summary
MODULE TYPE MODULE DESCRIPTOR LOCATION
Application Module Exists Module-path
Automatic Module Doesn't exist Module-path
Unnamed Module Exists or Doesn't exist Class-path
Platform Module Exists Platform's Module-path
MODULE TYPE EXPORTS
PACKAGES
CAN READ
MODULES
CAN BE READ BY
MODULES
Application Module Explicitly Application
Platform
Automatic
Application
Automatic
Unnamed
Platform Module Explicitly Platform All types of modules
Unnamed Module All All types of modules Application
Automatic
Unnamed
Automatic Module All All types of modules Application
Automatic
Unnamed
33. JPMS – Service Loader
Provides loose-coupling among modules.
Based on the java.util.ServiceLoader mechanism.
Service Loader locates the service providers at run-time
by searching jars in classpath.
34. JPMS – Service Loader
Service Loader in Java 7
Service File:
META-INF
/services
/com.example.CodecSet
File Content:
com.example.impl.StandardCodecs # Standard codecs
Code:
Iterator<CodecSet> codecs =
ServiceLoader.load(CodecSet.class).iterator();
foreach(codec) {
//select codec instance.
}
36. JPMS – Services
Highligts
Declaring service relation in module declaration
Improves efficiency and clarity (locating/loading time)
Provides compile&run time accessibility check
Provides compile-time type compatibility check
Provides safe linking prior to run-time
Provides capability to run Ahead-of-Time Compilation
37. JPMS – Reflection
package java.lang.reflect;
public final class Module {
public String getName();
public ModuleDescriptor getDescriptor();
public ClassLoader getClassLoader();
public boolean canRead(Module target);
public boolean isExported(String packageName);
}
Every Class object has an associated Module object
Module object returns by the Class::getModule method
ModuleDescriptor class represents module descriptor
canRead() tells whether the module can read the target module
isExported() tells whether the module exports given package
Class.forName() continues to work as soon as the reflected class
is exported in target module and readable by caller module.
38. JPMS – Class Loader
Modules names/packages don't have to interfere with each other
A module has to be loaded/associated by only one class loader
Exception: Unnamed Module is associated by all class loaders.
Existing Hierarchy is
preserved.
Bootstrap and extension
class loaders load platform
modules.
Application class loader
loads classes of modules in
the module path.
Load classes from one or
more modules.
39.
Besides application modules, new layer may contain upgradable
platform modules as soon as they are loaded from a different location.
During resolution process, modules in new layer can read modules in
lower layers.
CONTAINER
JPMS – Layers
Upgradeable modules
v1.0v1.1
Container launches the
application w/ initial layer (L1)
An upgrade necessitates and
v1.1 modules are loaded in a
new layer (L2) on the top of
initial layer (L1) from a
different jar location.
Container performs this loading
operation by using module
reflection API's and dynamic
class loading.
40. JPMS – Qualified Exports
Allows a package to be exported to specifically-named modules.
module java.base {
...
exports sun.reflect to
java.corba,
java.logging,
java.sql,
java.sql.rowset,
jdk.scripting.nashorn;
}
Can be used to hide the internal implementation from the
unintended users.
Provides a kind of module security. (e.g. in container environment)