Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
By Sander Mak
Java 9 Modularity
in Action
@Sander_Mak
Today's journey
Modularity matters
Java 9 modules
Services
Migration
Linking
Modularity matters
Modularity is the ultimate
agile tool
Good fences
make good neighbours
Hide your internals!
Service contract
Module
Work in progress,
almost there now.
Java 9: Jigsaw, JSR-376
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
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'
Something happened in May...
Java 9: Jigsaw, JSR-376
Drama, intrigue, politics
Drama, intrigue, politics
JCP
Executive
Committee
Drama, intrigue, politics
JCP
Executive
Committee
JEP 200: The Modular JDK
Goodbye classpath
Reliable configuration
Goodbye classpath
Modules with explicit dependencies
my.module
requires
other.module
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)
Why not just use OSGi?
‣ OSGi is built on top of the JVM
‣ Can’t be used to modularise the JDK itself
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
Demo: EasyText
Analyze text complexity
easytext.cli
easytext.analyis
requires
Contracts & Services
MyInterface i = new MyImpl();
How to use code
that you can’t access?
Contracts & Services
Provider
module
Service
Registry
Consumer
module
Register
service
Lookup
service
Return
service
instance
Use an interface as contract
JSR-376 services
module myApi {
exports com.api;
}
JSR-376 services
module myApi {
exports com.api;
}
module myConsumer {
requires myApi;
uses com.api.MyService;
}
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;
}
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();
}
Extensibility
Demo: EasyText
Extensibility
Multiple algorithms:
‣ Flesch-Kincaid
‣ Coleman-Liau
Demo: EasyText
Extensibility
CLI & GUI?
Multiple algorithms:
‣ Flesch-Kincaid
‣ Coleman-Liau
Demo: EasyText
easytext.algorithm.api
easytext.algorithm.kincaid
easytext.algorithm.coleman
easytext.cli
easytext.gui
Demo: EasyText
javafx.controls
JDKApplication
javafx.graphics
easytext.algorithm.api
easytext.algorithm.kincaid
easytext.algorithm.coleman
easytext.cli
easytext.gui
Demo: EasyText
javafx.controls
JDKApplication
ServiceLoader
javafx.graphics
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
Demo: EasyText linking
‣ Use jdeps to audit your code
‣ Escape hatch: 

--add-exports java.base/javax.security.auth.x500=mymod
Preparing for Java 9
‣ 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
Top down migration
commons.lang3.3.4.jar demonstrator.jar
classpath
java.base module path
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
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
Top down migration
commons.lang3.3.4.jar demonstrator.jar
classpath
java.base module path
Top down migration
commons.lang3.3.4.jar
demonstrator.jar
classpath
java.base module path
Can’t reference the classpath
from named modules!
Top down migration
demonstrator.jar
classpath
java.base module path
commons.lang
But this isn’t our code!
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
module demonstrator {
requires commons.lang;
}
Using Automatic Modules
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
Java 9 release
July 27th 2017
Java 9 release
July 27th 2017
Java 9 release
July 27th 2017
September 21st 2017
No promises :-)
‣ JSR-376 forces module-aware tooling
‣ Modularity throughout all
development phases
Java 9's promise
‣ JSR-376 forces module-aware tooling
‣ Modularity throughout all
development phases
Java 9's promise
Spotlight on modularity == good
Thank you.
bit.ly/java9book
@javamodularity
Thank you.
bit.ly/java9book
@javamodularity
40% discount:
AUTHD

More Related Content

Java 9 Modularity in Action