Sei sulla pagina 1di 48

Java Futures: Modules and More

Karen Kinnear
JVM Runtime Lead, Oracle
April 2017

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.


Java 9 Goals
• Enable developer productivity
• faster development
• easier deployment
• language improvements, library improvements
• better support for tooling ecosystem

• Improve Java for Cloud environments


• Java is basis for most commercial Clouds
• security, reliability, throughput, horizontal scalability
• startup time, reduced memory usage, predictability

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2


http://openjdk.java.net/projects/jdk9/
102: Process API Updates
110: HTTP 2 Client
143: Improve Contended Locking
158: Unified JVM Logging
165: Compiler Control
193: Variable Handles
197: Segmented Code Cache
199: Smart Java Compilation, Phase Two
200: The Modular JDK
201: Modular Source Code
211: Elide Deprecation Warnings on Import Statements
212: Resolve Lint and Doclint Warnings
213: Milling Project Coin
214: Remove GC Combinations Deprecated in JDK 8
215: Tiered Attribution for javac
216: Process Import Statements Correctly
217: Annotations Pipeline 2.0
219: Datagram Transport Layer Security (DTLS)
220: Modular Run-Time Images
221: Simplified Doclet API
222: jshell: The Java Shell (Read-Eval-Print Loop)
223: New Version-String Scheme
224: HTML5 Javadoc
225: Javadoc Search
226: UTF-8 Property Files
227: Unicode 7.0
228: Add More Diagnostic Commands
229: Create PKCS12 Keystores by Default
231: Remove Launch-Time JRE Version Selection Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3
232: Improve Secure Application Performance
233: Generate Run-Time Compiler Tests Automatically
235: Test Class-File Attributes Generated by javac
236: Parser API for Nashorn
237: Linux/AArch64 Port
238: Multi-Release JAR Files
240: Remove the JVM TI hprof Agent
241: Remove the jhat Tool
243: Java-Level JVM Compiler Interface
244: TLS Application-Layer Protocol Negotiation Extension
245: Validate JVM Command-Line Flag Arguments
246: Leverage CPU Instructions for GHASH and RSA
247: Compile for Older Platform Versions
248: Make G1 the Default Garbage Collector
249: OCSP Stapling for TLS
250: Store Interned Strings in CDS Archives
251: Multi-Resolution Images
252: Use CLDR Locale Data by Default
253: Prepare JavaFX UI Controls & CSS APIs for Modularization
254: Compact Strings
255: Merge Selected Xerces 2.11.0 Updates into JAXP
256: BeanInfo Annotations
257: Update JavaFX/Media to Newer Version of GStreamer
258: HarfBuzz Font-Layout Engine
259: Stack-Walking API
260: Encapsulate Most Internal APIs
261: Module System
262: TIFF Image I/O
263: HiDPI Graphics on Windows and Linux
264: Platform Logging API and Service
265: Marlin Graphics Renderer
266: More Concurrency Updates
267: Unicode 8.0
268: XML Catalogs
269: Convenience Factory Methods for Collections
270: Reserved Stack Areas for Critical Sections
271: Unified GC Logging
272: Platform-Specific Desktop Features
273: DRBG-Based SecureRandom Implementations Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 4
274: Enhanced Method Handles
Java 9 Modules

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5


Benefits of Java 9 Modules
• Faster evolution
• API provider
• maintain explicit contract
• improves security
• more quickly evolve internal implementation
• API user
• easier to port to dependences when they update
• easier to replace components
• Simplifies deployment
• jlink - only distribute what you need
• only update what you actually use

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6


Problem: Class path hell
common/hadoop-common-3.0.0-SNAPSHOT.jar:common/hadoop-nfs-3.0.0-SNAPSHOT.jar:common/lib/activation-1.1.jar:common/lib/apacheds-i\
18n-2.0.0-M15.jar:common/lib/apacheds-kerberos-codec-2.0.0-M15.jar:common/lib/api-asn1-api-1.0.0-M20.jar:common/lib/api-util-1.0\
.0-M20.jar:common/lib/asm-3.2.jar:common/lib/avro-1.7.4.jar:common/lib/commons-beanutils-1.7.0.jar:common/lib/commons-beanutils-\
core-1.8.0.jar:common/lib/commons-cli-1.2.jar:common/lib/commons-codec-1.4.jar:common/lib/commons-collections-3.2.1.jar:common/l\
ib/commons-compress-1.4.1.jar:common/lib/commons-configuration-1.6.jar:common/lib/commons-digester-1.8.jar:common/lib/commons-ht\
tpclient-3.1.jar:common/lib/commons-io-2.4.jar:common/lib/commons-lang-2.6.jar:common/lib/commons-logging-1.1.3.jar:common/lib/c\
ommons-math3-3.1.1.jar:common/lib/commons-net-3.1.jar:common/lib/curator-client-2.7.1.jar:common/lib/curator-framework-2.7.1.jar\
:common/lib/curator-recipes-2.7.1.jar:common/lib/gson-2.2.4.jar:common/lib/guava-11.0.2.jar:common/lib/hadoop-annotations-3.0.0-\
SNAPSHOT.jar:common/lib/hadoop-auth-3.0.0-SNAPSHOT.jar:common/lib/hamcrest-core-1.3.jar:common/lib/htrace-core4-4.0.1-incubating\
.jar:common/lib/httpclient-4.2.5.jar:common/lib/httpcore-4.2.5.jar:common/lib/jackson-core-asl-1.9.13.jar:common/lib/jackson-jax\
rs-1.9.13.jar:common/lib/jackson-mapper-asl-1.9.13.jar:common/lib/jackson-xc-1.9.13.jar:common/lib/java-xmlbuilder-0.4.jar:commo\
n/lib/jaxb-api-2.2.2.jar:common/lib/jaxb-impl-2.2.3-1.jar:common/lib/jcip-annotations-1.0.jar:common/lib/jersey-core-1.9.jar:com\
mon/lib/jersey-json-1.9.jar:common/lib/jersey-server-1.9.jar:common/lib/jets3t-0.9.0.jar:common/lib/jettison-1.1.jar:common/lib/\
jetty-6.1.26.jar:common/lib/jetty-util-6.1.26.jar:common/lib/jsch-0.1.51.jar:common/lib/json-smart-1.1.1.jar:common/lib/jsp-api-\
2.1.jar:common/lib/jsr305-3.0.0.jar:common/lib/junit-4.11.jar:common/lib/log4j-1.2.17.jar:common/lib/mockito-all-1.8.5.jar:commo\
n/lib/netty-3.6.2.Final.jar:common/lib/nimbus-jose-jwt-3.9.jar:common/lib/paranamer-2.3.jar:common/lib/protobuf-java-2.5.0.jar:c\
ommon/lib/servlet-api-2.5.jar:common/lib/slf4j-api-1.7.10.jar:common/lib/slf4j-log4j12-1.7.10.jar:common/lib/snappy-java-1.0.4.1\
.jar:common/lib/stax-api-1.0-2.jar:common/lib/xmlenc-0.52.jar:common/lib/xz-1.0.jar:common/lib/zookeeper-3.4.6.jar:hdfs/hadoop-h\
dfs-3.0.0-SNAPSHOT.jar:hdfs/hadoop-hdfs-nfs-3.0.0-SNAPSHOT.jar:hdfs/lib/commons-daemon-1.0.13.jar:hdfs/lib/hadoop-hdfs-client-3.\
0.0-SNAPSHOT.jar:hdfs/lib/hpack-0.11.0.jar:hdfs/lib/leveldbjni-all-1.8.jar:hdfs/lib/netty-all-4.1.0.Beta5.jar:hdfs/lib/okhttp-2.\
4.0.jar:hdfs/lib/okio-1.4.0.jar:hdfs/lib/xercesImpl-2.9.1.jar:mapreduce/hadoop-mapreduce-client-app-3.0.0-SNAPSHOT.jar:mapreduce\
/hadoop-mapreduce-client-common-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-client-core-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-ma\
preduce-client-hs-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-client-hs-plugins-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-\
client-jobclient-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-client-nativetask-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-c\
lient-shuffle-3.0.0-SNAPSHOT.jar:mapreduce/hadoop-mapreduce-examples-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-api-3.0.0-SNAPSHOT.jar:\
yarn/hadoop-yarn-applications-distributedshell-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-applications-unmanaged-am-launcher-3.0.0-SNAP\
SHOT.jar:yarn/hadoop-yarn-client-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-common-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-registry-3.0.0-S\
NAPSHOT.jar:yarn/hadoop-yarn-server-applicationhistoryservice-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-server-common-3.0.0-SNAPSHOT.j\
ar:yarn/hadoop-yarn-server-nodemanager-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-server-resourcemanager-3.0.0-SNAPSHOT.jar:yarn/hadoop\
-yarn-server-sharedcachemanager-3.0.0-SNAPSHOT.jar:yarn/hadoop-yarn-server-web-proxy-3.0.0-SNAPSHOT.jar:yarn/lib/aopalliance-1.0\
.jar:yarn/lib/commons-math-2.2.jar:yarn/lib/curator-test-2.7.1.jar:yarn/lib/fst-2.24.jar:yarn/lib/guice-3.0.jar:yarn/lib/guice-s\
ervlet-3.0.jar:yarn/lib/javassist-3.18.1-GA.jar:yarn/lib/javax.inject-1.jar:yarn/lib/jersey-client-1.9.jar:yarn/lib/jersey-guice\
-1.9.jar:yarn/lib/objenesis-2.1.jar

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7


1. Is anything missing?
2. Are there any conflicts?
3. Is it safe to change an internal API?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8


Module System Requirements

Reliable configuration

Strong encapsulation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
9
Accessibility: Can I access Class D?
• accessibility Java 8:
• containers: class, package
• class access: default (package), public Module Y
Package Q

class Q.D

• With modules: Java 9


• containers: class, package, module
• class access: default (package), public*
• public within module, public to specific modules, public to all

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10


What’s a module descriptor?
• Java source file: module-info.java
• Compiled into module-info.class
• Class file lives at top level of a JAR
• Declares module metadata

module-info.java:
module MN { // module name
requires {modifier} ModuleName; // some have dependences
exports PackageName [to ModuleName {, ModuleName}];// some declare exports
opens PackageName [to ModuleName {, ModuleName}]; // some open for reflection
}

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11


Two-way street: Can Class C access public Class D?

Module X Module Y
Package P Package Q
class Q.D
class P.C class Q.D

module X { module Y {
requires Y; exports Q;
} }

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12


Strong Encapsulation
• Module requires other modules
• Fine-grained control at the package level
• module exports packages to specific modules or to all
• module opens package explicitly for reflective access

• Accessibility enforced by compiler, VM and reflection


• sample runtime error message
Exception in thread "main" java.lang.IllegalAccessException: class P.C (in module X) cannot
access class Q.D (in module Y) because module Y does not export Q to module X

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13


Module java.base

java.base
// module-info.java
java.lang module java.base {
java.io
java.net exports java.lang;
java.util exports java.io;
exports java.net;
com.sun.crypto.provider
sun.nio.ch
exports java.util;
sun.reflect.annotation }
sun.security.provider

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14


java.base
hello.world
Reusing a Module java.lang
com.hello.printpkg java.io
// src/module-info.java java.net
module hello.world {
exports com.hello.printpkg;
com.sun.crypto.provid
requires java.base; er
(No concealed) sun.security.provider
}

$ javac -d mods -sourcepath src module-info.java com/hello/MyHelloWorld.java

$ java —module-path mods com.hello.MyHelloWorld


Hello

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15


Concept: the module path
• module path
• path containing directories which contain modules
• Modules can be exploded directories, or modular JARs (other forms too)
• The runtime searches the module path when looking for a module (e.g.,
“Hello.world”)

• In a fully modular application, there is only the module path


• But we can have a mixed module and class-path application too

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16


Why is a module path better than a class path?
• Class path
• Class path can get in trouble when multiple classpath elements contain the
same package
• Class path is searched (linearly) every time a new class is requested

• Module Path
• Modules form a partition of packages
• no package can be in more than one runtime module
• Modules perform a directed search
• Once the runtime finds a module,it remembers its packages
• Never has to search for those packages again
• Class loading becomes O(1), not O(n)

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 17


Where are the versions?
X lib (v1)

hello.worl
d

Y lib (v2)

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 18


hello.worl java.base
Reliable Configuration: d

The module graph

• Resolving a module entails resolving all its dependences


• Transitive closure of a graph
• Modules come from the module path, and from the system image

• Module resolution happens at both compile time and run time


• No missing dependences
• No cyclic dependences
• If any packages exist in more than one resolved module (split package), fail
• Prevent the bad things ahead of time!

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19


Summing up
• Remember our three questions?
• Is anything missing?
• Are there any conflicts?
• Is it safe to change an internal API?

• We can now answer all of these ahead of time


• Missing modules are detected when building the module graph
• Conflicts between packages are detected when building the module graph
• Internal APIs cannot be accessed from outside the module

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 20


The Module System: A Seat Belt, Not A Jetpack

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 21


Modularization of the JDK

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 22


JLink – the Java linker
• The Java linker (jlink) links a set of modules (with their transitive
closure) into a custom, modular runtime image
• Accepts modular JAR, exploded, and JMOD module formats
• Many options for customizing the generated image
• Java creates runtime images for SE modules
• You can optionally create your own custom image
• If your image has a main class, jlink can generate a launcher executable
$ jar --create --file two.jar --main-class b.Epoch -C mods/two .
$ jlink --module-path $JAVA/jmods:two.jar --add-modules two --output image-
two
$ ls image-two/bin
java keytool two
$ image-two/bin/two
1969-12-31 19:00:00.0

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 23


Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 24
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 25
Running Your Code On Java 9

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 26


Moving to Java 9
• Try JDK 9 Early Access binaries on https://jdk9.java.net/download/
• First Try: No Changes
• try to run your application unchanged using your classpath
• clean applications that just depend on java.se should just work

• NoClassDefFoundError ?
• Check your dependences with jdeps -s
• Command-line flags: —add-modules=
• to add a non java.se module to your module graph
• May need to change source to adopt a supported API

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 27


Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 28
Incompatible Changes in the JDK 9 implementation
• Classes removed from sun.misc
• But sun.misc still accessible (for now)
• And sun.misc.Unsafe still there (for now)
• Packages encapsulated
• sun.net.www, sun.security.x509, com.sun.org.apache.xerces.internal.jaxp
• On-disk format of JDK
• rt.jar, tools.jar
• Class loader assumptions
• -Xbootclasspath/p

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 29


Prepare for Java 9 and Modules
• Run jdeps -jdkinternals tool on your application on Java 9 to analyze dependences
• Will your code or libraries you depend on, be affected by Java 9 changes?
• https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

• Project Jigsaw: http://openjdk.java.net/projects/jigsaw/

• Modularity JEPS: openjdk.java.net/jeps/#


• 261: Module System
• 201: Modular Source Code
• 220: Modular Run-Time Images
• 260: Encapsulate Most Internal APIs
• 200: The Modular JDK
• 282: jlink: The Java Linker

• Helpful java and javac module flags


• —add-modules, —add-exports, —add-reads, —add-opens

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 30


Backward Compatibility: The Unnamed Module
• Clean applications that just depend on java.se should just work

• All the code on the classpath gets put into one big module, called the
unnamed module
• Requires everything
• Exports / opens everything
• Maximum compatibility with classpath behavior

• This allows java, libraries and frameworks to incrementally move to


named modules while allowing applications to continue to run

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 31


Migrating Your Code to Modules

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 32


Migrating to Modules
• Read The State of the Module System
• http://openjdk.java.net/projects/jigsaw/spec/sotms/

• Moving your application to a named module


jdeps —-gen-module-info src *.jar (java 9 jdeps)
• creates your module descriptor for you

• You can migrate an application or a library without having migrated libraries


that it depends on.
• Use automatic modules for the library you depend on
• If that library or framework needs to reflect on your types
• may need to open your module or packages

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 33


Migrating
from the top module module
down myapp mylib

jackson- jackson-
jackson-core-2.6.2.jar
databind-2.6.2.jar annotations-2.6.2.jar

module module module module


java.base java.logging java.sql java.xml

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 34


// src/myapp/module-info.java
module myapp {
requires mylib;
requires java.base;
requires java.sql;
requires jackson.core;
requires jackson.databind;
opens com.myapp.domain to jackson.databind;
}

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 35


Forward Migration: Automatic modules
• “Real” modules
• No changes to someone else’s JAR file :-)

• Inferred module descriptor


• Module name derived from JAR file name
• Exports all its packages
• Requires all other modules

• You can explicitly depend on them in your module-info.java

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 36


Java 9 - Ways to make migration easier
• 238: Multi-Release JAR Files
• Allow distributing a single JAR that works with multiple java releases
• Can contain java-release-specific versions of class files

• 247: Compile for Older Platform Versions


• Existing javac options:
•-source: which version of java language compiler accepts
•-target: classfile version created
• New javac option:
• —release
• only use APIs defined for specific release
• and set -source and -target to match

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 37


Post Java 9 Highlights

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 38


Post Java 9 Project Highlights
• Panama
• Improve Java to native interconnections
• Ease-of-Use and performance improvements for Java access to native functions
and data
• Amber
• Productivity-oriented java language features
• 11 Incubator Modules
• Valhalla
• User-defined extended primitives
• http://openjdk.java.net/projects/
• http://openjdk.java.net/jeps/0 - project index

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 39


Project Panama: Java to Native
• Ease of Use
• Java to native library provider
• Tools to extract interface definition from C/ C++ include files
• Bind native library as a java API, potentially standardize
• Library users write normal java code

• Performance
• Optimize Java calls to native
• Optimize Java access to native data

• Non-goals: Not an evolution of JNI


Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 40
Project Panama: Vector API
•  Motivation: expose data-parallel operations through a cross-platform API

•  Maximally expressive and portable API


•  “principle of least astonishment”
•  uniform coverage of operations and data types
•  type-safe

•  Performant
•  High quality of generated code
•  compiled to supported SIMD instructions
•  Competitive with existing facilities for auto-vectorization

•  Graceful performance degradation


•  fallback for missing support in native architectures

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 41


Project Amber - Programmer Productivity
• 286 Local-Variable Type Inference
• Extend type inference to declarations of local variables with initializers
• var list = new ArrayList<String>() // infers ArrayList<String>
• 301 Enhanced Enums
• Allow type-variables in enums (generic enums)
• 302 Lambda Leftovers
• Improve usability of lambda and method references
• e.g. allow underscore (_) for an unnamed, unused parameter
• 181 Nestmates

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 42


Nestmates
• Nest as an container between class and package
• Classes in a nest usually derive from same source file
• Allow access to private members defined in nest
• Aligns JVM access checks with java language rules for members in
nested classes
• Should help build sealed classes and a safe alternative to
Unsafe.defineAnonymousClass()

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 43


Valhalla Goals
• Enable functional-style computation with pure data, for optimized
parallel computations
• Extend language with user-defined types: complex, vector values, tuples
• Improve performance for modern hardware
• “Codes like a class, works like an int”

• Brian Goetz: ”Adventures in Parametric Polymorphism” – JVMLS, 2016


– https://www.youtube.com/watch?v=Tc9vs_HFHVo
– http://www.oracle.com/technetwork/java/jvmls2016-goetz-3126134.pdf

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 44


Value Types
• Immutable, identityless types
• allow JIT optimizations
• allow off-heap allocation (registers, stack, other): reduce GC
• Align JVM memory layout
• flatten contained values - e.g. values in objects or arrays
• faster access: reduced indirection, reduced cache misses
• Minimal Value Types
• Early Adopters experimental value types using MethodHandles
• http://cr.openjdk.java.net/~jrose/values/shady-values.html

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 45


Questions?
• Please get involved - check out openjdk projects
• Help us improve java

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 46


Safe Harbor Statement
The preceding is intended to outline our general product direction. It is
intended for information purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any material, code, or functionality,
and should not be relied upon in making purchasing decisions. The development,
release, and timing of any features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 47

Potrebbero piacerti anche